How to make unlimited variables (like Num1, Num2, etc)? - vb.net

I am using vb 2012 express (for desktop), and I was wonder how I can make unlimited variables.
For example
Dim Num1 as integer
Dim Num2 as integer
I want the application to go make a new variable with the Num3,4,5,6 etc.
Is this possible? If so, how?

Consider using and an array or a list:
Dim Num As New List(Of Integer) 'Create a list of integers
For i = 0 To Integer.MaxValue 'Add to the list as much as it can hold which is 2147483647 items, it is integer's maximum value.
Num.Add(0)
Next
'OR
Dim NumArray(Integer.MaxValue) As Integer 'Create an array of integers which holds maximum number of items, again 2147483647 items.
'Youy may access them both via their indexes:
Console.WriteLine(Num(0))
Console.WriteLine(Num(1))
Console.WriteLine(Num(2))
'or
Console.WriteLine(NumArray(0))
Console.WriteLine(NumArray(1))
Console.WriteLine(NumArray(2))
'and so on...
Btw, Console.WriteLine() converts integers to strings in this case.

This is something you need arrays for (or possibly a collection class, depending on other needs).
Something like:
Dim Idx As Integer
Dim Num(10) As Integer
' Now you can use Num(0) thru Num(10) '
For Idx = 0 To 10
Num(Idx) = 10 - Idx
Next

Related

How do I generate random numbers from a list of specific numbers that I have?

I want my application to populate random sets of numbers using a list of specific numbers that i chose. For example; I have a set of numbers (1,3,5,9,21,70,56). I want to be able to randomize the order in which these numbers are selected. Is that possible?
If you want to generate a list of 1000 numbers using only those you gave:
Dim r as New Random()
Dim thousand as New List(Of Integer)(1000)
'short way to create an array
Dim onlyFrom = {1,3,5,9,21,70,56}
For i = 1 to 1000
thousand.Add(onlyFrom(r.Next(0, onlyFrom.Length)))
Next i
It repeatedly asks a Random for a random integer between 0 and the array length. Next() may return the lower number, but never the upper number. Documentation
If you want to shuffle those numbers you gave into a random order, easy way to use LINQ:
Dim r as New Random()
Dim onlyFrom = {1,3,5,9,21,70,56}
Dim shuffled = onlyFrom.OrderBy(Function(x) r.Next()).ToArray()
Note: Do not use New Random() in a loop
Randomize()
Dim NumberList= {1,3,5,9,21,70,56}
' Generate random value between 1 and 7, or use NumberList length to make it generic
Dim value As Integer = CInt(Int(( 7 * Rnd()) + 1))
return NumberList(value-1)
* The above code may produce the same value multiple times in a series. so if the requirement is that a different value be produced from the array each time when the code is called seven times, this wouldn't work *
If the requirement is to have a different value from the array each time for the first 7 calls, you may use Shuffle function as laid out here Shuffling an array of strings in vb.net

How to use the value of one variable in the name of another in VB.NET

I am trying to make a simple keygen app (hoping to evolve it later) and am running into a problem with naming my variables:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim keylength As Double = TrackBar1.Value
Dim baseloop As Double = 1
Dim charpos As Double = 1
Do While baseloop < keylength
Randomize()
Dim name As String = "num" & charpos
Dim (name) As Integer = CInt(Int((62 * Rnd()) + 1))
'I would like to name this variable the current value of the variable "name".
baseloop = baseloop + 1
charpos = charpos + 1
Loop
End Sub
The end result should be random doubles (as many as TrackBar1.Value) containing random doubles from 1-62 and named num1, num2, num3, etc.... Then I can replace all the 1s with a, 2 with b, etc etc....
Thanks for your help
As has been said, what you claim to want to do is not possible. You can't declare an arbitrary number of variables at run time because variables are declared at design time. If you want to store an arbitrary number of values then you declare one variable and assign to that variable an array or collection of some sort. An array can be created with any size at run time and collections can grow to an arbitrary size at run time. Basically, what you're trying to do is outright wrong.
If you want an arbitrary number of Double values then create an array with a sized based on a user-defined value and then simply loop to populate it, e.g.
Dim values(userDefinedValue - 1) As Double
For i = 0 To values.GetUpperBound(0)
'...
Next
You then access the values by index. The first value is at index 0, the second at index 1, ..., the last at index GetUpperBound(0).

how i can figuring the highest number in my array...visual basic

