Have some problem with enum and string

T

tony

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
 
S

Sean Chambers

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
 
T

tony

Hello!

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

Any better idea?

//Tony

Sean Chambers said:
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
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
 
G

Guest

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 said:
Hello!

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

Any better idea?

//Tony

Sean Chambers said:
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
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
 
J

Jon Skeet [C# MVP]

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

That's the whole point of Int32.Parse - it converts a string into an
int.

Jon
 
G

Guest

try this:

string[] m_columnBlowStep = Enum.GetNames(typeof(colBlowStep));

for (int column = 0; column < m_flgBlowStep.Columns.Items.Count; column++)
{
switch (Enum.Parse(typeof(colBlowStep), 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;
}
}


HTH

Ciaran O'Donnell
 
T

tony

Hello!

I have letters in this field *m_columnBlowStep [column])*
such as "No" that is not convertable to int .
So is it possible to convert from string to enum?

//Tony


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

That's the whole point of Int32.Parse - it converts a string into an
int.

Jon
 
C

Christof Nordiek

Hi tony,

why you are using stringrepresentation?
you could directly put enums in the array.

isntead of

private string[] m_columnBlowStep = { colBlowStep.No.ToString(),
colBlowStep.BlowSteps.ToString(), colBlowStep.EndCond.ToString(),
colBlowStep.Value.ToString(), colBlowStep.Ref.ToString(),
colBlowStep.N_switch.ToString() };

use

private colBlowStep[] m_columnBloyStep = {colBlowStep.No,
colBlowStep.BlowSteps., colBlowStep.EndCond.,
colBlowStep.Value, colBlowStep.Ref,
colBlowStep.N_switch };

and them simply use:

switch (m_columnBlowStep[column])
{
...
}

HTH
 
J

Jon Skeet [C# MVP]

tony said:
I have letters in this field *m_columnBlowStep [column])*
such as "No" that is not convertable to int .

Ah - right, that makes more sense.
So is it possible to convert from string to enum?

Absolutely: use Enum.Parse.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top