|
-
Oct 26th, 2003, 10:28 PM
#1
Thread Starter
Lively Member
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.
-
Oct 27th, 2003, 01:17 AM
#2
Lively Member
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)
-
Oct 27th, 2003, 04:59 AM
#3
Thread Starter
Lively Member
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!
-
Oct 27th, 2003, 05:06 AM
#4
Thread Starter
Lively Member
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?
-
Oct 27th, 2003, 05:35 AM
#5
Thread Starter
Lively Member
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!
-
Oct 27th, 2003, 06:13 AM
#6
Thread Starter
Lively Member
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.
-
Oct 27th, 2003, 08:54 AM
#7
PowerPoster
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.
-
Oct 27th, 2003, 09:06 AM
#8
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|