Solved

Basic VB.Net Class Question

Posted on 2009-07-06
11
251 Views
Last Modified: 2013-11-08
How can I have a class created on one (main) form be able to be access from other forms?

Example, when the main window loads, I do something like:
--> Dim User as new UserClass(UserId)
then I can use 'User' on that form, however if I open a new window like so:
--> Dim SettingWindow as new SettingsFrm
--> SettingsWindow.Show
I cannot access the 'User' object...

I know this is how it works, but how (I'm sure basic) way can I get access to the 'User' object from the child window (not a copy, same one, without passing it as a parameter)

Feel like it's a silly question - but looking on Google did not reveal much.
(Code above from head, may not be 100% correct)

500pts.

Thanks,

Andrew
0
Comment
Question by:Crazy_Penguins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 13

Expert Comment

by:drypz
ID: 24791030
Hi! Declare your class as Public.



0
 

Author Comment

by:Crazy_Penguins
ID: 24791050
Okay...

Let me show you what I have, maybe we can work from there...

I make this object in my first form, but cannot seem to access it from other child forms...

Thanks for your help drypz
Public Class CSW_UserSecurity
    Private UserID As Integer
    Public RemoveService As Boolean
    Public RemoveHardware As Boolean
    Public RemovePayment As Boolean
 
    Public Sub New(ByVal UserID As Integer)
        'called when created
        Me.UserID = UserID
        LoadSecurity()
    End Sub
 
    Private Sub LoadSecurity()
        'some DB stuff here, sets the var values
    End Sub
 
End Class

Open in new window

0
 

Author Comment

by:Crazy_Penguins
ID: 24791059
More Information:
'I put this in my primary window:
Dim myUserSecurity As New CSW_UserSecurity(My.Settings.UserID)
 
'I can't use this in a child window
myUserSecurity.aFunctionNameHere
 
'Says it's not declared 

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 15

Accepted Solution

by:
JackOfPH earned 500 total points
ID: 24791183
Try the class below it is using the Singleton Design pattern of GOF....

USAGE:

'On Main window...

Dim myUserSecurity As CSW_UserSecurity = CSW_UserSecurity.CreateInstance(My.Settings.UserID)

In child window...

Dim myUserSecurity As CSW_UserSecurity = CSW_UserSecurity.GetInstance

myUserSecurity.aFunctionNameHere

Public Class CSW_UserSecurity
    Private Shared _CSWObject As CSW_UserSecurity
    Private UserID As Integer
    Public RemoveService As Boolean
    Public RemoveHardware As Boolean
    Public RemovePayment As Boolean
 
    Private Sub New()
 
    End Sub
 
    Public Shared Function GetInstance() As CSW_UserSecurity
        Return _CSWObject
    End Function
 
    Public Shared Function CreateInstance(ByVal UserID As Integer) As CSW_UserSecurity
        If _CSWObject Is Nothing Then
            _CSWObject = New CSW_UserSecurity
            _CSWObject.UserID = UserID
            _CSWObject.LoadSecurity()
        End If
 
        Return _CSWObject
    End Function
 
 
    Private Sub LoadSecurity()
        'some DB stuff here, sets the var values
    End Sub
End Class

Open in new window

0
 

Author Comment

by:Crazy_Penguins
ID: 24791263
Thanks JackOfPh,

However now I get this:
Name '_CSWObject' is not declared.

Open in new window

0
 

Author Comment

by:Crazy_Penguins
ID: 24791265
Opps missed a line, let me re-test
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24791268
Declare something like this in your class...

Private Shared _CSWObject As CSW_UserSecurity
0
 

Author Comment

by:Crazy_Penguins
ID: 24791344
Okay, it all works, but I want to understand why it works...

is the _CSWObject available from all my forms now? (because of the "Private Shared" part?)

why do I have to use '_CSWObject.LoadSecurity()' instead of just 'LoadSecurity()'

Thanks again
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24791357
This will create the instance of the class not the

CreateInstance(My.Settings.UserID)


If _CSWObject Is Nothing Then

'This line of code checks if you already has an object CSQObject created... If the _CSObject is nothing you will create new intance of the class, If there is an existing object already you use the created objects you created before.


       
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24791379
_CSWObject.LoadSecurity() ' This will call the function LoadSecurity in the object that you created.

If you use LoadSecurity you will calling the function from that class not from the object that created...

:)

I hope I explain it, clearly.

It is hard to speak english, if you know what I mean...
0
 

Author Comment

by:Crazy_Penguins
ID: 24791401
Thanks for your help.  I think I got it now.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question