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.
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
Public Class Utilities
Private CodesTable As New Hashtable
Private ReadOnly Property HashValue(ByVal Version As String, ByVal Code As String) As String
If CodesTable.Keys.Count = 0 Then
' Build the hashtable from the database
CodesTable.Add("Test", "3,2") ' - this is just for testing
Catch ex As Exception
' Return an error code
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)
Here is an example of a call that i'd like:
GetData("V1", "ABCDEFG", "Test") - should return CD
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.