Instance or no instance...

S

Scott M.

I know that if a class has shared members, I don't have to have an instance
of the class to use the member(s) (i.e. messagebox.show). But, does this
mean that a class that does not require an instance to use it MUST be using
shared members.

For example, why am I allowed to do this without an actual instace of the
class:

Dim x as Textbox
x.text = "test"

This does not create an instance of a textbox and the text property is not
shared, yet the code still is valid.
 
J

Jon Skeet [C# MVP]

Scott M. said:
I know that if a class has shared members, I don't have to have an instance
of the class to use the member(s) (i.e. messagebox.show). But, does this
mean that a class that does not require an instance to use it MUST be using
shared members.

For example, why am I allowed to do this without an actual instace of the
class:

Dim x as Textbox
x.text = "test"

This does not create an instance of a textbox and the text property is not
shared, yet the code still is valid.

It's not at runtime though, as far as I can see - it generates a
NullReferenceException when I try to run it.

(C# is nice and picky about this, not letting you use a local variable
until you've explicitly given it a value.)
 
T

Tian Min Huang

Hello Scott,

Jon's reply is correct. In addition, I'd like to share the following
information with you:

As you know, data types in .NET are classified into Reference Type and
Value Type. Reference objects require instantiation with the New keyword,
while value types don't.

A VB.NET Primer--Getting to Know Objects All Over Again
http://whidbey.msdn.microsoft.com/library/default.asp?url=/library/en-us/dni
nvb01/html/invb1200.asp

Value Types and Reference Types
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/
vbconValueRefTypes.asp

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

Dolly

To be able to access a member of the class without
instantiating it, the member has to be declared static
[what you call as shared ie]

So you have to say
TextBox tb = new TextBox()

to access tb.text

Regards,
Dolly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top