Solved

Reference to a non-shared member requires an object reference

Posted on 2010-11-08
9
353 Views
Last Modified: 2012-05-10


I'm trying to call a function in a class from a sub (page_load) on a page (See attached code).  I'm new to ASP.net but it seems to me that calling a function from a class would be a very common task.  But I cannot seem to figure it out and I have not been able to find a resolution in my searches.  

I have multiple functions in classes that I call that appear to work, but they all seem to throw this error in the function in the class.  However the exception is cleared and the correct value is returned when control returns back to the calling sub.  I would not have discovered this if I had not put a try... catch block in a function.  

I have tried every permutation of declaring the class as a new instance that has been suggested.  They all produce the error.  The error appears to be in the function GetCurrentUser1 not in the calling proc.  It is cleared after on the calling proc when the function returns control.  It does not seem to affect processing, "MyName" is returned from the function.  

 I must be doing this wrong.  Does anybody call a function from a class and have working code.  

This is the calling Sub

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

       Dim clsCommon1 As New clsCommon1
       strCurrentUser1 = clsCommon1.GetCurrentUser1()

   End Sub
-------
This is the Class

Function in clsCommon1 

   Public Shared Function GetCurrentUser1() As String

      Dim strCurrentUser1 As String
      strCurrentUser1 = "MyName"
      Return strCurrentUser1

   End Function

Open in new window

0
Comment
Question by:deepPowdah
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34086913
It's a Shared method, so you don't need an instance. Just drop the "Dim" line and you should be set:

0
 

Author Comment

by:deepPowdah
ID: 34086953
I deleted the Dim instance and I still get the same error.  As I mentioned in my post, the exception is in the function not in the calling sub.
0
 
LVL 3

Expert Comment

by:mortimer452
ID: 34087596
Do you have any other functions in clsCommon1?  One of them may be referencing a non-shared function.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:deepPowdah
ID: 34087635
I get this error in that website.  I created a test website with one page and one class.  I created a simple function and call in on the Page_Load event.  I get the same error message in that web site.  
0
 
LVL 3

Expert Comment

by:mortimer452
ID: 34087702
I don't see anything wrong with what you're doing as long as it really is that simple, providing you take out the Dim clsCommon1 as clsCommon1:




Public Class Common1
  Public Shared Function GetCurrentUser1()
    Dim strCurrentUser1 as string
    strCurrentUser1 = "MyName"
    Return strCurrentUser1
  End Function
End Class


Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

  strCurrentUser1 = clsCommon1.GetCurrentUser1()

End Sub

Open in new window

0
 

Author Comment

by:deepPowdah
ID: 34088108
I have many functions in the class that I have been using for six months.  I discovered this error when I added a try... catch into one of the functions.  But all the functions I tested generate this error while running the function, but the error is cleared when control is returned to the calling proc.  

I wonder if this is not a problem with ASP.net 3.5.  Have you ever put a breakpoint in a called function to see if this exception is thrown?
0
 
LVL 3

Accepted Solution

by:
mortimer452 earned 500 total points
ID: 34088266
Yes, I have -- never seen it before, not using the exact code above

The error you are receiving is caused by one thing, that is trying to call a non-shared function without creating a new instance of the class first.  For example:

Public class MyClass
  Public function MyFunction() as String
    Return "AStringValue"
  End Function
End Class

Public sub MySub()
  dim str as String
  str = MyClass.MyFunction()
End Sub

The above code would generate the error you're getting, because you did not create an instance of the MyClass first, and tried to access a non-shared member

Check all the other functions within your clsCommon1, make sure you have the "Shared" declaration in front of all of them, I bet you missed one.
0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34276667
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question