469,631 Members | 1,305 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

Have some problem with enum and string

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
Sep 21 '06 #1
8 1736
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
Sep 21 '06 #2
Hello!

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

Any better idea?

//Tony

"Sean Chambers" <dk****@gmail.comskrev i meddelandet
news:11**********************@m73g2000cwd.googlegr oups.com...
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

Sep 21 '06 #3
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" <dk****@gmail.comskrev i meddelandet
news:11**********************@m73g2000cwd.googlegr oups.com...
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


Sep 21 '06 #4
tony wrote:
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

Sep 21 '06 #5
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
"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
Sep 21 '06 #6
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 [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:11**********************@m73g2000cwd.googlegr oups.com...
tony wrote:
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

Sep 21 '06 #7
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

"tony" <jo*****************@telia.comschrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP06.phx.gbl...
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


Sep 21 '06 #8
tony <jo*****************@telia.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Chiller | last post: by
10 posts views Thread by dof | last post: by
1 post views Thread by Anonieko Ramos | last post: by
2 posts views Thread by Andrea V.F. | last post: by
9 posts views Thread by Mircea | last post: by
7 posts views Thread by Harris | last post: by
34 posts views Thread by Steven Nagy | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.