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.
Related
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I want to check if my Listentry = null but I don't know how.
Note:
ID is a Integer
Here is my Code
if(MeineGaeste[i].ID !=null)
{
i++;
}
else
{
MeinGast.ID = i++;
test = false;
}
As stated, an int cannot be null. If a value is not set to it, then the default value is zero. You can do a check for 0 if that is what you think it is being set to...
Otherwise if you are keen on avoiding nullable integers, you would have to turn it into a string and then perform the check.
String.IsNullOrEmpty(ID.ToString());
Which returns a boolean so you can stick it into an if statement.
An int cannot be null. On the other hand a nullable int, int?, can be. That being said the check that you want to do is meaningless.
Changing the type of ID from int to int?, the check you do has a meaning and the compiler wouldn't complain about it.
you can use this ID.HasValue it return true when there is a value and false if null
As a normal int type is a value type, it can not be null.
You can use the nullable type int? for the property ID which is basically a struct, that allows null or any int value. See MSDN or this question or this question for additional documentation.
If it is not possible to change the type of the ID property, a workaround might be to choose an integer as marker value which is guaranteed not appear in your model (for example Int32.MinValue, as most generated IDs seem to be positive integers). Don't do this if you don't have to, as this will most likely lead to errors. I would a least recommend to throw an exception if you serialize models with that marker value.
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.
This question already has answers here:
When should one use nullable types in c#?
(10 answers)
Closed 8 years ago.
the null-able short hand property
public long? id{get;set}
what is the incentive of using it, other than if you are reading it from a database, is there a chance the value might be null...?
also, if you want to get the id,
int a = id.value;
what is the performance impact on this? and in which situation will you be compelled to use this shorthand. Please share your thoughts on this
The advantage is the direct check if is null or no
long v = id ?? v2; //if id is null v will get the v2 value
And the performance is pratically the same of a normal one