Re: Have some problem with enum and string

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Perhaps using Enum.GetValues and Enum.GetNames can be used to convert between
the the string representation of the enum verses the byte values? Not sure
this is what you need but thought I'd give this as an avenue to persue. Hope
this helps.
--
Thom


"tony" wrote:

Hello!

This doesn't work because
m_columnBlowStep [column])
in not an int it's a string.

Any better idea?

//Tony

"Sean Chambers" <dkode8@xxxxxxxxx> skrev i meddelandet
news:1158839557.293577.312640@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Instead of trying to Convert.ToInt32, you have to parse the string and
tell C# that you know the string is an int value like so:

(colBlowStep)Int32.Parse(m_columnBlowStep [column])

That should work.
hope that helps!

Sean

tony wrote:
Hello!

I have below a for loop and a switch in the for loop.
I have also a enum called colBlowStep with some values.
I have also an array[] called m_columnBlowStep with some strings.
All items in the array[] m_columnBlowStep is string because I have used
ToString on each enum item as you can see.

I want to use enum in the case statment in the switch.
I know I can use string but I rather want to use enum.

The problem is that I can't match string that exist in m_columnBlowStep
[column]
with enum that I have in the case statements.
I can't write in this way
switch( (colBlowStep)Convert.ToInt32( m_columnBlowStep [column] ) )
because I can't convert a string to an int.

So what I want is to loop through the for loop check the switch and then
go
to the corresponing
case where I have enum values.

So is it possible to use string as I have with enum in the case.

private enum colBlowStep : byte
{
No, BlowSteps, EndCond, Value, Ref, N_switch
}

private string[] m_columnBlowStep = { colBlowStep.No.ToString(),
colBlowStep.BlowSteps.ToString(), colBlowStep.EndCond.ToString(),

colBlowStep.Value.ToString(), colBlowStep.Ref.ToString(),
colBlowStep.N_switch.ToString() };



for (int column = 0; column < m_flgBlowStep.Columns.Items.Count;
column++)
{
switch( (colBlowStep)Convert.ToInt32( m_columnBlowStep [column] ) )
{
case colBlowStep.No :
...
break;
case colBlowStep.BlowSteps :
...
break;
case colBlowStep.EndCond :
...
break;
case colBlowStep.Value :
...
break;
case colBlowStep.Ref :
...
break;
case colBlowStep.N_switch :
...
break;
}
}


//Tony




.



Relevant Pages

  • Re: enums: What the H? again
    ... > find no sign of it in the Enum class until I did a decompile and saw ... > private String shortName; ... > private static InDir currentState = UNKNOWN; ...
    (comp.lang.java.programmer)
  • Re: enums: What the H? again
    ... Everything comes clear when you decompile an enum, ... private String shortName; ... private static InDir currentState = UNKNOWN; ...
    (comp.lang.java.programmer)
  • A Class to get the name of Enum constant at runtime
    ... Dim rs As ADODB.Recordset ... You can get the description of an Enum form it's value. ... EnumName As String, ... Dim Idx As Long ...
    (microsoft.public.vb.general.discussion)
  • Re: Reflection and variable selection? Late binding?
    ... If your variables are all integers (or any of the supported base Types for Enums) then use an Enum as in my second example. ... If your variables are other Types such as strings or complex Types then you should use the switch statement. ... when each case is doing something very similar: going from string "x" to ...
    (microsoft.public.dotnet.framework)
  • RE: Have some problem with enum and string
    ... I have below a for loop and a switch in the for loop. ... I have also a enum called colBlowStep with some values. ... I know I can use string but I rather want to use enum. ...
    (microsoft.public.dotnet.languages.csharp)