Question about OOP and Class Member in VB.net

G

Guest

Hello, everyone. I was hoping if someone could help me out with a problem
that I'm trying to solve? I have a project that I'm working on. As the
splashscreen launches for an application I check the directory for the
location of a file and I assign that directory path to a class member in my
main module; so, I have the following syntax cClass.mydirectory = (path of
file). However, when I Load a form I have to recreate the class object cClass
and the member mydirectory is now blank. I was under the impression that once
a class object is created it's stored in memory and the code should be able
to access that class member from memory. Am I wrong in assuming this? Can a
class be made global or referenced for access from all forms? Thanks.
 
J

JohnFol

The code behind the splash screen creates an instance of the class that is
"In scope" of the splash screen. If a.n.other form / module / class creates
an instance then you have multiple instances, all with different
..MyDirectory properties.

You could have a Sub Main that holds a single instance of the class, ant it
becomes responsbile for launching the splash screen and the subsequent form.
Then there is only 1 instance and the property is retained.
 
G

Guest

Thanks JohnFol. I'll give that a shot.

JohnFol said:
The code behind the splash screen creates an instance of the class that is
"In scope" of the splash screen. If a.n.other form / module / class creates
an instance then you have multiple instances, all with different
..MyDirectory properties.

You could have a Sub Main that holds a single instance of the class, ant it
becomes responsbile for launching the splash screen and the subsequent form.
Then there is only 1 instance and the property is retained.
 
G

Guest

Ok, I placed the class in the main sub but I still loose scope when I go to a
form. Here is what my main sub looks like:
Imports System.IO
Imports System.Data.OleDb

Module MainModule
Dim vfClass As New FoxConnectionClass 'create class
Public Sub Main()
'Dim vfClass As New FoxConnectionClass 'create class
'declare object for each form.
Dim frmSplash As New fclsSplash
Dim mainForm As New fclsMain
Dim SqlLogin As New frmLogin

'Directory structure to fox table.
vfClass.mydirectory = Directory.GetCurrentDirectory & _
"\sysconn.dbf"

Try
System.Windows.Forms.Application.EnableVisualStyles()
System.Windows.Forms.Application.Run(frmSplash)
(Remaining Code.....)

Unfortunately, when I try to access the vfClass.mydirectory method from my
sqlLogin form it finds vfClass as not being declared. Any help would be
greatly appreciated...
 
S

Sean Hederman

Terrance,

Keep in mind the difference between a class and an object. A class is what
you wrote, and an object is an instance of that class. So if you create two
instances of the class:
Dim vfObject1 As New FoxConnectionClass
Dim vfObject2 As New FocConnectionClass

They are totally different classes. Any properties set on one, will not
propagate to the other. However, the declaration (Dim) is actually declaring
a reference to the class (New FoxConnectionClass), so if you did:

Dim vfObject1 As New FoxConnectionClass
....
Dim vfObject2 = vfObject1

Now, both the vfObject1 reference and the vfObject2 reference are pointing
to the same object.

In your case, you're going to have to pass a reference to your vfClass into
your frmSplash.

So you could change your frmSplash constructor to be:
Public frmSplash(ByVal vfConnection As FoxConnectionClass)...

And then assign the passed-in reference to a local reference.
 

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

Top