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
Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get the selected ValueMember of Combobox 5 39
VB.Net - TypeInitializer Error 25 32
Store results in vb.net 3 24
VB.NET 2008 - Windows Protected Your PC 7 41
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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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