I am checking a value in textbox control “txtType” and I have 3 possible values: it is either TypeA, TypeB or TypeC. So all what I want to do is: This is what I have so far
string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
int updatedType;
If string myType = ‘TypeA’ then set updatedType to 1
If string myType = ‘TypeB’ then set updatedType to 2
If string myType = ‘TypeC’ then set updatedType to 3
I have tried to use switch statement but messed up.
Try this :
string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
int updateType = 0;
switch (myType)
{
case "TypeA":
updateType = 1;
break;
case "TypeB":
updateType = 2;
break;
case "TypeC":
updateType = 3;
break;
default :
throw new ArgumentException("txtType value not supported :"+myType);
}
Have you tried:
switch(myType)
{
case "TypeA":
updatedType = 1;
break;
case "TypeB":
updatedType = 2;
break;
case "TypeC":
updatedType = 3;
break;
default:
break;
}
string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
int updatedType;
switch(myType)
{
case "TypeA"
updatedType = 1;
break;
case "TypeB":
updatedType = 2;
break;
case "TypeC"
updatedType = 3;
break;
default:
updatedType= 0;
}
The switch
statement is fully explained here http://msdn.microsoft.com/library/06tc147t(v=vs.80).aspx and here http://msdn.microsoft.com/en-US/library/k0t5wee3(v=vs.80).aspx
Using switch your code will look like:
string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
int updateType = 0;
switch (myType)
{
case "TypeA":
updateType = 1;
break;
case "TypeB":
updateType = 2;
break;
case "TypeC":
updateType = 3;
break;
default:
// Do some stuff
break;
}
Hope this will help you
string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
int updatedType;
switch (myType)
{
case "TypeA": updatedType = 1;
break;
case "TypeB": updatedType = 2;
break;
case "TypeC": updatedType = 3;
break;
default: updatedType = 0; //Optionnal if myType not in (TypeA, TypeB, TypeC); otherwise, you muste initialize updatedType
break;
}
You switch statement looks nothing like C# but just psuedo code, but the C# equivalent is:
switch(myType){
case "TypeA":
updateType=1;
break;
case "TypeB":
updateType=2;
break;
case "TypeC":
updateType=3;
break;
default:
break;
}
this works for me:
string myType = "typeB";
int updatedType = 0;
switch(myType) {
case "typeA":
updatedType = 1;
break;
case "typeB":
updatedType = 2;
break;
case "typeC":
updatedType = 3;
break;
}
Console.WriteLine("number: " + updatedType .ToString());
If i got you correctly i think what you want is....
string str = "";
int updatedType;
foreach(Control c in this.controls)
{
if(c.GetType() == typeof(TextBox) && c.Name == "txtType")
{
str = c.Text;
}
}
switch(str)
{
case "TypeA":
updatedType = 1;
break;
case "TypeB":
updatedType = 2;
break;
case "TypeC":
updatedType = 3;
break;
default:
break;
}
string[] data = {"", "TypeA", "TypeB", "TypeC"};
string myType = ((TextBox)DV_LogAdd.FindControl("txtType")).Text;
int updatedType = Array.IndexOf(data, myType);
Note: It will return -1, if it can't find the item in the array
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.