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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a PUBLIC SHARED Function. 1

Status
Not open for further replies.

blondebier

Programmer
Jun 19, 2003
142
GB
I am struggling with the concept of a Public Shared Function as opposed to a Public Function. Can someone please explain?

In my problem, my function needs to be PUBLIC SHARED as a third party application needs to reference it.

Problem Scenario:

I need to create a HashTable with data from a database. The hastable must only be loaded on the first function call. It will store a CODE as the key, and then a string separated by a comma for two integers e.g. (1,2) as the value. (This is so that the database isn't used to get every code)

I have this VB.NET class:

Public Class Utilities
Private CodesTable As New Hashtable

Private ReadOnly Property HashValue(ByVal Version As String, ByVal Code As String) As String
Get
Try
If CodesTable.Keys.Count = 0 Then
' Build the hashtable from the database
CodesTable.Add("Test", "3,2") ' - this is just for testing
End If

Return CodesTable(Code)

Catch ex As Exception
' Return an error code
End Try
End Get
End Property

Public Shared Function GetData(ByVal Version As String, ByVal DataString As String, ByVal Code As String) As String
Dim HashValue As String
Dim SeparatorPosition As Integer
Dim StartPosition As Integer
Dim length As Integer

HashValue = HashValue(Version, Code)

' Split the hash value into two parts. Part 1: StartPosition, Part 2: Length
SeparatorPosition = InStr(HashValue, ",")
StartPosition = CInt(HashValue.Substring(0, SeparatorPosition))
length = CInt(HashValue.Substring(SeparatorPosition))

Return DataString.Substring(StartPosition - 1, length)
End Function
End Class

Here is an example of a call that i'd like:

GetData("V1", "ABCDEFG", "Test") - should return CD

PROBLEM:

I get the following error message when referencing the HashValue property in the PUBLIC SHARED GetData FUNCTION.

"Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."

If the function is just PUBLIC without the SHARED keyword the error disappears but I cannot reference the function in my third party application.

I'd appreciate any advice.

Cheers.
 
Your function doesnt need to be shared, as I presume your 3rd party app will create an instance of your dll class.

All that SHARED does is allow you to access the function without instantiating a class object
eg utilities.getdata(.....)

If however you create an object and instantiate your class there is no need for the SHARED keyword, so long as your Subs are public.
Code:
dim myclass as new Utilities
myclass.getdata(.............)







Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
Shared means that all instances use the same method/object.

Here's a sample class:
Code:
public class Item
  private [b]Shared[/b] m_Count as integer = 0

  public readonly property Count as integer
    get
      return m_Count
    end get
  end property

  public sub New()
    m_Count += 1
  end sub
end class

Now, as you instantiate more and more of these objects, you can look at the .Count property on any of them and it will give you the total number of items.

You can also use Shared methods with having an instance of an object, ie:
Code:
Public Class MyFunctions
  public shared function DoThing() as string
    return "A Thing!"
  end function
end class

Now you can call MyFunctions.DoThing with out having to declare an object.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks for the replies.

Why does it have a problem with me referencing the property HashValue in the Public Shared GetData Function though?
 
Shared methods can only access shared objects.

If an variable/method is NOT shared, it must be referenced by an instantiation of the class it is a part of.

If a variable/method IS shared, it can be accessed with out an instantiation.

Since a shared variable/method doesn't need an instantiation, it can't references variables/methods that do need an instantiation because it can not be sure there will be an instantiation to use.

If you need to use a shared function, make sure all of the properties/variables/methods/etc it references are also shared.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
or use a module.

E&F p-)

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
sorry that should have been

[tongue] [tongue] [tongue] [tongue] [tongue] [tongue]

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top