If "parameterfield" is a class, which it appears to be, you have to create a new object of this type before using it. You have declared a variable (pr1) of that type, but you have not created a new object, and you have not assigned that object to the variable. Creating an object is done with the New keyword, assigments is done with the = assignment operator. Example:
Code:
Dim x As Button = New Button
The part before the assignment operator is called declaring a variable, it uses the Dim keyword for local declares, next is the name of the variable (x), the As clause is used to specify which type values/objects the variable can hold, and finally "Button" here is the type, a standard .Net class representing a button control incidentally. After the assignment operator you see the New keyword in action, it is followed by the type name for the object that is to be created. Using the New keyword on a class type invokes one of the Sub New methods of that class, these are called "constructors" and exist for the purpose of initializing the class object with default values, a constructor can also be empty if nothing need to be done when the object is created.
Once you understand what all those parts in the above code line means you can get all fancy with shortcuts and stuff and throw it all into apparently one single instruction:
Code:
Dim x As New Button
That code line does all the same three operations as the previous example, but hides the complexisites of object creation and assignment somewhat, the New keyword kind of sneaks into the declaration anonymously.
About the error message, "Object reference not set to an instance of an object", this sounds a bit convoluted, and it is a bit confusing at first. I will explain briefly; variables can hold simple values like the number 1 or a reference to a class object, they are called value types and reference types. A value type variable holds the actual value, while a reference type variable just refers to the object placed somewhere in computer memory. Reference types allows different parts of the program to handle and refer to the same object, where for value types a new copy of the value is created each time it is passed on. In this case you have a reference variable that does not point to any object yet, so "pr1" is your object reference and the problem is that it is not set to point to an object instance (which you also have not created). Let's say you did create a New object, then dereferenced it with the instruction "pr1 = Nothing", then you would also get the same error even if you did create an object, because after assigning the Nothing (null reference) the pr1 object reference once again does not point to an instance of that object type. Perhaps you also noticed the title of the error dialog, it said "NullReferenceException". Visual Studio has more in store for you, if you look at the Error List you will also find a warning for this variable that reads this:
Quote:
|
Variable 'pr1' is used before it has been assigned a value. A null reference exception could result at runtime.
|