Initializing global variables - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Initializing global variables

EagleEyesEagleEyes Posts: 13Member
Hi all!

I've got a class called Initializer that I use the initialize global variables. It has properties that allow other classes to retrieve these variables.

Here's my problem: I must create an instance of this class to initialize everything. I suppose the best place to do that would be in the very first form that loads, mine's called "MainMenu()". Thing is, as I transfer control to other forms, I dispose of those I don't need anymore so I don't waste resources. Does my instance of Initializer() get destroyed when I dispose of the form that created it?

How do you initialize global variables, and how do you access them? I tried just making a variable Public, thinking that all classes everywhere could access it, but other classes don't seem to recognize it. And I don't see anything like main() or winmain() in a windows forms application, so I have no idea where to stick these variables.

Yeah, I am new to VB.NET, but please help!! Thanks a bundle. If you help me now, I promise to give you a million dollars when I am a multi billionnaire like Bill Gates.



  • karthik_masconkarthik_mascon Posts: 23Member
    I am not able to correctly identify your problem. But I had understood it correctly, then this should solve it.

    Whenever u say Global variables, I hope u mean the ones that u declare inside a Module, as any variable declared in the module is accessible by any form and class from anywhere.

    If u want an instance of a class (for eg., initialiser) accessible from all parts of the program, BETTER DECLARE IT IN THE MODULE.
    (Right click on your solution-->Add Module--> Mymodule.vb).


    dim obj as new Initialiser()
    obj.(some method..)


  • EagleEyesEagleEyes Posts: 13Member
    Thanks for your reply, Karthik.

    I am new to VB.NET so sometimes the terminology stumps me. This is what I really want to do:

    I have a turn-based game where you manage a team of mercenaries and send them to battle. At different times in the game, you will want to hire a new mercenary. At that point in time, it generates a new merc with random statistics, and gives him/her a random name.

    I have 3 text files with names in them - one name on each line. They contain male names, female names and last names. (I will add Karthik to the file because it's a cool name I don't have there yet). Each time I need to create a name for a new merc, I generate a random number which is somewhere between the first and the last record in my name file, and then using a FOR-NEXT loop, cycle through my name file to retrieve the name I randomly selected.

    The problem is, I don't know how long my name files are at the start of the program. I add names to them all the time. So I have a loop which finds that out for me, and stores the result in a variable. To optimize my program, I want to be able to do this loop in the beginning and not have to repeat it every time I want a new merc. The number of names in the name files will not change during runtime.

    So I want a variable of type SHORT to hold the number of names in my name files, and that variable to be accessible from any part of my program.

    I have to read up about what modules are, but as far as I can tell, my whole program is composed of only forms and classes. So I want to know where I should initialize this, because I don't see a main() function or anything where the program starts. I have a book on VB.NET and I am reading it every day, but your help is really appreciated. You can't ask a book a question. Thanks!


Sign In or Register to comment.