Static Members

M

Mark

Hello Friends
Please check the code below.
One in C# and other in VB .Net
In C# I am not able to access a static property by an
instance variable, but in VB I can do it easily.

The Error is

Static member 'ClassInCS.ABC.MyInt' cannot be accessed
with an instance reference; qualify it with a type name
instead

Can anybody explain why there is such a difference between
to language?
Is this advantage to VB??
Or it doesn't fit in rule of OOP?
Regards
Mark.

C# Code

class MyClass{
[STAThread]
static void Main(string[] args){
Console.WriteLine(ABC.MyInt);
ABC a;
Console.WriteLine(a.MyInt); //ERROR
a = new ABC();
Console.WriteLine(a.MyInt); //ERROR
}
}
class ABC{
private static int m_MyInt = 1;
public static int MyInt{
get{
return m_MyInt;
}
set{
m_MyInt = value;
}
}
}

VB Code

Sub Main()
Console.WriteLine(XYZ.MyInt)
Dim x As XYZ
Console.WriteLine(x.MyInt)
x = New XYZ()
Console.WriteLine(x.MyInt)
End Sub
Class XYZ
Public Const MyPConst As Integer = 2
Private Shared m_MyInt As Int32 = 1
Public Shared Property MyInt() As Int32
Get
Return m_MyInt
End Get
Set(ByVal Value As Int32)
m_MyInt = Value
End Set
End Property
End Class
 
M

Morten Wennevik

Probably to make it less confusing. A static variable doesn't belong to
any instance, but rather to the class.
I think VB.Net does some things differently because it needs to be like VB
whereas C# was designed for .Net Framework?
 
J

