Really Newbie: Using classes

I’m confused: I created a class containing several properties. In a local method (say method1) I call these properties by Dim c as New Class1, then c.var1, c.var2 etc.
What if I want to recall, or reuse, some of those properties from another local method (call it method2): do I have to Dim another variable, say f, and call f.var1, f.var2? I don’t think I can Dim f as New Class1 again.

More: if I move method1 inside Class1, how do I address it from a btn in a window to have it perform whatever action is supposed to do?

Thank you

If you want to have this class instantiated past this method then you will need to create a global property which will preserve your class instantiation once your method goes out of scope.

When you instantiate your object inside of this method using ‘Dim c…’ this only creates this class inside of this method’s scope.

Once you have this global property (your class) then you can use it elsewhere.

If you move methods/functions inside of your instantiated class then you just call it using your class.

Just in case Armando is a bit newer than that…

Add a module to the app.
Add a property C of Class1 to the module, with public scope

When your app starts, set

C = new Class1

Now you can access C and its properties anywhere in your app, because it is global.

To be clear, this is what Mike said , but I looked at the explanation and worried that if may have been complicated to follow.

I’m beginning to see the light but I might be back. Thank you both

Here I am again: I did like you said. The code assembles with no errors but when it runs and tries to collect a variable using “c.H=CDbl(AltFld.text)” I get the following error: “An exception of class NilObjectException was not handled. The application must shut down.”

c is initialized as Property (c as Class1 - Global) and H is a Double in the same Module.

Not a property of the class?
If so, dont prefix with C.

which one is a nil object?
If it is C, did you call

C = new Class1

at any time before this line is encountered? (eg in event)


Dim c As New Class1

[quote=237922:@Jeff Tullin]
C = new Class1[/quote]

Or, as an extra tip: You can also instantiate an object just by doing this:

Dim c As New Class1.
See Class1 as a blueprint of something. While c is really a new object created of Class1.

For example:
You create a class called ‘Car’, it holds 2 properties: Brand and Color. It has one methode: Drive.

You want to create an object that holds those properties:

[code]Dim car1 as New Car
car1.Brand = “Ford”
car1.Color = “Green”


But you can also create a car object that is not yet instatiated. You can create an instance of that class later on. For example:

You create an array of cars that are not instantiated and ask the user in a sequence what the properties of that car are, the user can stop creating cars anytime he wants (it’s pseudo code):

Dim cars() as Car

Do until users_presses_stop
Dim c as New Car
c.Brand = Fill_in_what_user_wants
c.Color = Fill_in_what_user_wants

cars(cars_created+1) = c


So, I created an array of uninstantiated cars, and filled it with instantiated cars.

You cannot do this:

Dim c as car c.color = "Blue"

Just because “c” is not a real car at this moment, it’s just a space in memory that can hold an instantiated object of the type ‘car’.

I hope you understand a bit of what I tried to explain.

I see what you mean but… Say I have a created a class as specified before. In App module I set Activate as
" Dim c as AppMod.Class1

c=New AppMod.Class1

Now in a window I have to fill and use a variable (say H) declared as property in the global module which contains Class1 as well. Up to this point, calling Dim c as New AppMod.Class1 and using c.H, everything goes smooth.
What, though, if I have to present the results or use whatever is stored in H in a totally different window which has its own methods? How do I access H again from there? This is what’s buffling me: if I use c.H in this new window I get an error so I can’t figure how to get at the data stored previously inside H.

What I’m trying to get at is: Class1 is defined as global, do I have to dim different variable to get to its properties (one for each window and associated methods)? Do I have to instatiate a variable once and for all (in case where and how should I do it and how can I use it in different places)?
Sorry if I’m a bother guys, as you can see I’m really confused

If you do this:

Dim c as AppMod.Class1 c=New AppMod.Class1

The c you are using here is private within the method where you use Dim
As soon as this method or event ends, the C you have created will be killed off.
Im not sure why this was suggested, because you want to use it everywhere.

So, to recap

Make C a public property of a module.

Do NOT use Dim in a method for a variable you expect to be global.
Just set it to a new instance once when the application starts, with

C = new Class1

Now you can use it anywhere.
If H is a property of Class1, then if you set c.H = 6, it will still be 6 if you access it in a different window.

When you add a property to the module, it creates a reference type variable that has the potential to hold an object, but it does not create the actual object. You must instantiate the object as Jeff describes before you attempt to use it.

I think I made (knock on wood). Thanks for your patience