Solved

Reference to a non-shared member requires an object reference

Posted on 2010-11-08
9
343 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now