Jon Skeet [C# MVP]

Mark said:
Please check the code below.
One in C# and other in VB .Net
In C# I am not able to access a static property by an
instance variable, but in VB I can do it easily.

Why do you think you *should* be able to access a static property via
an instance variable?

It makes it look like the property is an instance property, so would
possibly confuse the reader. This is one problem with Java, IMO - and
it's led a lot of people to write silly code like:

Thread.currentThread().sleep(5000);

when they really mean

Thread.sleep(5000);

- but the first version makes it look like you could call sleep() on a
particular thread to send it to sleep.

Basically I view this as a flaw in VB.NET, not in C#.
 
H

Herfried K. Wagner [MVP]

* "Mark said:
In C# I am not able to access a static property by an
instance variable, but in VB I can do it easily.

Yes -- a "limitation" in C#. It's intended to aviod programming errors
and make it easier to distinct between a call to a shared method and a
call to an instance method.
 
H

Herfried K. Wagner [MVP]

* Morten Wennevik said:
Probably to make it less confusing. A static variable doesn't belong
to any instance, but rather to the class.

In VB.NET, a static variable belongs to /all/ instances ('Shared').
That's semantically a difference.
 
O

One Handed Man [ OHM# ]

Morton, what you have written is fundementally wrong.

VB.NET and C# are *BOTH* written for the .NET Framework.

The problem here is that Static members in C# are prevented from being
called through an instance of the class they are members of. This *ought*
to be the case for VB.NET as well in my opinion.

What John Skeet said was absolutely correct. In VB.NET you can do this

Dim a As Char = '1'
a.IsDigit(a)
or
Char.IsDigit(a)


In C# you 'Must' do this
char.IsDigit( . . . )

Making it far more readable IMHO.

Regards - OHM
 
O

One Handed Man [ OHM# ]

The issue here is that Static members in C# are prevented from being
called through an instance of the class they are members of. This ought
to be the case for VB.NET as well in my opinion.

What John Skeet said was absolutely correct. In VB.NET you can do this

Dim a As Char = '1'
a.IsDigit(a)
or
Char.IsDigit(a)


In C# you 'Must' do this
char.IsDigit( . . . )

Making it far more readable IMHO.

Regards - OHM
 
H

Herfried K. Wagner [MVP]

* "One Handed Man said:
I would check the MSIL if I were you.

MSIL doesn't matter in this case. It's the semantic of 'Shared' which
tells the programmer that the member is shared between all instances.
 
M

Morten Wennevik

I stand corrected, VB.NET is of course written for .Net Framework

My point is that VB.NET is a conversion of VB to make it "for .Net
Framework" (Like J#, which could be called "Java.Net")

C# however has no non framework counterpart and where VB.NET can't stray
too far from the original VB the designers of C# were free to make a
programming language tailored for .Net framework, while also getting rid
of many uncertainties found in C/C++ VB and Java.
 
O

One Handed Man [ OHM# ]

What I am driving at is that the net effect is the same. The method of
getting at the (Static/Shared) member may be vary but there is only ONE
copy.

'Shared' is the right word when accessing the member through more than one
instance. However, the member is still 'Static' and can be accessed through
the Class definition because thats where it lives.

VB.NET extends the C# and C++ exposure by allowing ( wrongly ) in my humble
opinion, the exposure through an instance. I make it a point never to access
a shared member through an instance because, someone may revoke the current
flexibility at some later date, and I dont want to retro-change my code.

Regards - OHM
 
O

One Handed Man [ OHM# ]

My point is that VB.NET is a conversion of VB to make it "for .Net
Framework" (Like J#, which could be called "Java.Net")
J# was never intended to be a serious tool for development in .NET in my
opinion. It was simply a way to leverage brand J into NET world after all
that nasty business with Sun. I also think it would be more correct to say
that VB.NET was re-written rather than converted.
C# however has no non framework counterpart and where VB.NET can't
stray too far from the original VB the designers of C# were free to
make a programming language tailored for .Net framework, while also
getting rid of many uncertainties found in C/C++ VB and Java.
C# does not derive from a single not net base. I think if you look at it
it looks like some of the best of Java and C++
 
C

Cor

Hi OHM,

I respectfully disagree.

From the European languages has the English language something strange.

While languages as German, French and Spanish are very strict and other
languages by instance Dutch have/are adopting in the language not existing
words from all kind of languages (Chinese, French, Italian, Portuguese,
Jadish etc) is English totally different.

It is (as far as I know) a mixture between the old French language, original
Gallic and the old North Sea language.

Therefore, English has many words, which are almost the same or often the
same that even does not look at each other.

In my opinion, that does not make the English language incorrect or other
languages better.

Just my thought.

Cor
 
A

Armin Zingler

Morten Wennevik said:
I stand corrected, VB.NET is of course written for .Net Framework

My point is that VB.NET is a conversion of VB to make it "for .Net
Framework" (Like J#, which could be called "Java.Net")

Ask most VB6 programmers. They ask why VB.NET is so totally different from
VB6.

VB.NET is a completely new language. There are only very few things that
have been built-in for compatibility reasons - and some of these _can_ also
be useful, like Modules.

Concerning static members accessible by references: I'd suggest an option to
turn this off because it leads to confusion.

Ex:
pictuerbox1.image.fromfile(...)
question: "Why does the image not change?"

Or:
system.text.encoding.ascii.asciiencoding.getstring(...)
 
A

Armin Zingler

Jon Skeet said:
Why do you think you *should* be able to access a static property via
an instance variable?

I don't think Mark wants the member to be accessible via a variable. He just
wanted to know why there is a difference. IMHO.
 
A

Armin Zingler

One Handed Man said:
The issue here is that Static members in C# are prevented from
being called through an instance of the class they are members of.
This ought to be the case for VB.NET as well in my opinion.


ACK.
 
J

Jon Skeet [C# MVP]

Cor said:
I respectfully disagree.

From the European languages has the English language something strange.

While languages as German, French and Spanish are very strict and other
languages by instance Dutch have/are adopting in the language not existing
words from all kind of languages (Chinese, French, Italian, Portuguese,
Jadish etc) is English totally different.

It is (as far as I know) a mixture between the old French language, original
Gallic and the old North Sea language.

Therefore, English has many words, which are almost the same or often the
same that even does not look at each other.

In my opinion, that does not make the English language incorrect or other
languages better.

I don't see what relevance your analogy has. VB.NET allows you to
(perhaps unwittingly) write confusing code in a way that C# doesn't. In
C# you get told if you're trying to access something as if it were an
instance member - what's the downside of that? The only *possible*
benefit of the VB.NET way as far as I can see is that you can use

a.Method()
instead of
SomeVeryLongIndeedAndAnnoyingToReadTypeName.Method()

If you really want to avoid that, you can alias the type name -
although I'd be very wary about doing that.

So, as far as I can see there's a downside to the VB.NET way of doing
things, but no upside. In what way does that *not* make C# better in
this respect? (Presumably the reasoning for VB.NET working that way is
for compatibility reasons, but if so there should at least be an option
to turn it off, just like option strict.)
 
S

Steve

I also agree with Jon. This is just one of many reasons why it is hard
to communicate with management that c# is better, and that you shouldn't
start new projects with vb.net just because you're "comfortable" with
it. And you know that if you try to explain this they simply won't
understand.
 
C

Cor

Hi Jon,

As often discussed C# and VB.net have both advantages but not in the code
itself.

The advantage VB.net that it is somehow like a natural language

The advantage of C# that it is more strict.

Both are good idea's but they conflict with each other.

That does not make in my opinion things absolute more better or worse.
It is just the point of view what you find important decide that.

And because I am writing this from the VB.language group I did make the
comparising with a natural language like English.


Cor
 

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