Solved

Reference to a non-shared member requires an object reference

Posted on 2010-11-08
9
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 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