Int conversion

S

SK

Hi All,

Dim str As String
Dim myParsedInt As Integer

str = "3,3,4"

myParsedInt = CInt(str)
MessageBox.Show(myParsedInt)

I get 334 as result but I need it to be 3,3,4 as an integer.

Any help!

Thanks,
Sonu
 
J

Jon Skeet [C# MVP]

SK said:
Dim str As String
Dim myParsedInt As Integer

str = "3,3,4"

myParsedInt = CInt(str)
MessageBox.Show(myParsedInt)

I get 334 as result but I need it to be 3,3,4 as an integer.

3,3,4 isn't an integer though - it's three integers.

What single integer do you want to get back, and why?
 
S

SK

Well this is what I am trying to do

Select Case ID
Case 1,3,5
do....
End Select

Now works with it but I need to read 1,3,5 programmatically from a file and
it doesn't work 'coz it treat 1,3,5 as 135 if I use CInt(str).
Where str is a string = "1,3,5"

So I need to read whatever data is there in a file for these values like
4,5,7,8 into a case statement

Select Case ID
Case 4,5,7,8
do....
End Select

I'm stuck :(

Thanks,
Sonu
 
J

Jon Skeet [C# MVP]

SK said:
Well this is what I am trying to do

Select Case ID
Case 1,3,5
do....
End Select

Now works with it but I need to read 1,3,5 programmatically from a file and
it doesn't work 'coz it treat 1,3,5 as 135 if I use CInt(str).
Where str is a string = "1,3,5"

So I need to read whatever data is there in a file for these values like
4,5,7,8 into a case statement

Select Case ID
Case 4,5,7,8
do....
End Select

I'm stuck :(

Right - your problem is that you're trying to parse it as a single
number. 1, 3, 5 represent three different options. You need to:

1) Split the string by commas
2) Parse each of them as an integer
3) Check if the test ID is equal to *any* of the integers
 
S

SK

Could you please give an example..?
Thanks


Jon Skeet said:
Right - your problem is that you're trying to parse it as a single
number. 1, 3, 5 represent three different options. You need to:

1) Split the string by commas
2) Parse each of them as an integer
3) Check if the test ID is equal to *any* of the integers
 
P

Peter Duniho

Well this is what I am trying to do

Select Case ID
Case 1,3,5
do....
End Select

Now works with it but I need to read 1,3,5 programmatically from a file
and it doesn't work 'coz it treat 1,3,5 as 135 if I use CInt(str).
Where str is a string = "1,3,5"

If the string is "1,3,5" and you want to read the string as individual
integers separated by commas, then you may want to use the String.Split()
method to get three different strings, each of which you can parse.

But it's not clear that's actually going to address your question.

In the VB code you posted, the code in the "Case" will be executed if the
integer is _any_ of the three listed. If your file actually specifies a
string reading "1,3,5", does that mean you only want to execute the code
if that string matches _all_ of the options? Does it mean that you want
to execute the code _once_ if the string matches _any_ of the options? Or
does it meant that for each integer in the string, you want to execute the
code in that "Case"? Or perhaps some other possibility I haven't even
mentioned?

As you've asked it, your question is ambiguous and until you state your
question more clearly, no one will be able to provide you with the details
you want.

Pete
 

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