This question already has answers here:
The left-hand side of an assignment must be a variable, property or indexer
(2 answers)
C# Left-Hand Side Of An Assignment Must Be a Variable, Property or Indexer
(2 answers)
left hand side of an assignment must be a variable [closed]
(6 answers)
Closed 5 years ago.
I am trying to assign value of on datatable value to other datatable
strCustIssueNo=DtMstrCustIssue.Rows[i]["CustIssueNo"].ToString();
DtMstr.Rows[i]["CustIssueNo"].ToString()=strCustIssueNo;
OR
DtMstr.Rows[i]["CustIssueNo"].ToString()=DtMstrCustIssue.Rows[i]["CustIssueNo"].ToString();
but i getting an error "Left Hand Side of assignment must be a variable,property or indexer "
Try this:
DtMstr.Rows[i]["CustIssueNo"] = DtMstr.Rows[i]["CustIssueNo"].ToString();
or
DtMstr.Rows[i]["CustIssueNo"] = strCustIssueNo;
Just remove the ToString on the left side.
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:
Variable is assigned but its value is never used (C#)
(4 answers)
Closed 5 years ago.
int price;
if (listBox1.Text == "Regular McYum")
{
price = 70;
}
How come the 'price' variable is assigned but never used?
The warning is just clear: assigning a variable and using it are different things. The first is you set a value to it, the second ones means you´re doing something with that value.
So in your case you should pass your variable to a method for example:
Console.WriteLine(price);
This should be a warning from your compiler, so you can - although you shouldn´t - ignore this.
If listBox1.Text does not equal "Regular McYum", then price will never get set. You need to ensure that price will receive a value on all of the possible code paths.
This question already has answers here:
What does question mark and dot operator ?. mean in C# 6.0?
(3 answers)
Closed 5 years ago.
I've encountered this operator in C# when dealing with custom events: MyEvent?.Invoke(this, new EventArgs());. What is the purpose of the ?. portion of this statement?
It's making sure it's not null. They have these in swift and their called optionals. If the variable is null then it returns null
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.
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.