Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

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

11 Experts available now in Live!

Get 1:1 Help Now