Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 13th, 2005, 06:45 AM
Authorized User
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Public Shared Functions?

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
                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


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.


Old July 15th, 2005, 02:26 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

I think you are a bit confused about the terminology:

The "Shared" modifier means that you can access a function without a class instance. Any non-shared members have to be called off of a instance of the class.

For this function:
   Public Shared Function GetData(...) As String

You can call it like this:

For this:
   Public Function GetData(...) As String

You have to do this:
   Dim objUtils As New Utilities

The error message is due to the fact that you have a non-shared property in the class that you are trying to call from a shared method. Seeing as the property that you wrote is readonly and private it has no need to be a property at all and should be a private shared method. Change it to the following and all will be well:

  Private Shared Function 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
      End If
      Return CodesTable(Code)
    Catch ex As Exception
      ' Return an error code
    End Try
  End Function

Of course, looking back on your code, I reallize that you also have a private class field (CodesTable). This will have to change as well. Ideally you should move the instance of this variable into the method(s) that use it. You will probably also need to pass the instance from the main method (GetData) to the helper method.

On the other hand, that helper method/property is only getting called once from the GetData method, so it doesn't even need to be in its own method body. Just simplify the whole thing and move that code to the GetData method.

I'd also recommend using a variable prefix standard so your code is more readable. It took me a little bit to figure out if "HashValue" was referring to the variable in the GetData method or the property you defined above.


Similar Threads
Thread Thread Starter Forum Replies Last Post
public article - public articledetails _keysersoze_ BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 September 8th, 2007 08:38 AM
public sub or function ? kscase Visual Basic 2005 Basics 3 May 20th, 2007 03:14 PM
public functions stojkovmarjan ASP.NET 2.0 Basics 1 September 19th, 2006 03:57 PM
Shared / Public Assembly ashu_from_india General .NET 1 March 20th, 2005 07:22 AM
difference b/w shared and public variable lgeetha VS.NET 2002/2003 1 March 4th, 2005 08:24 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.