Solved

Reference to a non-shared member requires an object reference

Posted on 2010-11-08
9
340 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

10 Experts available now in Live!

Get 1:1 Help Now