Solved

VB.Net error Cannot refer to an instance member of a class from within a shared method...

Posted on 2016-09-07
5
20 Views
Last Modified: 2016-09-14
Hi

I'm trying to convert a Windows Form VB.Net app to a console application. What I have done is create a new public class, banged all the subroutines I had in the 2 forms I had in the original app in that new class as Public Sub, and rewrote the form Load event routines into normal subs, also placed in the new class. I replaced/got rid of any reference to form controls, and used console.writeline for any output. Then I changed the app type to console, and got rid of the now empty forms.

I then added a Shared Main subroutine in the new class to start the app.

I still have juts 3 errors remaining, of the type in the title, each on a call to one of the manuy Subs in the class, and I can't understand why, because my Main sub is calling a number of Public Subs, but I get the error only on 3 of them, and these Subs look to me to be exactly like all the other Subs that are called, so why would these 3 calls generate an error, and not all the other calls I have ? 2 of the 3 are calls to a sub without parameters, 1 has a string parameter.

What am I missing ? Where should I look for finding out why the calls to these 3 subroutines cause a compile error ?

Thanks for help, hints, tips !
Bernard
0
Comment
Question by:bthouin
  • 3
  • 2
5 Comments
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 41788042
Please have a look at this Microsoft documentation, "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class". If that does not solve your issue please post the three Sub's and the class you are calling them from here.
0
 
LVL 1

Author Comment

by:bthouin
ID: 41789269
My sub main must be a shared sub, otherwise nothing runs. When it calls one my public subs, these in turn refer to public variables which are defined in a public module which is of course outside of the class. I have NO public variables defined in the class. All the public variables are defined in that single public module.

But I'm basically completely stuck, and I just don't understand how a console app can be built using a class. Wherever I put my public variables or weherever I call my public subs, I always end up with this error !

Curently I have:

Public Class ClassMain

    Public Shared Sub Main()
        Call StartUp()
    End Sub

    Public Sub StartUp()
...
      Call A
...
     Call B
...
   End Sub

  Public Sub A
...
   x= 0
...
   Call C
...
  End Sub

 Public Sub B
...
 End Sub

 Public Sub C
...
 End Sub


End Class

Public Module publicvars

  Public x as Integer

End Module

and the error is on the Call Startup in the shared Sub Main()


Where is the problem ?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41789309
Please post the actual code so we can determine why you are having this issue. Also use the CODE icon on the tool bar of the text box by pasteing your code, highlighting the code, and then clicking on the CODE icon.
0
 
LVL 1

Accepted Solution

by:
bthouin earned 0 total points
ID: 41789403
I've solved my problem: I moved all the subroutines to my public module, and it works like a breeze...

Fernando, thanks for your time :)
0
 
LVL 1

Author Closing Comment

by:bthouin
ID: 41797514
Because I was the one finding the solution...
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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

16 Experts available now in Live!

Get 1:1 Help Now