how i can figure the highest number in my array...below is the code...can someone help me to solve my problems...n i wan to show the result in the label from the other windows form....thank u... :
Public Class Frm2
Public Parties(9) As String
Public Votes(9) As String
Dim vote As Integer
Dim Party As String
Party = TParty.Text
vote = TVote.Text
For I As Integer = 0 To Parties.Length - 1
If Parties(I) = "" Then
Parties(I) = TParty.Text()
For J As Integer = 0 To Votes.Length - 1
If Votes(J) = "" Then
Votes(J) = TVote.Text()
MsgBox(TParty.Text & TVote.Text & " votes")
TParty.Clear()
TVote.Clear()
Exit Sub
End If
Next J
End If
Next
MsgBox("you can vote now")
If you want to use an algorithm to find the highest number into an array (let's say Votes), the classic is coming from the so-called Bubble Sort:
Dim max As Long 'change the type accordingly, for example if votes are 1-10 then Integer is better
max = Votes(0) 'set the first vote as the max
For j = 1 To Votes.Length - 1
If Votes(j) >= max Then max = Votes(j) 'if another element is larger, then it is the max
Next j
Now the variable max stores the highest value of the array Votes, that you can show anywhere as, for example, in MyForm.MyLabel.Text = max. More useful info here.
Please note that now you declare Public Votes(9) As String, which means they are strings so not usable as numbers. You might want to declare them with a different data type, or use the CInt() method to convert strings in integers as suggested by ja72.
I thought this would only work with a Variant array, but in quick testing it seems to work with an array of Longs as well:
Dim Votes(9) as Long
Dim Max As Long
Max=WorksheetFunction.Max(Votes)
Note that, as Matteo says, you should change Votes() to an array of numeric types. I'd use Long, as it's a native VBA type.
EDIT: As noted by Dee, the code in this question is actually VB.Net. I added that as a tag. In VBA the solution would be even simpler, as Max is an array property:
Max=Votes.Max
(I suppose it would be a good idea to change the variable name from "Max".)

Trouble with Dynamic Arrays

As a noobie to VBA, I am having a Hell of a time understanding how arrays, dynamic arrays in specific, work. I am an Industrial Engineering student, so most of my coding has been done with MatLab.
My question is: Why do I keep getting Run-time error '9' "Subscript out of range" for the following code:
Sub FunWithArrays()
Dim DynArray()
Dim i As Integer
Dim j As Integer
j = 1
For i = 1 To 10
DynArray(j) = i
j = j + 1
ReDim DynArray(UBound(DynArray) + 1)
MsgBox DynArray(i)
Next
End Sub
Any and all help is appreciated!
As people in the comments have mentioned, dynamic arrays are just that: dynamic. That is to say that if you declare an array without dimension, as you have here with Dim DynArray() then it doesn't at this point have any "slots" to store anything.
When an array is declared this way, you then need to Redim it to tell it how many slots you want it to have (presumably after determining this from some other data or user input).
The full code for this would be:
Sub FunWithArrays()
Dim DynArray()
Dim i As Integer
Dim j As Integer
j = 1
Redim DynArray(10)
' redimensioned the array to hold 10 items (since
' you've set the loop as i=1 to 10 you must already
' know that the array wants to hold 10 things)
For i = 1 To 10
'ReDim Preserve DynArray(i)
' this is how you could do it if you didn't know in
' advance that you were going to loop 10 times,
' but as somebody else said, this is a slow way to do it.
DynArray(i) = j
j = j + 1
MsgBox DynArray(i)
' Generally instead of a messagebox here I would use:
' Debug.Print DynArray(i)
' this then prints out the value (on a new line each time)
' into the immediate window which means you can see the
' output without having to click "OK" each time.
Next
End Sub
You can also redimension the array within the loop, or later on, to hold a different number of items, but if you want to keep the items already stored in the array you must use ReDim:
ReDim Preserve DynArray(i)
It's also good practice in general to declare the array type. When you use Dim DynArray() this creates an array of type Variant which can hold anything (String, Double, Object, etc.). If you explicitly declare the type e.g. Dim DynArray() as Integer then it'll only allocate enough memory to hold integers, which is more efficient. For many applications the difference in speed will not make a difference, but when you're dealing with looping through something thousands of times it can matter.

Pick a random number from a set of numbers

I have an array of integers like these;
dim x as integer()={10,9,4,7,6,8,3}.
Now I want to pick a random number from it,how can I do this in visual basic?Thanks in advance...
First you need a random generator:
Dim rnd As New Random()
Then you pick a random number that represents an index into the array:
Dim index As Integer = rnd.Next(0, x.Length)
Then you get the value from the array:
Dim value As Integer = x(index)
Or the two last as a single statement:
Dim value As Integer = x(rnd.Next(0, x.Length))
Now, if you also want to remove the number that you picked from the array, you shouldn't use an array in the first place. You should use a List(Of Integer) as that is designed to be dynamic in size.
randomly choose an index from 0 to length-1 from your array.