473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 1911
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chiller | last post by:
Ok, this is a continuation of a problem I posted on an earlier thread. I've started another thread because my problem has progressed from the initial constructor problem into a general method...
10
by: dof | last post by:
I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D #include stuff .... ...
1
by: Anonieko Ramos | last post by:
Answer: http://weblogs.asp.net/tims/archive/2004/04/02/106310.aspx by Tim Sneath I've come across the situation on a number of occasions when coding where I've wanted to convert from a string...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
2
by: Andrea V.F. | last post by:
I use the code below (VB.NET) to display a Popup balloon in the Tray Icon of my application. The balloon is displayed, but the timeout never happens and the balloon is always visible even if I...
9
by: Mircea | last post by:
Hi everybody, I have a big problem. I am trying to use serial communication in C# 2. The problem is that the event DataReceived is never fired. I have tried on two computers and it does not...
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
6
by: Rico | last post by:
Hello, I'm looking for a way to reference the string name of an enumerator. I know in VB.net you can do "MyEnum.ToString" and get the name instead of the integer value. Is there a way I can do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.