module use (simple question)

wildfire1982

Regular
Joined
Oct 23, 2003
Messages
50
Location
Southampton
I have just moved from vb6 to .net and im using a module to store global variables in. I dont get any problems when writing the code but when it compiles and runs, the program gives a runtime exception where it gives "nullReferenceException" i understand what this means but i cant see why im getting it.

The module has publicly declared variables and a jagged array of type string so i assumed that you could use these variables by using

variableName = moduleName.variable

ok so that doesnt work so i tried casting it from the module into the function where its being used i.e

dim variableName as moduleName.variable

still no luck. Im sure its so simple its unbelievable but this is only the 3rd time i have used .net and am still getting to grips with it.

Any pointers would be great. Ta very muchly.

Chris
 
If you use Module, then you can direct use the variable, no need for "Module.variable".

For Object Oriented, I will suggest you use class rather than Module. You can use define "Shared" for variable and use it "Class.variable"
 
Arrays must be dim'd before use. Assuming you've declared them something like:
Public g()() as Int32

Then you must redim both dimensions:
Visual Basic:
        ' ReDim the first element of the jagged array
        ReDim g(5)
        ReDim g(0)(1)
        ReDim g(1)(2)
        ReDim g(2)(5)
        ReDim g(3)(3)
        ReDim g(4)(27)
        ReDim g(5)(42)

If you don't really have a jagged array, but just a multi-dimensional array (declared with "Public g(,) As Int32") then you just need one ReDim with both dimension's sizes, as in:
ReDim g(5, 42)

-Nerseus
 
Thanks very much for that, i had thought about using classes but i just needed to share a few variables so i thought a module would be best. I tried it without the module. but it gave the same error. Excuse the ignorance nerseus but... where should i redim the multi dimensional array? in the module just after the declaration of it or in the functions where the data is to be used.

Thanks for all of your help.

Chris
 
Where to redim them? I have no idea... what makes sense for your application? Maybe in Main you want to call a function to initialize the globals?

And using a Class with static variables/properties is the same as using a Module, but a module is more "sloppy" since it's not really a .NET standard. Behind the scenes (I'm guessing) the compiler is creating a class and static properties for you.

-Nerseus
 
Well the idea is that i have a number of forms which will need to be able to read and write a 2D array. I made the module i made the global variables:

Public ingredientsList(,) As String

and two others for integers.

In my main form i want to write to these quite regularly so first of all i made a reference to the variables in the module:

myIngredients = module1.IngredientList

i decided that wasnt really necessary so now i use calls like:

For counters = 0 To counters = Module1.numIngreds / 2 - 1
If Module1.ingredientsList(counters, 1) = "" Then
Module1.ingredientsList(counters, 1) = Module1.ingredientsList(counters + 1, 1)
Module1.ingredientsList(counters, 2) = Module1.ingredientsList(counters + 1, 2)
counters2 = counters2 + 1
End If
Next

So now on every line that i have made reference to the module variables, i get a NullReference Exception. So, can i initialise the variables in the form_load so that they are initialised for use but the values will not be wiped?

as in

Redim ingredientsList(5,5) etc. The idea of this array is that i dont know how many ingredients there are so i was told that declaring it as i did publicly above would create a dynamic array. Have i been mislead? it looks to me that whatever i redim, will be the size of the array and thats not what i want.

Thanks for your help.
 
Back
Top