using a class without creating it (probably an easy question for those who know)

  • Thread starter Thread starter D
  • Start date Start date
D

D

I was reviewing these vb.net classes which deal with user logins, logouts,
cookies etc etc.

I noticed that in the Page_Load function of a user control the code calls
into another library like so

Call AnotherClassLibrary.AnotherClass.UserLogin(userId)

I do not understand how this code can call this function without first
creating an instance of the AnotherClass.

What is it that I am not understanding?

Thanks

Best Regards.
 
D said:
I was reviewing these vb.net classes which deal with user logins, logouts,
cookies etc etc.

I noticed that in the Page_Load function of a user control the code calls
into another library like so

Call AnotherClassLibrary.AnotherClass.UserLogin(userId)

I do not understand how this code can call this function without first
creating an instance of the AnotherClass.

What is it that I am not understanding?

UserLogin is a *Shared* (static in C# speak, and a lot of the docs)
method of AnotherClass.

Shared members belong to the class-as-a-whole, rather than to
individual instances of the class, which is why you don't need an
object to invoke them on. Shared members exist as a way of putting
class-specific functionality logically 'in' a class definition.

For example, consider the Screen class. Instances of this class
represent actual screens, so for example WorkingArea is a non-Shared
(instance) property - it is a property of an actual Screen.

But consider the method FromPoint that takes a Point and returns a
Screen. Given the OO concept of encapsulation, the only sensible place
for this function to be defined is in the Screen class, but it doesn't
make sense for it to be an instance member. So it is defined as a
Shared member:

Public Shared Function FromPoint(ByVal point As Point) As Screen

and is invoked as Screen.FromPoint.
 
D said:
I noticed that in the Page_Load function of a user control the code calls
into another library like so

Call AnotherClassLibrary.AnotherClass.UserLogin(userId)

I do not understand how this code can call this function without first
creating an instance of the AnotherClass.

Add the 'Shared' attribute to the method you want to access without creating
an instance of the class:

\\\
Public Class AnotherClass
Public Shared Sub UserLogin(ByVal UserID As String)
...
End Sub
End Class
///
 
Larry Lard said:
UserLogin is a *Shared* (static in C# speak, and a lot of the docs)
method of AnotherClass.

Shared members belong to the class-as-a-whole, rather than to
individual instances of the class, which is why you don't need an
object to invoke them on. Shared members exist as a way of putting
class-specific functionality logically 'in' a class definition.

For example, consider the Screen class. Instances of this class
represent actual screens, so for example WorkingArea is a non-Shared
(instance) property - it is a property of an actual Screen.

But consider the method FromPoint that takes a Point and returns a
Screen. Given the OO concept of encapsulation, the only sensible place
for this function to be defined is in the Screen class, but it doesn't
make sense for it to be an instance member. So it is defined as a
Shared member:

Public Shared Function FromPoint(ByVal point As Point) As Screen

and is invoked as Screen.FromPoint.

oh ok. I saw that shared word but I was thinking it was like a global or
public access type of thing.

Thanks
 
Herfried K. Wagner said:
Add the 'Shared' attribute to the method you want to access without
creating an instance of the class:

\\\
Public Class AnotherClass
Public Shared Sub UserLogin(ByVal UserID As String)
...
End Sub
End Class
///

Ok I get it. thanks alot !!
 

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