Question Reference to a non-shared member requires reference?

j7linz

New member
Joined
Feb 3, 2014
Messages
1
Programming Experience
3-5
Can somebody help me with this super simple VB question?

<link removed by moderator>
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
If you would like to ask a question then please ask a question. Don't post a link to a competitor site where you have asked the question.

With regards to that error message, it means that you're trying to access a member of an object without actually having the object. Consider the fact that programming objects are modelled on real-world objects. In the real world, if you want to drive a car then you actually have to have a car to drive, right? The same goes in OOP. If you have a Car class and it has a Drive method, you can't just do this:
Car.Drive()
because the Car class is just a description of what a Car object is and does. To call Drive on a Car object, you must create a Car object, e.g.
Dim myCar As New Car

myCar.Drive()
That's an example of what you're doing wrong. If you want information more specific to your particular case then provide us with information about your particular case.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
By the way, if you know that the question is super simple then why do you need help with it? It always amuses me that people feel the need to tell us that the thing that they can't do is super simple. I think that it's up to us to decide that for ourselves. :)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,545
Location
Norway
Programming Experience
10+
Or you could also declare Drive as Shared, in which case you will be able to access it from the default instance.
Shared members are for functionality that is not tied to an instance of the class, you use the type name to qualify the instanceless shared member.
Default instance is something else, application framework for Windows Forms projects provide this, where by using the form type name it returns either a new instance or a previously generated one.
It could seem like the same, since in both cases you use the type name to qualify, but they are exactly opposite since one means "no instance" and the other means "one instance".
 
Top Bottom