Link to home
Start Free TrialLog in
Avatar of Crazy_Penguins
Crazy_Penguins

asked on

Basic VB.Net Class Question

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
Avatar of drypz
drypz
Flag of Philippines image

Hi! Declare your class as Public.



Avatar of Crazy_Penguins
Crazy_Penguins

ASKER

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

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

ASKER CERTIFIED SOLUTION
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks JackOfPh,

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

Open in new window

Opps missed a line, let me re-test
Declare something like this in your class...

Private Shared _CSWObject As CSW_UserSecurity
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
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.


       
_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...
Thanks for your help.  I think I got it now.