By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,056 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,056 IT Pros & Developers. It's quick & easy.

Have some problem with enum and string

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.