Variable Names That are Created on runtime - vb.net

Is there a way in VB to create variables that are created on run time with that are automatically named
For y As Integer = 0 To 100
Dim Varible{y} as integer
Next

You cannot create the name of a variable at runtime, but you can use a collection of variables with a specified key (name) for each element, or a class with a single public variable or property plus a name property.

Why not just use an array?
Dim Variable(100) As Integer

Related

Global array in module file

I have issue
file 1:
Module1 - file name
In this file I need to declare global array, something like
Dim array1(100) As Integer
And in another file, this is UserForm - I need change values of this array
Module1.array1(2) = 1995 //for example
How can I do it?
I have a compile error: wrong number of dimensions
When you want to define a global or public variable, you need to (a) define it outside of a function/sub definition, and (b) use global or public.
So in your module:
Public array1(100) As Integer
And in your form:
array1(0) = 1995

VB.Net - when shall I use "New" word?

In declaration of variables and objects, when exactly should I use "New" word, and when shouldn't I use it?
I know that I should declare a string without "New" word:
Dim mystring As String
I also know I should use it declaring a datatable:
Dim mytable As New Datatable()
New creates an object that is an instance of the specified class. If you just write the following then you have a reference, but the reference is Nothing as you didn't actually create a Datatable for it to refer to:
Dim mytable As Datatable
You don't typically use New for value types (Numbers, Dates, Booleans, Structures, Enums - a full list is here), as they always have a value (cannot be Nothing). For example this outputs 0:
Dim num as Int32
Console.WriteLine(num)
I wouldn't worry too much about this, but some value types (structures) can be initialised with New, which is somewhat inconsistent, for example:
Dim dec = New Decimal(2, 3, 4, True, 5)

Variable structure visibility in vb.net

I've create a structure in my program like this:
Public Structure Team_Data
Public Shared punti_home As Integer
Dim punti_away As Integer
Dim goal_fatti As Integer
Dim goal_subiti As Integer
End Structure
I use the variable of this structure in a Width for valorize it after a regex parser control, and I don't encounter problems. But if I would use the structure variable in a function like a parameter, so:
pressure(punti_home)
The compiler tells me that the variables isn't declared. Why happean this?
You would need to refer to it as Team_Data.punti_home.

Dynamically create variables in VB.NET

I have been trying to figure this out for some time now and can't seem to figure out an answer to it. I don't see why this would be impossible. I am coding in VB.NET.
Here is my problem:
I need to dynamically create variables and be able to reference them later on in the code.
More Details:
The number of variables comes from some math run against user defined values. In this specific case I would like to just create integers, although I foresee down the road needing to be able to do this with any type of variable. It seems that my biggest problem is being able to name them in a unique way so that I would be able to reference them later on.
Simple Example:
Let's say I have a value of 10, of which I need to make variables for. I would like to run a loop to create these 10 integers. Later on in the code I will be referencing these 10 integers.
It seems simple to me, and yet I can't figure it out. Any help would be greatly appreciated. Thanks in advance.
The best way to do something like this is with the Dictionary(T) class. It is generic, so you can use it to store any type of objects. It allows you to easily store and retrieve code/value pairs. In your case, the "key" would be the variable name and the "value" would be the variable value. So for instance:
Dim variables As New Dictionary(Of String, Integer)()
variables("MyDynamicVariable") = 10 ' Set the value of the "variable"
Dim value As Integer = variables("MyDynamicVariable") ' Retrieve the value of the variable
You want to use a List
Dim Numbers As New List(Of Integer)
For i As Integer = 0 To 9
Numbers.Add(0)
Next
The idea of creating a bunch of named variables on the fly is not something you are likely to see in any VB.Net program. If you have multiple items, you just store them in a list, array, or some other type of collection.
'Dim an Array
Dim xCount as Integer
Dim myVar(xCount) as String
AddButton Event . . .
xCount += 1
myVar(xCount) = "String Value"
'You will have to keep Track of what xCount Value is equal to to use.
'Typically could be an ID in A DataTable, with a string Meaning

vb.net when i build it is showing 0 errors but when i execute it is raising this error

The type for variable 'cri' will not be inferred because it is bound to a field in an enclosing scope. Either change the name of 'cri', of use the fully qualified name (for example, 'Me.cri' or 'MyBase.cri')
I have created a class file with name Predefined which consists of this methods like cricket,BasketBall from this methos im returning List of string type
im calling that class in button click event like the object of class is pre
now im calling a method called pre.Cricket();
so when i execute the program with warnings it is raising this error:
'Cricket' is not a member of 'predefined'.
this is the code where it is showing error in line 2:
Dim cric As New List(Of String)()
cric = pre.Cricket()
For cri As Integer = 0 To cric.Count - 1
See this msdn article:
http://msdn.microsoft.com/en-us/library/bb385162.aspx
(from above)
A loop control variable in your code
has the same name as a field of the
class or other enclosing scope.
Because the control variable is used
without an As clause, it is bound to
the field in the enclosing scope, and
the compiler does not create a new
variable for it or infer its type.
In the following example, Index, the
control variable in the For statement,
is bound to the Index field in the
Customer class. The compiler does not
create a new variable for the control
variable Index or infer its type.
Class Customer
' The class has a field named Index.
Private Index As Integer
Sub Main()
' The following line will raise this warning.
For Index = 1 To 10
' ...
Next
End Sub
End Class