Modules not initializing
-
Hi. I have encountered a peculiar problem with modules that I have never seen before. Here is a very simple example to demonstrate my point:
Module Module1
Dim x as integer = 1
End ModuleModule Module2
Dim y as integer =2
End ModulePrivate Sub Form_Load(bla, bla, bla)
Me.Text = Str(x+y)
End SubWhen the program runs, both modules are automatically initialized so that the result is the value "3" getting sent to the form. Up to now, I have never had a problem with modules not getting initialized. But in a new project I am working on, one gets initialized and the other does not. One of them simply does not get recognized. Adding Public to both/either does not help. Does anyone have an idea as to the kinds of things that can cause this to happen? Many thanks! :)
-
Hi. I have encountered a peculiar problem with modules that I have never seen before. Here is a very simple example to demonstrate my point:
Module Module1
Dim x as integer = 1
End ModuleModule Module2
Dim y as integer =2
End ModulePrivate Sub Form_Load(bla, bla, bla)
Me.Text = Str(x+y)
End SubWhen the program runs, both modules are automatically initialized so that the result is the value "3" getting sent to the form. Up to now, I have never had a problem with modules not getting initialized. But in a new project I am working on, one gets initialized and the other does not. One of them simply does not get recognized. Adding Public to both/either does not help. Does anyone have an idea as to the kinds of things that can cause this to happen? Many thanks! :)
treddie wrote:
But in a new project I am working on, one gets initialized and the other does not.
Modules are initialized on first use, and the compiler has no incentive to change that behaviour. It should act as a static class does in C#.
treddie wrote:
Does anyone have an idea as to the kinds of things that can cause this to happen?
Check your usings; is there any "Module2" in the other namespaces? If there is, then it might be calling the wrong module. Further, those x and y values should be prefixed with their module-name, to ensure that they're taken from that module (and not some other "global" function called "x").
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
treddie wrote:
But in a new project I am working on, one gets initialized and the other does not.
Modules are initialized on first use, and the compiler has no incentive to change that behaviour. It should act as a static class does in C#.
treddie wrote:
Does anyone have an idea as to the kinds of things that can cause this to happen?
Check your usings; is there any "Module2" in the other namespaces? If there is, then it might be calling the wrong module. Further, those x and y values should be prefixed with their module-name, to ensure that they're taken from that module (and not some other "global" function called "x").
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
Eddy Vluggen wrote:
Check your usings; is there any "Module2" in the other namespaces?
No, no others.
Eddy Vluggen wrote:
Modules are initialized on first use, and the compiler has no incentive to change that behaviour.
OK, I think I see what you mean. The form_load procedure is referencing two variables with each declared in each module. Since they are both referenced in the form_load procedure, both modules initialize. But when I change the variable in module2 to "z" instead of "y", there is no reference for it in the form_load, so the compiler deems module2 uneccessary at the present time. At least a test of this confirms it in principle.
Eddy Vluggen wrote:
those x and y values should be prefixed with their module-name
That part I knew about...I was just making a very simple demo which was not "safe".