Results 1 to 8 of 8

Thread: How to get equivalent of VB(.NET) Module? [RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    88

    Question How to get equivalent of VB(.NET) Module? [RESOLVED]

    I can understand that you add a new Class to your project... declare methods and variables inside the new Class... but how do you refer to methods and variables in the helper class from a different one (say a form)? I have been having trouble figuring out exactly the syntaxes involved, normally I would look this up in the help but I can't figure out exactly how to phrase the search and I haven't had much luck. Been flipping through my various C# books too but haven't hit the answer yet. How do you do this?
    Last edited by Crunch; Oct 27th, 2003 at 06:14 AM.

  2. #2
    Lively Member
    Join Date
    Sep 2002
    Posts
    100
    In vb.net you use a reference to the class to get the variable value.

    I do c# so I'm not too familiar with the vb.net syntax or I'd show you...but it's something like this:

    GlobalClass gc = new GlobalClass()
    MessageBox.Show(gc.MyString)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    Oh I think I understand what was giving me problems. I was getting stuck on WHERE to put the instantion of the helper object - when I do it in the main form's constructor or in Form.Load, the object wasn't accessible, but I think I get it now: it goes in the declaration area just after

    public class Form1 : System.Windows.Forms.Form

    ... doesn't it? That appears to work.

    Thanks!

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    Yeah that works for the first form - the trick now is to figure out how to get subsequent forms to be able to see the instance of the helper class. Do you pass each subsequent form a reference to the object gc (in your example)? How do you do this, have the constructor parameters for subsequent forms expect a reference to an object?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    Oh hey I think I got it. In the top of the class declaration for Form2, you put:

    public GlobalClass gc;


    And in the code to instantiate and show Form2, you put:

    Form2.gc = this.gc;

    This appears to work exactly the way I want it to. Thanks for getting me on the right track!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    Even better, declare the variables and methods in the helper class as static and the object does not have to be instantiated, but member variable state persists. THAT'S what I was trying to do! props to Edneeis for helping me get my head around this.

  7. #7
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    In VB.NET, modules are nothing more than classes with a private constructor (disallowing object instantiation), exposing static members when you look under that hood at the IL.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    Yeah I think I get it now. I didn't realize what was going on even though I've been using a Module in my "big" app for app-wide stuff for well over a year

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width