Results 1 to 17 of 17

Thread: Sting to Int from Array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    28

    Resolved Sting to Int from Array

    Hi i'm stuck on something so easy, i've googled, tired various methods but cant get it to work. Imagine if you can, i have an array (set as a string type) but the second slot holds a numebr that i want, e.g. 2333.

    So to grab it i use the following code, however as my sport array is set to a string it wont allow it. Like i said i have tried a few ways but nothing works. Such a stupid and simple situation. I'm much much better with actionscript surely its just a string to int conversion?

    Code:
    Dim myNumber As Integer = sport(1)
    Last edited by albo; Apr 7th, 2009 at 10:51 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Sting to Int from Array

    Dim myNumber As Integer = CInt(sport(1))

  3. #3
    Member GeekInOhio's Avatar
    Join Date
    Jun 2008
    Location
    You'll never guess...
    Posts
    56

    Re: Sting to Int from Array

    Code:
    Dim myNumber As Integer = Convert.ToInt32(sport(1))
    In case I forget: I'm using Visual Basic 2008 Express Edition...

    Should I, in my odd, bumbling way, actually offer some assistance, feel free to show me some RATE love.


    Just Another Laptop Hero

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    28

    Re: Sting to Int from Array

    With both methods i get the following error

    "Conversion from string "" to type 'Integer' is not valid."

  5. #5
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Sting to Int from Array

    Perhaps you should use a list(of integer) instead. It'll help avoid issues with blank items.

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Sting to Int from Array

    You will have to make sure that string actually is an integer. It cant be blank, you can use a different object to store the data to resolve this or,

    2 Solutions,

    1) when loading the array if the string is blank load a 0
    2) Do somthing like this, (My VB is rusty)

    Code:
    If(sport(1).Trim() != "") then
        myNumber = Convert.ToInt32(sport(1))
    else 
        myNumber = 0
    End If

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    28

    Re: Sting to Int from Array

    oh does that mean my array has a blank item in it?

  8. #8
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Sting to Int from Array

    Yes, a string of "" is equivilant to String,Empty.

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Sting to Int from Array

    Use:

    Code:
    Dim myNumber As Integer 
    Integer.TryParse(sport(1), myNumber)

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    28

    Re: Sting to Int from Array

    Code:
    If(sport(i).Trim() != "") then
                number = Convert.ToInt32(sport(1))
            Else
                number = 0
            End If
    Is there any reason why the "=" is showing an error? It says identifier expected. Thanks for your help chaps.

    VB Express 2008

  11. #11
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Sting to Int from Array

    != should be <>

    but use Integer.TryParse

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Sting to Int from Array

    Quote Originally Posted by ForumAccount View Post
    != should be <>

    but use Integer.TryParse
    Yep, correct told you my VB was rusty :-S

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    28

    Re: Sting to Int from Array

    Hehe all is good now

  14. #14
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Sting to Int from Array

    Integer.TryParse is the solution you should go for only if you can not be certain that a certain string contains a valid integer representation, possible scenarios where that applies is if you let the user insert a number by using a textbox.

    If you however have the possibility to make sure that the string will always represent an integer, do not use Integer.TryParse but rather Cint(), Integer.Parse or the likes.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  15. #15
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Sting to Int from Array

    Quote Originally Posted by Atheist
    Integer.TryParse is the solution you should go for only if you can not be certain that a certain string contains a valid integer representation, possible scenarios where that applies is if you let the user insert a number by using a textbox.

    If you however have the possibility to make sure that the string will always represent an integer, do not use Integer.TryParse but rather Cint(), Integer.Parse or the likes.
    Not sure if that was to me, but while what you said is valid I suggested Integer.TryParse because he posted this:

    Quote Originally Posted by albo
    With both methods i get the following error

    "Conversion from string "" to type 'Integer' is not valid."
    So I didn't think he would have consistent Integers passing through.

  16. #16
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Sting to Int from Array

    It wasnt aimed at anyone in particular but I felt like it was a relevant addition to the topic

    Indeed it seems like there are empty strings in the array, so what albo should do at first is to find out why this occurs, since it was mentioned in the first post that the second index should contain a number.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  17. #17
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Sting to Int from Array

    Another method is using a converter as shown below

    Code:
    Sub StringToInt()
        Dim MyStringArray() As String = _
            {"100", "", "200", "23", Nothing}
    
        Dim MyIntegerArray() As Integer = _
            Array.ConvertAll(MyStringArray, _
            New Converter(Of String, Integer)(AddressOf _ToInteger))
    
        For Each Item In MyIntegerArray
            Console.WriteLine(Item)
        Next
    End Sub
    Private Function _ToInteger(ByVal value As String) As Integer
        Dim Result As Integer = 0
        If Not Integer.TryParse(value, Result) Then
            Result = 0
        End If
    
        Return Result
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width