471,339 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

A value of an integral type expected

I am getting this error with a switch statement: what doe it mean?

int[] res = new int[] {Convert.ToInt32(arg1 is string[]),
Convert.ToInt32(arg2 is string[])};

switch (res)
{
case new int[]{0,0}:
{
............
}
case new int[]{0,1}:
{
....
}
}

How can I resolve this?

Thanks for your help.
Jun 27 '08 #1
4 6566
On Jun 4, 8:48 am, AA2e72E <AA2e...@discussions.microsoft.comwrote:
I am getting this error with a switch statement: what doe it mean?
switch/case only applies to integral types (including char).

Why are you converting booleans to ints anyway? Are you just trying to
do four different things depending on whether arg1 and arg2 are string
arrays or not? If so, I'd just use if/else if/else if/else. You
*could* do some bitshifting to get a 0-3 value and then switch on
that, but I don't think it would be as readable.

Jon
Jun 27 '08 #2
On Wed, 04 Jun 2008 00:48:00 -0700, AA2e72E
<AA*****@discussions.microsoft.comwrote:
I am getting this error with a switch statement: what doe it mean?
It means that the type used in the switch() statement is required to be an
"integral" type. As in "integer". As in NOT "array of integers".

The switch() statement simply does not work with complex types. Anything
that can be represented as a simple integer value is fine. Anything else
is pretty much not, with the exception of strings.

An equivalent to what it _appears_ you are doing would be this:

switch (arg1 is string ? "1" : "0" + arg2 is string ? "1" : "0")
{
case "00":
break;
case "01":
break;
// etc.
}

Now, that said...given that we're dealing with only four different
possibilities here, and given that it seems likely to me that arguments
that are actually strings are likely to be handled in similar ways to each
other, it might make more sense to just handle each argument individually.

I mean, does the code that depends on whether arg1 is a string _really_
affect how you use arg2, and vice a versa? Unless each case statement has
code that's radically different from every other case statement, I think a
switch() statement is probably not the right construct to use here.

Pete
Jun 27 '08 #3

Thanks for yout help.

"You *could* do some bitshifting to get a 0-3 value and then switch on
that"

How do you do that?

That's what I started out with, get to values 0,1,2,3: could not figure it
out.

Thanks also, Peter.
Jun 27 '08 #4
On Jun 4, 9:31 am, AA2e72E <AA2e...@discussions.microsoft.comwrote:
Thanks for yout help.

"You *could* do some bitshifting to get a 0-3 value and then switch on
that"

How do you do that?

That's what I started out with, get to values 0,1,2,3: could not figure it
out.
int nasty = (arg1 is string[] ? 2 : 0) | (arg2 is string[] ? 1 : 0);

Jon
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Dave | last post: by
2 posts views Thread by bdwise | last post: by
3 posts views Thread by Jon | last post: by
14 posts views Thread by Geoff Jones | last post: by
18 posts views Thread by u.int.32.t | last post: by
10 posts views Thread by Tomás | last post: by
2 posts views Thread by Ranganath | last post: by
reply views Thread by rosydwin | last post: by

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.