dimanche 18 mai 2014

c# - valeur de la convertir au type de données au format de chaîne - Stack Overflow


I an c# beginner and working in web development using Silver Light-5 in Visual Studio-2010. I have my GUI on running my code which has it's GUI created by xaml and the button clicks are handled in c#.


Now what i have to achieve is :


I am trying to create a GUI in which i use combo box which will contain options like this (please see this link) http://prntscr.com/36l58s In this link i select the one datatype among the 5 given datatypes (which are byte,sbyte,short,int,long). And after that i want to assign this datatype to a variable in c# code like this: (suppose i selected "short" in that). then i want to assign it to a variable (suppose "varble" here) like this short varble =1000;


How to do that ?


My xaml code for it is :


and c# code for it is (Inside the constructor) :


            comboBox1.Items.Add("byte");
comboBox1.Items.Add("sbyte");
comboBox1.Items.Add("short");
comboBox1.Items.Add("int");
comboBox1.Items.Add("long");

and button click event of ComboBox is :


private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}

Why i am trying to do this? :


Actually ,i was just trying to create a GUI where i can select the datatype of variable at run time among the given specified options and i guess using combobox is best suitable. This selected dataType (for example short in previous example) that selected dataType i then assign to a variable in my code in c#.(Actually i am reading i binary file and i have to select the byte read to store in a variable which must have options of dataType (like short,int,long) in 16/32/64 bit integer which is supposed to be selected by COMBO Box). Could you please help me in doing that ? Thanks a lot (If my logic don't work and you have other alternative then please tell me with full details because i am just a beginner). Thanks again




You can get idea from below code sample :


var selectedDatatype = "String";
var valueToConvert = 1;
var convertedValue = Convert.ChangeType(valueToConvert, Type.GetType("System." + selectedDatatype));

Note: make sure that items populated in dropdown belongs to System namespace. So instead of long you should use Int64



I an c# beginner and working in web development using Silver Light-5 in Visual Studio-2010. I have my GUI on running my code which has it's GUI created by xaml and the button clicks are handled in c#.


Now what i have to achieve is :


I am trying to create a GUI in which i use combo box which will contain options like this (please see this link) http://prntscr.com/36l58s In this link i select the one datatype among the 5 given datatypes (which are byte,sbyte,short,int,long). And after that i want to assign this datatype to a variable in c# code like this: (suppose i selected "short" in that). then i want to assign it to a variable (suppose "varble" here) like this short varble =1000;


How to do that ?


My xaml code for it is :


and c# code for it is (Inside the constructor) :


            comboBox1.Items.Add("byte");
comboBox1.Items.Add("sbyte");
comboBox1.Items.Add("short");
comboBox1.Items.Add("int");
comboBox1.Items.Add("long");

and button click event of ComboBox is :


private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}

Why i am trying to do this? :


Actually ,i was just trying to create a GUI where i can select the datatype of variable at run time among the given specified options and i guess using combobox is best suitable. This selected dataType (for example short in previous example) that selected dataType i then assign to a variable in my code in c#.(Actually i am reading i binary file and i have to select the byte read to store in a variable which must have options of dataType (like short,int,long) in 16/32/64 bit integer which is supposed to be selected by COMBO Box). Could you please help me in doing that ? Thanks a lot (If my logic don't work and you have other alternative then please tell me with full details because i am just a beginner). Thanks again



You can get idea from below code sample :


var selectedDatatype = "String";
var valueToConvert = 1;
var convertedValue = Convert.ChangeType(valueToConvert, Type.GetType("System." + selectedDatatype));

Note: make sure that items populated in dropdown belongs to System namespace. So instead of long you should use Int64


0 commentaires:

Enregistrer un commentaire