简体   繁体   中英

How do I convert each element of a string array to an integer? (VB)

For my current task I have to read in an 8 digit card number using an array. I have declared my array as this at the beginning of my code:

Dim LoyaltyCardDigits(8) As Integer

The reason I declared this as an integer is because I need to carry out calculations on these array elements after it has been read in. I am reading in 8 characters from a textbox and then assigning these characters to a variable.

CardNumber = txtCardNumber.Text

and I am assigning these characters to the array elements; but this is my problem that I am stuck on.

Private Sub btnSubmitDetails_Click(sender As Object, e As EventArgs) Handles btnSubmitDetails.Click

    For Counter = 1 To 8
        LoyaltyCardDigits(Counter) = Mid(CardNumber, Counter, 1)
    Next Counter

So to conclude I would like a solution to convert the array elements from string to integer so I can perform calculations. I have no need to worry about validation as I have already coded it. As I am extremely new to VB I would prefer if any help was as simplified as possible but I will obviously accept anything that works.

You said you already took care of validation, so try something like:

Private Sub btnSubmitDetails_Click(sender As Object, e As EventArgs) Handles btnSubmitDetails.Click
    If CardNumber.Length = 8 Then
        For i As Integer = 0 To 7
            LoyaltyCardDigits(i) = CInt(CardNumber.Substring(i, 1))
        Next

        ' ... now do something with LoyaltyCardDigits ...

    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM