VARIABLE TYPE CONVERSION
When a variable is defined, the type of that variable and the memory allocated based on that type are also determined. Therefore, assigning a different type of value to that variable will cause an error.
If we are going to operate between variables of different types, we need to perform variable conversion. We use ready-made methods to transform variables.
For example, the Text properties of the TextBox, Label and similar controls are considered as strings. We cannot directly assign the value in the TextBox to a numeric variable. Because the type of the variable and the type of the data trying to be assigned are different.
Likewise, we cannot directly print a numeric variable into Label. In such cases, we need to perform a variable conversion.
We can divide variable transformation operations into two: explicit and implicit transformation.
1- Closed Conversion:
There should be no problem when assigning the value of small-type numeric variables to larger-type variables. Here, the program will do the conversion itself. For example, the value stored by a variable of type byte can be given to a variable of type int. Because int type is a larger data type, it will also be able to store byte type data.
Short a = 245;
int b;
b = a;
This example works fine because the type of variable a is shorter than the type of variable b.
This example also works fine because the float type will be able to store larger numbers than the int type.
However, something like the following will cause an error. Because a variable of type short, int type variables are tried to be a larger value.
Int a=245;
short b;
b = a;
In the example below, different types of variables are defined and they are converted to each other. It is also stated which ones will work and which ones will not.
byte a;
short b;
int c;
long d;
float x;
a=b; (Error)
b=a; (Works)
c=b; (Works)
c=d; (Error)
d=c; (Works)
d=x; (Error)
x=d; (Works)
2- Open Transformation:
In this method, we need to perform the conversion process ourselves. For this, different methods are used depending on the place.
For example, the values (Text properties) of controls such as Label and textBox are always treated as strings. Therefore, it is necessary to perform a transformation when assigning values to or getting values from these controls.
a- Convert method:
int a;
a= Convert.toInt32(TextBox1.Text);
In this example, we convert the value in the text box to an integer and get it into the variable a.
int avg=53;
Label1.Text=Convert.toString(avg);
In this example, we converted the value in the avg variable to a string and wrote it into label1.
b- Parse method:
It is used to convert string type values to number type values. For example, this is one of the methods that can be used when retrieving the value in a form element into a variable.
What we mean by converting text to numbers is to convert a textual data consisting of numbers into a numeric data. In other words, if there are characters other than numbers in the text that is being converted, an error will occur.
int a;
a= int.Parse(textBox1.Text);
long b = long.Parse(textBox2.Text);
c- toString() method:
It is a method that can convert numbers or different types of variables to string type. With this method, only string type conversion can be done.
int b = 100;
label6.Text = b.ToString();
We converted the variable b to string type and wrote it to the label.
label7.Text = comboBox1.SelectedItem.ToString();
We converted the selected element of the combobox control to string type and wrote it to the label.
d- Casting process:
In some cases, this method can be used. For example, it can convert an object type variable or element to other types. It can also convert double to int, that is, convert between numbers . But it cannot convert a string type to an int type or an int type to a string type.
double number = 6.789;
int a;
a = (int)number;
In this example, we have converted the number variable of type double to type int.
Label8.Text = (string)ComboBox1.SelectedItem;
In this example we have converted the selected element of the combobox control to string. In other words, we have converted the object type to string type.
numeric variable to label, getting the number in the textbox into a variable, variable conversion, data type conversion, how to do it, c# convert examples, what is int.parse
EXERCISES
There are no examples related to this subject.
Read 791 times.