+ 1
hey guys,good time, I want make new object from a class,but I would like set or insert a object name from a variable!
look: Bankaccount p1=new Bankaccount(); now I want replace p1 to value of a variable. something like this(it's not work): string a=console.readline(); Bankaccount a =new Bankaccount();
5 Respuestas
+ 1
Currently, in c#, you can't convert a type to an other (int to char for example). Downcast is used when a class is derived from another (see c# course and polymorphism).
+ 1
You can't change the type of a variable : if you declare a variable as a string, you can't convert it to another type (here Bankaccount).
+ 1
what about castung in c#?
if you sey it can't be,so what is your suggestion from other ways
0
Here is why :
In memory :
Adress Value Type
0x00 'a' char
0x01 |
0x02 10 | int
0x03 |
0x04 |
Imagine a variable a which is a char : it takes one byte in memory and it's stored at address 0x00. Create now an other variable, b, which is an int : it will take 4 bytes in memory (0x01, 0x02, 0x03, 0x04). Now, if you try to convert a to an int, it will take 4 bytes (0x00, 0x01, 0x02, 0x03).
You see the problem? You can now overwrite the value of b by changing the value of a...
Actually, the compiler replace all variable names by an Adress in memory, with a specific size that must not change.