codeKiller
codeKiller

Reputation: 5739

Impicit conversion warning

I am doing this simple operation with a string in VB.NET

I have a string of information called segmentInfo looking like this:

XRT0034:3:89:23

So I am just trying to get a number out of it:

Dim rowNum As Integer = segmentInfo.split(":")(1)

And I am getting actually 2 warnings. Both warning are given on the same line of code.

Warning - Implicit conversion from 'String' to 'Integer'
Warning - Implicit conversion from 'String' to 'Char'

Maybe I could understand the first one, about the string to integer...but the second one?? I do not understand.

Anyway, can anybody tell me what I am doing wrong and how to fix it?

Upvotes: 1

Views: 1851

Answers (4)

user5030980
user5030980

Reputation:

Dim rowNum As Integer = Integer.Parse(segmentInfo.split(":"c)(1))

Upvotes: 1

hdkhardik
hdkhardik

Reputation: 662

You must parse it to integer form

Dim rowNum As Integer = Convert.ToInt32(segmentInfo.split(":")(1))

Upvotes: 1

Fabio
Fabio

Reputation: 32445

No such overload method String.Split which take one parameter of type String

From MSDN String.Split Method

You can change to

Dim rowNum As Integer = segmentInfo.split(":"c)(1)

":"c - c is character of type Char Type Characters (Visual Basic)

For converting String to Integer use Int32.TryParse or Int32.Parse method to convert string to integer

Dim rowNum As Int32 = Int32.Parse(segmentInfo.split(":"c)(1))
'Exception will be thrown if string value not convertible to Integer type

Or

Dim rowNum As Int32
If Int32.TryParse(segmentInfo.split(":"c)(1), rowNum) = True Then
    'Use succesfully converted integer value
Else
    'Show message about invalid text
End If

Upvotes: 1

Nico Schertler
Nico Schertler

Reputation: 32597

The Split method takes a Char (array) as a parameter, not a string. Hence:

.... segmentInfo.split(":"c)(1)

Secondly, you need to parse the resulting string to an integer:

Dim rowNum As Integer = Integer.Parse(segmentInfo.split(":"c)(1))

Do this only when you know that the string is a number. Otherwise, use TryParse.

Upvotes: 2

Related Questions