Solved

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

Posted on 2016-09-07
5
42 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
[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
  • 3
  • 2
5 Comments
 
LVL 63

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 63

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 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