Why is my enum.Parse method failing? - c#

I'm trying to dynamically set an enum based on the value in a string so far so good I don't know what I've been doing wrong. I have the following code:
public enum TagLabels : long
{
TurnLeft = 0x0000000000000000000030A38DB1,
TurnRight = 0x00000000000000000000307346CC,
LiftApproach = 0x0000000000000000000012107A8D
}
TagLabels IDs;
string someID = "0x0000000000000000000012107A8D";
IDs = (TagLabels)Enum.Parse(typeof(TagLabels), someID ); //<== I get runtime error on this line
I cannot see what's wrong with what I'm doing.

Enum.Parse is intended to convert a string representation of the symbolic name into an enum val, as in Enum.Parse("TurnLeft"). If what you have is a string giving the numeric value, then you should just parse the string as the corresponding integer type and cast it to the Enum val.
IDs = (TagLabels)long.Parse("0x0000000000000000000012107A8D");

IDs = (TagLabels)Convert.ToInt64(someID, 16);
EDIT: You have a string that is in hex format and not a direct number. So, it needs conversion to int first.
If the Enum value exists, you can cast an int value to Enum type.
EDIT2: Changed after Marc's suggestion from Convert.ToInt32 to Convert.ToInt64

SomeID is a string and your enum is a long.
Try using TurnLeft instead of "0x0000000000000000000012107A8D"

Where is the string you're parsing? Aren't you trying to turn a string like "TurnLeft" into TagLabels.TurnLeft?
MSDN

Related

Parsing the value of an integer using a string?

I have Script2 which has int restaurant1Current, along with several other variables with similar names. On another script, I'm trying to parse the value of restaurant1Current using this code. category is a string with the value of "restaurant1".
string currentStringBuffer = "Script2." + category + "Current";
int currentNumber = int.Parse(currentStringBuffer);
I want to be able to use the same code as I change the value of category hence the need to parse it using a string instead of referencing the variable directly. Is this the correct syntax or am I missing something? As is the code tells me "The input string is not in the correct format" but when debugging it shows that the string contains the value "Script2.restaurant1Current" which again is an int. So why wouldn't I be able to parse the value to currentNumber in the next line?
public Enum Categories{
Restauraunt,
Grocer,
Bank
}
Enum stands for enumeration, it is a numbered list. with this particular enum,
Categories.Bank =2; so you can call or assign.
something.category=2;
or
int rando = Categories.Bank

Regex to validate price (type of decimal)

I have looked through stackoverflow trying to find a solution to this but with no luck so hence why I have resulted in asking the question..
I have a field on my form which is price, type of decimal this is optional depending on what they have selected from a dropdown, So I cant use the [Required] attribute.
When the form is submitted if they have chosen a value from the dropdown which requires the user to enter a postage price I then need to check this field to make sure its a valid decimal so to do this I have the following
public static bool IsValid(decimal postagePrice)
{
var regex = new Regex(#"^\d+.\d{0,2}$");
return regex.IsMatch(postagePrice);
}
But it complains and says "Argument type decimal is not assignable to parameter type string" which I understand, I also can't use Decimal.TryParse as that expects a string.
How can I resolve this (I'm not in a position to change the type from decimal to string either)
If all you want is to verify that the value has at most two decimal positions, you could use a modulo:
public static bool IsValid(decimal postagePrice)
{
return postagePrice % 0.01m == 0m;
}
Regular expressions work on strings - it's that simple.
So in one way or another you'll need to covert the decimal to a string before using a regex to validate it.

C# conversion issue from a generic value to string

I am facing quite an odd issue,....
I have got a code which reads XML and converts each value irrespective of what type it is for example, int, float, double or a String it self to a String value and then stores it into a String variable.
String column = System.Convert.ToString(values.GetValue(rowNum, colNum))
problem I have is, lets say if "values.GetValue(rowNum, colNum)" returns 0.000003825, then when ran, the value that gets converted and stored in "column" is "3.825E-06" which is in scientific notation which I do not really want,
I want "column" to store value 0.000003825 in string format, how do I do that?
thanks
You will need to supply formatting information. Unfortunately, you can't supply formatting information to System.Convert.ToString(). Instead, you must call string.Format() or object.ToString().
For example:
double value = 0.000003825;
string s1 = value.ToString("0.################");
Console.WriteLine(s1);
string s2 = string.Format("{0:0.################}", value);
Console.WriteLine(s2);
Try ToString() to convert it to object
OK, I have fixed this now..
It works a treat irrespective of what the type of value we pass through obj,
thanks.
Object obj = -0.00002357467;
String value = obj.ToString();
String type = obj.GetType().ToString();
if (type.Equals("System.Double")&&value.Contains("E-"))
{
double doubleValue = (double)obj;
value = doubleValue.ToString("0.############################"); //thanks #Matthew Watson
}
Console.WriteLine(value); //prints -0.00002357467

Converting string to int - datareader

trying to figure out how to conver this zip string to an int. I get a cast exception:
member.Zip = reader.GetInt16(ordinals[(int)Enums.MemberColumn.Zip]);
UPDATE:
thanks all. Here's what I came up with that works for me good enough:
Int32.TryParse(reader.GetString(ordinals[(int)Enums.MemberColumn.Zip]), out number) ? number : 0;
You need to get it as a string and then parse that string:
string zipString = reader.GetString(ordinals[(int)Enums.MemberColumn.Zip]);
member.Zip = Int16.Parse(zipString);
DataReaders expect the underlying type of the field to be the same as the specific method you're calling. So GetInt16 requires an underlying 16 bit integer, GetBoolean requires an underlying bit, and GetString requires an underlying string. It won't do any conversion for you.
member.Zip = Convert.ToInt16(reader.GetString(ordinals[(int)Enums.MemberColumn.Zip]));

String.Format in C# not returning modified int value

I am tring to return an int value with comma seperators within the value.
12345 would be returned as 12,345
The follwing code works:
int myInt = 1234567;
MessageBox.Show(string.Format("My number is {0}", myInt.ToString("#,#")));
12,345 is displayed as expected.
While the following code does no work, but from what I am reading, should work:
int myInt = 1234567;
MessageBox.Show(string.Format("My number is {0:#,#}", myInt.ToString()));
12345 is displayed.
Can you help me understand why the second set of code is not working?
Thanks
You shouldn't ToString the int before the format. Try this:
MessageBox.Show(string.Format("My number is {0:#,#}", myInt));
You are converting to string before the formatter has the chance to apply the formatting. You are looking for
MessageBox.Show(string.Format("My number is {0:#,#}", myInt));
myInt.ToString() is redundant since you are using a String.Format(). The point of String.Format is to supply is with a bunch of objects and it will create a string for you. No need to convert it to a string.
In order for the Numeric Format to reflect you need to give it an actual numeric value type not a numeric value that's of type string.
When you give the Format method a string it doesn't take into account any numeric formatting since its already a string type.
int myInt = 1234567;
MessageBox.Show(string.Format("My number is {0:#,#}", myInt));
When you use format strings "inline" as in your second example, the input to the parameter needs to be the original value (i.e int, double, or whatever) so that the format string can do something useful with it.
So omitting the ToString from the second call will do what you want:
int myInt = 1234567;
MessageBox.Show(string.Format("My number is {0:#,#}", myInt));

Categories