Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shared Functions what are they??

Status
Not open for further replies.

Modica82

Technical User
Jan 31, 2003
410
GB
Hi all,

i am not all that new to .Net but i am still having the occasional plimp. Shared functions, i am confused to when i should declare a function as shared.

Now in take this instance for example. I have a real estate section wich is linked to resorts. Now when i add a new property, i also add a link to the resortID so i placed a drop down list within the page that i use to add the other property details.

to populate the drop down list i have a function in my resorts class which is getAllResorts(), now i know that i dont need all of the functionality of the resorts class, thus i shoudnt make an instance of it if i indeed i dont need it all, right?? so in this instance i have made getAllResorts() a shared function so i can call it without having to create an instance of the resorts class.

Is this right?? please tell me even if i have answered my own question.

Thanks,

Rob
 
I'm guessing you're using VB.NET? In this forum, there are people using other .NET languages, so you have to specify what you're using.

In VB.NET, the "shared" keyword indicates that a method or member of a class is not tied to any particular instance. Whereas to call an instance-member, you have to prefix it with an instance variable:
Code:
Public Class Person
  Public Name As String
End Class

Dim MyPerson As New Person
MyPerson.Name = "chiph"
Accessing the Name member without first creating a new instance of a Person object would result in the "you have to have an instance first" error message.

If the Name member had been shared, then there would have been no need to create a new instance with the New operator -- you would just access it with the class name (since it applies to all instances of the class):
Code:
Public Class Person
  Public Name As String
End Class

Person.Name = "chiph"

Note that this is a contrived example, and I'm a C# guy, not a VB.NET guy, so my syntax is probably all wrong.

A common use of shared methods is to hide the constructor of a class (like when using the Singleton design pattern). In this case, you don't want people creating an instance themselves - you want them going through a method so that only one instance is ever made:
Code:
Class Camera
  Public Shared C As Camera
  Public Shared bCreated As Boolean

  ' Hide constructor
  Private Sub New()
  End Sub

  ' Public shared method to get a Camera
  Public Shared Function GetCamera() As Camera
    If Not bCreated Then
      C = New Camera()
      bCreated = True
      Return C
    Else
      ' We already have created a Camera object
      ' so just give them a copy of it
      Return C
    End If
  End Function
End Class

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
It's also handy for utilities that don't require an instance of an object.

Ever wonder why you don't have to instantiate a Convert class to use Convert.ToInt32( "78" )? It's because the function is "Shared", or, as Chip pointed out, exposed such that you don't need an instance of the object to call the function.
 
Hi there,

yes i am using VB.Net, i apologise about that. So in my example am i right to make my function shared?? sorry again, i am trying to read up on this and it just doesnt seem to want to go in.

Rob
 
I think your proposal makes sense. I'd do it or something similar.
 
One other thing that I just thought of --

If you declare everything in your class to be Shared, what you've done is duplicated the functionality of a code module (are they still called .bas files in VB.NET??).

All methods would be callable at any time, and you'd never have to use the "New" operator to create an instance.

Would it be a Good Thing to do this? Probably not. With O-O design methodologies, this should be avoided, as you'd be working "outside the paradigm", that is, you'd be working at cross-purposes with the concept of object-oriented-ness.

As BoulderBum points out, there are times where doing this is useful, like in the Convert & XmlConvert classes. But if you'll notice, there aren't that many classes in the framework designed like this.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top