Reference to a non-shared member requires an object reference



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

deepPowdahAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
It's a Shared method, so you don't need an instance. Just drop the "Dim" line and you should be set:

0
deepPowdahAuthor Commented:
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
mortimer452Commented:
Do you have any other functions in clsCommon1?  One of them may be referencing a non-shared function.
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

deepPowdahAuthor Commented:
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
mortimer452Commented:
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
deepPowdahAuthor Commented:
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
mortimer452Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kumaraswamy RCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.