Results 1 to 7 of 7

Thread: String Problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    23

    String Problem

    I generated 10 integers in the array(9). Then I separate the array into two parts by using substring. But I just can't run the code, it has no error but cannot run. Is my code wrong???

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Randomize()
    Dim rand As New Random
    Dim array(9) As String
    Dim X As String
    Dim Y As String

    array(0) = rand.Next

    Dim HiOrder As String = array(0).Substring(0, 5)
    Dim LoOrder As String = array(0).Substring(5, 5)

    X = 1.3 + (HiOrder * ((2.4 - 1.3) / ((10 ^ 5) - 1)))
    Y = 3.4 + (LoOrder * ((4.7 - 3.4) / ((10 ^ 5) - 1)))

    ListBox1.Items.Add(HiOrder)
    ListBox1.Items.Add(LoOrder)
    ListBox1.Items.Add("X = " & X)
    ListBox1.Items.Add("Y = " & Y)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String Problem

    First up, as I said in another of your threads, there is absolutely no point using the Randomize statement when using the Random class.

    As for your code, how can you get a substring that's 5 characters long from a string that may not be that long? If you expect to get two 5 character substrings from your original string then you have to make sure that your original string is 10 characters long.

    You're generating that original string by calling Random.Next, which is going to return a non-negative Integer, which you then convert to a string. That string could be anything from "0" to "2147483647". The vast majority of values in that range are not 10 characters long. If you want to ensure a 10 character string then you'd have to use the String.PadLeft method to pad with zeroes, but you're also well short of the maximum 10-digit number 0f 9,999,999,999.

    Perhaps you should explain what your app is supposed to do, rather than what you think the code is supposed to do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: String Problem

    The code your posted doesn't really make sense to me. Why do you have a string array and then try to store a random integer value in it?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    23

    Re: String Problem

    I know my code is not wrong...I just wanted to generate random integer and store it in array string. Then I want to use half of the array string for other use. That my application purpose.

    My code can be build and generate...but the success rate of the code is not high....sometime can sometime can't....just want to know why...and how to overcome the problem?? Is there something in my code that not right??

  5. #5
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: String Problem

    I think you are missing the idea of a string.

    Code:
    Dim array(9) As String
    Is actually creating an array of strings, that is you are creating 10 different strings. You are not actually creating a string that can have 10 characters. If you add a breakpoint to this line of code and hover over it you will see something like this:
    array(0) =""
    array(1) =""
    array(2) =""
    array(3) =""
    etc etc

    Just use 1 string:

    Code:
    Dim strMyNumber As String = rand.Next
    Then extract the first 5 characters as you have done, but check the size of the string first:

    Code:
    Dim HiOrder As String = ""
    Dim LoOrder As String = ""
    
    If strMyNumber.Length > 5 Then
     HiOrder = strMyNumber.SubString(0,5)
     LoOrder = strMyNumber.SubString(5) 'anything on from 5
    Else
     HiOrder = strMyNumber
     LoOrder = "0"  'Not enough numbers to fill lo order
    End If

    Note, untested

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    23

    Re: String Problem

    If I use rand.next function, I generate 10 or less integer in one time....can I just generate one integer at a time???

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String Problem

    Quote Originally Posted by pysdex View Post
    If I use rand.next function, I generate 10 or less integer in one time....can I just generate one integer at a time???
    Each call to Random.Next generates one and only one random Integer value. There's no way to generate multiple numbers with one call. If you want multiple numbers then you need multiple calls.

    If you actually mean digits then generating a single digit simply means generating a number in the range 0 to 9. You control the range of the generated number using the arguments you pass to the Next method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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