Results 1 to 19 of 19

Thread: [RESOLVED] Convert dtring to an Int array?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    Resolved [RESOLVED] Convert dtring to an Int array?

    This is an easy one I'm sure...and I'm also sure it involves a for loop and I'm kicking myself for not rememberign it.

    Info: Using VB Studio 2008
    need to convert a string consisting of numbers ie. "1234567890"
    to a one dimensional int array where each caracter (digit) holds one place in the array.

    Thanks in advance for the help.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Convert dtring to an Int array?

    Welcome to the forums.

    You posted in wrong forum, .Net has its own. Notified moderators to move it for you.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Convert dtring to an Int array?

    Quote Originally Posted by LaVolpe View Post
    Welcome to the forums.

    You posted in wrong forum, .Net has its own. Notified moderators to move it for you.
    That's a shame. It's a piece of cake in VB6. Why not let us solve OP's problem here?
    Code:
    Dim MyStr As String, MyNum(0 To 9) As Integer
    MyStr = "1234567890"
    For I = 1 To Len(MyStr)
        MyNum(I - 1) = CInt(Mid$(MyStr, I, 1))
    Next
    Last edited by Code Doc; Feb 5th, 2011 at 08:42 PM.
    Doctor Ed

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    Re: Convert dtring to an Int array?

    47 views and no help?

    Ok I know it should be something like this
    txtNumber is the text box holding the string...and I need to get all the individual characters of the string into individual elements in a integer array....
    The integer array is intNumberArray()

    For i = 0 To txtNumber.text.length Step 1
    (I have forgotten the commands to actually move data from the string to the array)
    Next i

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Convert dtring to an Int array?

    Quote Originally Posted by Vedren View Post
    47 views and no help?

    Ok I know it should be something like this
    txtNumber is the text box holding the string...and I need to get all the individual characters of the string into individual elements in a integer array....
    The integer array is intNumberArray()

    For i = 0 To txtNumber.text.length Step 1
    (I have forgotten the commands to actually move data from the string to the array)
    Next i
    See Post #3.
    Doctor Ed

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    Re: Convert dtring to an Int array?

    Yeah I don't think it is a .Net issue. I'm taking VBII and were using Visual Studio to create windows applications...

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    Re: Convert dtring to an Int array?

    Thanks for the help but there is a problem with it we have not covered CInt or Mid$ or Len()we're still doing it the beginner (long) way, so while I could use that code I would be just copy pasting it in without actually understanding it which would defeat the purpose. For example were doing ....

    For I = 1 To Len(MyStr)

    like so

    For i = 0 To txtNumber.text.length

  8. #8
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Convert dtring to an Int array?

    Quote Originally Posted by Vedren View Post
    Yeah I don't think it is a .Net issue. I'm taking VBII and were using Visual Studio to create windows applications...
    I used to do this back in the QuickBasic days (please don't ask me the date nor my age at that point in time).
    Doctor Ed

  9. #9
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Convert dtring to an Int array?

    Quote Originally Posted by Vedren View Post
    Thanks for the help but there is a problem with it we have not covered CInt or Mid$ or Len()we're still doing it the beginner (long) way, so while I could use that code I would be just copy pasting it in without actually understanding it which would defeat the purpose. For example were doing ....

    For I = 1 To Len(MyStr)

    like so

    For i = 0 To txtNumber.text.length
    Now you can take it to the top floor. Amazing how much difference there is between VB.Net and the rest of all BASIC. Let the boys upstairs translate my code in Post #3 to Vb.Net for you. That's another pure blunder by Microsoft.
    Doctor Ed

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Convert dtring to an Int array?

    Quote Originally Posted by Code Doc View Post
    That's a shame. It's a piece of cake in VB6. Why not let us solve OP's problem here?
    Short answer. Syntax is not nearly the same. The VB6 solution can lead to more confusion and probably won't work; unless of course, you did post a .Net solution. For example, .Net has many string functions and one probably does what the OP wants.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Convert dtring to an Int array?

    Quote Originally Posted by LaVolpe View Post
    Short answer. Syntax is not nearly the same. The VB6 solution can lead to more confusion and probably won't work; unless of course, you did post a .Net solution. For example, .Net has many string functions and one probably does what the OP wants.
    +1. See my post #9. Hard to believe there is so much difference.

    I suppose it would be interesting to see if .Net can do it in 5 rather simple lines or less. I could reduce it to 3 with colons and get criticized. However, .Net is a far more "robust" language and the language of the future... I yield to the powers in Redmond.
    Last edited by Code Doc; Feb 5th, 2011 at 10:05 PM.
    Doctor Ed

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Convert dtring to an Int array?

    Without knowing what you've covered so far it's difficult to show a suitable example. I'm no .NET Programmer but here is a solution using the Substring method
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myNumbers As String = "13579"
            Dim myIntegers As New ArrayList
            Dim loopControl As Integer
            For loopControl = 0 To myNumbers.Length - 1
                myIntegers.Add(myNumbers.Substring(loopControl, 1))
                Debug.Print(myIntegers(loopControl).ToString)
            Next
        End Sub
    End Class
    EDIT: I guess you may not have covered ArrayList so here's a version using a static Integer Array
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myNumbers As String = "13579"
            Dim myIntegers(myNumbers.Length - 1) As Integer
            Dim loopControl As Integer
            For loopControl = 0 To myNumbers.Length - 1
                myIntegers(loopControl) = myNumbers.Substring(loopControl, 1)
                Debug.Print(myIntegers(loopControl).ToString)
            Next
        End Sub
    End Class
    Last edited by Doogle; Feb 6th, 2011 at 04:12 AM.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Convert dtring to an Int array?

    Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum
    Quote Originally Posted by LaVolpe View Post
    Notified moderators to move it for you.
    Thanks for letting us know

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Convert dtring to an Int array?

    This could be shorter IF it was certain that the string only contained integers

    Code:
            Dim testString As String = "12345AB67890"
            'working with a list in this case
            Dim ints As New List(Of Integer)
    
            For Each c As Char In testString 'examine each character
                ints.Add(New Integer) 'add a palce holder for TryParse
                If Not Integer.TryParse(c, ints(ints.Count - 1)) Then
                    Debug.WriteLine(c & " is not an Integer")
                    ints.RemoveAt(ints.Count - 1) 'remove the bad one
                End If
            Next
            Dim intsA() As Integer = ints.ToArray
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    Re: Convert dtring to an Int array?

    Thanks guys! The substring method is what I needed. Data validation to make sure the string contains nothing but ints was done in a secondary function and if not the program will display and error message in a message box to the user to retype some inputs.

  16. #16
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: [RESOLVED] Convert dtring to an Int array?

    Possibly the simplest way, but I doubt you will have covered this in your course:

    vb.net Code:
    1. Dim chrArray As Char() = TextBox1.Text.ToCharArray
    2. Dim intArray As Integer() = Array.ConvertAll(chrArray, Function(c As Char) Integer.Parse(c))

    Just another suggestion to add to the mix.
    If I helped you out, please take the time to rate me

  17. #17
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: [RESOLVED] Convert dtring to an Int array?

    Quote Originally Posted by J-Deezy View Post
    Possibly the simplest way, but I doubt you will have covered this in your course:

    vb.net Code:
    1. Dim chrArray As Char() = TextBox1.Text.ToCharArray
    2. Dim intArray As Integer() = Array.ConvertAll(chrArray, Function(c As Char) Integer.Parse(c))

    Just another suggestion to add to the mix.
    This is what I was alluding to in my previous post.

    In this case, using Substring, is the last approach I would have taken.

    But
    For Each
    his own
    Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  18. #18
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: [RESOLVED] Convert dtring to an Int array?

    Quote Originally Posted by dbasnett View Post
    But
    For Each
    his own
    Next
    I see what you did there
    If I helped you out, please take the time to rate me

  19. #19
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: [RESOLVED] Convert dtring to an Int array?

    Hmm, I modified it slightly to only accommodate integers in the string so it won't crash if someone puts a stray letter in there.

    vb.net Code:
    1. Dim chrArray As Char() = TextBox1.Text.ToCharArray.Where(Function(c As Char) IsNumeric(c)).ToArray
    2. Dim intArray As Integer() = Array.ConvertAll(chrArray, Function(c As Char) Integer.Parse(c))
    If I helped you out, please take the time to rate me

Tags for this Thread

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