Parse String value of numbers to an integer array

Looking to parse a label and store the values into an integer array. I'm getting a value of 1 dimensional array of string cannot be converted to integer error
Dim values As String
        values = lblDisplay.Text
        Dim tagIDsArray As Integer = Nothing
        tagIDsArray = values.Split(",")

Open in new window

DotNetNoobyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Commented:
Use the TryParse() method of the Integer struct:
Dim tagIDsArray As Integer

If Integer.TryParse(lblDisplay.Text, tagIDsArray) Then
   ' tagIDsArray now holds a valid integer
Else
   MessageBox.Show("Invalid integer value")
End If

Open in new window

Den_HBRCommented:
see code below
Dim values As String
        values = lblDisplay.Text

        Dim tagIDsArrayStrings() As String = Nothing
        tagIDsArrayStrings = values.Split(","c)

        Dim tagIDsArray(tagIDsArrayStrings.Length - 1) As Integer
        For i As Integer = 0 To tagIDsArray.Length - 1
            tagIDsArray(i) = CInt(tagIDsArrayStrings(i))

Open in new window

Den_HBRCommented:
looks like I didn't copy the last line.

10:        next
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Commented:
I missed the part with the commas...  This should account for commas (and it's more ASP-centric):
If Integer.TryParse(lblDisplay.Text.Replace(",", String.Empty), tagIDsArray) Then
	Response.Write(tagIDsArray.ToString())
Else
	Response.Write("Invalid integer value")
End If

Open in new window

DotNetNoobyAuthor Commented:
When I try to pass tagIDsArray to the SetStudentTags method it says Value of type 'Integer' cannot be converted to '1 dimensional array of Integer'
Imports System.Net

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        lblDisplay.Text += txtID.Text & ", "

        txtID.Text = ""
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim tagIDsArray As Integer
        If Integer.TryParse(lblDisplay.Text.Replace(",", String.Empty), tagIDsArray) Then
            Response.Write(tagIDsArray.ToString())
        Else
            Response.Write("Invalid integer value")
        End If

        ViewState("studentTag") = txtStudentTag.Text
        ViewState("tagIDs") = tagIDsArray

        Dim ws As New lms_reference.LMS_Interface
        ws.Credentials = New NetworkCredential("ID", "Password")
        ws.SetStudentTags(ViewState("studentTag"), tagIDsArray))
    End Sub
End Class

Open in new window

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Commented:
That's because your SetStudentTags method is apparently defined to accept an array of integers. Is this intended? Are you trying to basically pass an unformatted number string (e.g. 3000 vs. 3,000) or do you actually need to split the parts of an integer (e.g.  [3][000])? If it is the former, then you should change the definition of SetStudentTags to accept an integer instead of an array. If it is the latter, then Den_HBR's solution should suffice.
DotNetNoobyAuthor Commented:
It is intended to accept an Int Array. I thought that is what I was passing it.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Commented:
Not according to this:

    Dim tagIDsArray As Integer

That is a single integer...  not an array. This is an array of Ints:

    Dim tagIDsArray() As Integer
DotNetNoobyAuthor Commented:
Now I'm getting the Opposite error on tagIDsArray in my TryParse. Value of type'1-Dimensional array of Integer' cannot be converted to 'Integer'.
Untitled.png
Den_HBRCommented:
Please, try my solution.
DotNetNoobyAuthor Commented:
DEN_ HBR it Throws an InvalidCastException
Untitled.png
ZhaolaiCommented:
To fix the above error, change CInt(tagIDsArrayStrings(i))  to Val(tagIDsArrayStrings(i)).
Den_HBRCommented:
replace 'tagIDsArray(i) = CInt(tagIDsArrayStrings(i))'

with

If Integer.TryParse(tagIDsArrayStrings(i), tagIDsArray(i)) Then
                Response.Write(tagIDsArray(i).ToString())
            Else
                Response.Write("Invalid integer value")

            End If
Den_HBRCommented:
Just to make it more fancy...


Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim tagIDsArray As Integer() = Array.ConvertAll(Of String, Integer)(lblDisplay.Text.Split(","c), New Converter(Of String, Integer)(AddressOf ConvertStringToInteger))
        ViewState("studentTag") = txtStudentTag.Text
        ViewState("tagIDs") = tagIDsArray

        Dim ws As New lms_reference.LMS_Interface
        ws.Credentials = New NetworkCredential("ID", "Password")
        ws.SetStudentTags(ViewState("studentTag"), tagIDsArray))
    End Sub

   Private Function ConvertStringToInteger(ByVal pString As String) As Integer
        Dim returnValue As Integer
        Integer.TryParse(pString, returnValue)
        Return returnValue
    End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DotNetNoobyAuthor Commented:
This Works
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.