This question already has answers here:
c# string character replace
(4 answers)
Closed 7 years ago.
I'm having problem in assigning a value to the specific index in the string after checking on the same index:
Here below is my code:
"bits" is a string and "dirtybit" is an integer.
if (bits.ElementAt(dirtybit).Equals('1'))
bits[dirtybit] = '0'; //shows red underlined error
Error:
Property or Indexer String.this[int] cannot be assigned to -- is only read
Why can I not access the same index (value)?
Is there any workaround?
Strings are immutable in C#. You can not change them after you have created them.
You can use StringBuilder to create a new string.
From MSDN:
Strings are immutable--the contents of a string object cannot be
changed after the object is created, although the syntax makes it
appear as if you can do this.
Related
This question already has answers here:
C# interpolated string with conditional-operator [duplicate]
(2 answers)
Closed 4 months ago.
I have a boolean isEuropean and based on it's value I want to write
Console.WriteLine("This individual is/is not European");
Is it possible in C# to add a conditional directly inside a string with no additional variables created?
bool isEuropean = true;
Console.WriteLine($"This individual {(isEuropean ? "is" : "is not")} European");
Yes, you can do string interpolation with a ternary, make sure you contain the ternary in parentheses.
This question already has answers here:
Accessing an instance variable by name (string), kinda like dynamic languages do, in C#
(3 answers)
How to get a property value based on the name
(8 answers)
Closed last year.
Say I have a List called this_list. If I make a string that says "this_list", how can I turn that string into the actual variable? It should do the opposite of what nameof would do.
List<string> this_list = new List<string> { "wow","amazing" };
string str = "this_list";
// str to this_list somehow
If this_list is a local variable (like in your example), you can't. Local variable names are lost during compilation.
If this_list is a field or property, you can use Reflection.
This question already has answers here:
Is there a string math evaluator in .NET?
(18 answers)
operators as strings
(13 answers)
Closed 3 years ago.
I have
i = double.Parse(TextBox.Text);
but when I enter the + symbol, this error appear "System.FormatException: 'Input string was not in a correct format.'"
If I understand correctly, you have a much larger issue than you think.
Your textbox has a string. In this case, "1+1" is the value in your textbox. However, that cannot be parsed to an integer value because it contains the plus sign. The plus sign is a character, it is not an integer (0,1,2,3,4..). So, what you get is a data type conversion conflict.
From what I gather, you'd like to evaluate that expression and then store the value into into the i variable. In this case, you would like i to equal 2.
You will need to evaluate the string and convert it into a formula then use the result to store in the variable.
Here's a link to an example of a conversion formula.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/7f62b87d-a35c-4074-a0f0-84a9dd7ff0a5/convert-string-to-formula?forum=csharpgeneral
This question already has answers here:
Is the order of elements on a C# List<T> deterministic?
(3 answers)
Closed 8 years ago.
Follow up question from How to select List<> by its index and return the content?
Can someone please comment if the elements in the list are expected to retain order? In the example above, is "Belly Buster" always expected to be at index 1 or are there cases where that might not be true?
List<string> pizzas = new List<string>();
pizzas.Add("Angus Steakhouse");
pizzas.Add("Belly Buster");
pizzas.Add("Pizza Bianca");
pizzas.Add("Classic Cheese");
pizzas.Add("Friday Special");
string result = pizzas[1]; // result is equal to "Belly Buster"
index starts at 0 so yes if the code stays the same..belly buster will always be at 1
This question already has answers here:
A field initializer cannot reference the nonstatic field, method, or property
(6 answers)
Closed 8 years ago.
I am trying to convert a textBox entry into an integer value so it can be used in an equation.
When I try create the int value using
int number1 = int.Parse(num1.Text);
I get an error:
A field initializer cannot reference the non-static field, method, or property 'Tutorial_Form.Form1.num1'
You would have to put the code to set that field in a method.
Does it really make sense to initialise a field using the contents of a TextBox though? The user can't possibly have entered anything into the control, so there's no integer to parse. You need to consider when it is that you want to get the number from the input and put your code there, e.g. the Click event handler of a Button.