Solved

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

Posted on 2016-09-07
5
25 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 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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