|
-
Mar 8th, 2010, 11:09 PM
#1
Thread Starter
Junior Member
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
-
Mar 8th, 2010, 11:21 PM
#2
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.
-
Mar 8th, 2010, 11:22 PM
#3
Hyperactive Member
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?
-
Mar 9th, 2010, 08:22 AM
#4
Thread Starter
Junior Member
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??
-
Mar 9th, 2010, 08:46 AM
#5
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
-
Mar 9th, 2010, 09:13 PM
#6
Thread Starter
Junior Member
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???
-
Mar 9th, 2010, 09:24 PM
#7
Re: String Problem
 Originally Posted by pysdex
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|