Converting textBox value to integer [duplicate] - c#

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.

Related

opposite of nameof (string of variable name to variable) C# [duplicate]

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.

I have a Textbox.Text which content is 1+1. How to I convert this into double and store it in i? [duplicate]

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

Variable assigned but never used [duplicate]

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.

Trying to convert the value of a textbox to an int C# .NET [duplicate]

This question already has answers here:
How can I convert String to Int?
(31 answers)
Closed 6 years ago.
I am currently trying to use the value of a textbox as the value of an integer in a Timer so its like this
User inputs a value
my application reads it and uses the input as a value of interval in a timer
but its giving me this error, whats going on?
Here is what it looks like
You Need to Convert into Integer from string.Then you can use it.
exp:
int timeInterval = Convert.ToInt32(txtTime.Text);
Use can use the Variable value(timeInterval).
Thread.Sleep() takes an integer value, but you are passing the string value into the function. Any value taken from the user input like as Textbox.Text is treated as string and you need to convert that to an int.
You will have to convert your String to Int like so:
int timeInterval = Int32.Parse(txtTime.Text);

not allowing to update value for a specific index [duplicate]

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.

Categories

Resources