Problems with a CLASS

  • Thread starter Thread starter William Cruz
  • Start date Start date
W

William Cruz

Ok, here is my situation. I have created a class called “Calculations”
in the main form of my project, I declare this class as “Dim Calcs as
New Calculations” and everything is fine and dandy, but In this same
project I have another form where I also have to access the same class.
So I do the same declaration to access the same class “Dim Calcs as New
Calculations” For some reason other than I’m a rookie programmer
wana-be, the values that I set while in a form other than the main form
do not stay permanent, in other words, as soon as I exit that form, the
value goes back to “0”. Can anyone help me on this one? Thanks

William Cruz
 
William Cruz said:
Ok, here is my situation. I have created a class called "Calculations"
in the main form of my project, I declare this class as "Dim Calcs as
New Calculations" and everything is fine and dandy, but In this same
project I have another form where I also have to access the same class.
So I do the same declaration to access the same class "Dim Calcs as New
Calculations" For some reason other than I'm a rookie programmer
wana-be, the values that I set while in a form other than the main form
do not stay permanent, in other words, as soon as I exit that form, the
value goes back to "0".

You are creating two independent instances of your class instead of a single
one. Instead, pass the reference to the instance you create in the first
form to the second form, for example in a property or a method parameter.
 
In other words William

1. Declare the class in a separate Module (Create a module if you don't have
one already)
2. Declare it this way

Public Calcs as New Calculations

:) and yes, get a good book on OOP.

Regards
Cyril Gupta
 
Cyril said:
In other words William

1. Declare the class in a separate Module (Create a module if you
don't have one already)
2. Declare it this way

Public Calcs as New Calculations

Hmm, you could do it like that, but filling modules up with public objects
is usually a recipe for problems IMO. If this code ever got used from within
ASP.NET, for example, all the web requests will be sharing the same object
instance. Similarly, multiple concurrent threads using the object could
cause the code to act in all sorts of unexpected ways.

A combination of experience and VS2003/VS2005 has made me extremely cautious
of variables declared inside modules.

I'd much prefer to do it as Herfried suggested, and pass it as a parameter
from one class to the next. No concurrency or sharing issues that way.
:) and yes, get a good book on OOP.

I do agree with this though. :)
 
Ok, I just want to make sure I understand. I could just do this:
I create an instance of the class @ the main form like so:

Dim New calcs as Calculation

Then @ the secondary form I could just do this:

Form1.calcs.xxxxx

And all will be fine and safe.

Can you confirm this?


William Cruz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top