473,378 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C# is too nitpicky?


Why is it that you can do this in VB.net but not (the syntactical
equivilent of it in) C#?:

Dim m as MessageBox
m.Show("hello")
Nov 20 '05 #1
17 977
In article <qc********************************@4ax.com>, VeebeeGeeBees wrote:

Why is it that you can do this in VB.net but not (the syntactical
equivilent of it in) C#?:

Dim m as MessageBox
m.Show("hello")


Because VB.NET is too lax... MessageBox.Show is a shared member (static
in C#). That means it is not associated with any particular instance of
a class, but with the class it's self. The effect is that it is
unnecessary to create an instance (use new) to invoke a static member.
In C#, they correctly make this distinction by not allowing you to call
static members through instances of the object. I think this is good,
because it makes the code more explicit. The VB.NET way is sloppy in my
honest oppinion and can make the code harder to read since you can never
be sure if the method invoked is shared or not.

--
Tom Shelton [MVP]
Nov 20 '05 #2
On Wed, 14 Apr 2004 13:05:03 -0700, Tom Shelton <to*@mtogden.com>
wrote:
In article <qc********************************@4ax.com>, VeebeeGeeBees wrote:

Why is it that you can do this in VB.net but not (the syntactical
equivilent of it in) C#?:

Dim m as MessageBox
m.Show("hello")


Because VB.NET is too lax... MessageBox.Show is a shared member (static
in C#). That means it is not associated with any particular instance of
a class, but with the class it's self. The effect is that it is
unnecessary to create an instance (use new) to invoke a static member.
In C#, they correctly make this distinction by not allowing you to call
static members through instances of the object. I think this is good,
because it makes the code more explicit. The VB.NET way is sloppy in my
honest oppinion and can make the code harder to read since you can never
be sure if the method invoked is shared or not.


Well its not a good thing when all of the Dot Net documentation leads
you to believe that Shared in VB.Net is the same as static in C#, when
in reality they behave differently. Being able to instantiate a new
var of type MessageBox is more in like with "purist" OOP. C# seems to
want to carry forward the poorly implemented OOP features of C++.

Nov 20 '05 #3
Tom, I'm pretty new but how is that different then say
dim x as string
x.toUpper

are you saying you can't do that in C#?

can't we tell that it's a shared class because of the absence of the New
constructor?

You're right that it's sometimes hard to read WRT reading if the method was
shared or not.


"Tom Shelton" <to*@mtogden.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
In article <qc********************************@4ax.com>, VeebeeGeeBees

wrote:

Why is it that you can do this in VB.net but not (the syntactical
equivilent of it in) C#?:

Dim m as MessageBox
m.Show("hello")


Because VB.NET is too lax... MessageBox.Show is a shared member (static
in C#). That means it is not associated with any particular instance of
a class, but with the class it's self. The effect is that it is
unnecessary to create an instance (use new) to invoke a static member.
In C#, they correctly make this distinction by not allowing you to call
static members through instances of the object. I think this is good,
because it makes the code more explicit. The VB.NET way is sloppy in my
honest oppinion and can make the code harder to read since you can never
be sure if the method invoked is shared or not.

--
Tom Shelton [MVP]

Nov 20 '05 #4
It leads you to believe this, because it is true - they are the same.

C# imposes a restriction, where you cannot use an instance to have access to
a static member. VB has no such restriction and allows you to do it either
way.

"VeebeeGeeBees" <vb**@vbgb.com> wrote in message
news:r2********************************@4ax.com...
On Wed, 14 Apr 2004 13:05:03 -0700, Tom Shelton <to*@mtogden.com>
wrote:
In article <qc********************************@4ax.com>, VeebeeGeeBees wrote:

Why is it that you can do this in VB.net but not (the syntactical
equivilent of it in) C#?:

Dim m as MessageBox
m.Show("hello")


Because VB.NET is too lax... MessageBox.Show is a shared member (static
in C#). That means it is not associated with any particular instance of
a class, but with the class it's self. The effect is that it is
unnecessary to create an instance (use new) to invoke a static member.
In C#, they correctly make this distinction by not allowing you to call
static members through instances of the object. I think this is good,
because it makes the code more explicit. The VB.NET way is sloppy in my
honest oppinion and can make the code harder to read since you can never
be sure if the method invoked is shared or not.


Well its not a good thing when all of the Dot Net documentation leads
you to believe that Shared in VB.Net is the same as static in C#, when
in reality they behave differently. Being able to instantiate a new
var of type MessageBox is more in like with "purist" OOP. C# seems to
want to carry forward the poorly implemented OOP features of C++.

Nov 20 '05 #5
On Wed, 14 Apr 2004 16:26:11 -0400, "Marina" <so*****@nospam.com>
wrote:
It leads you to believe this, because it is true - they are the same.

C# imposes a restriction, where you cannot use an instance to have access to
a static member. VB has no such restriction and allows you to do it either
way.

How can you say they are the same then say they are different,
separated only by a line break?
Nov 20 '05 #6
In article <ee**************@TK2MSFTNGP12.phx.gbl>, Eric Sabine wrote:
Tom, I'm pretty new but how is that different then say
dim x as string
x.toUpper

are you saying you can't do that in C#?
ToUpper is not a static method. It is an instance method. So you have
to create an instance of a string.

string s = "hello, world";
Console.WriteLine(s.ToUpper());

can't we tell that it's a shared class because of the absence of the New
constructor?


When looking at a line of code, it is not always convienient to go look
at the declaration.

--
Tom Shelton [MVP]
Nov 20 '05 #7
In article <r2********************************@4ax.com>, VeebeeGeeBees wrote:
On Wed, 14 Apr 2004 13:05:03 -0700, Tom Shelton <to*@mtogden.com>
wrote:
In article <qc********************************@4ax.com>, VeebeeGeeBees wrote:

Why is it that you can do this in VB.net but not (the syntactical
equivilent of it in) C#?:

Dim m as MessageBox
m.Show("hello")


Because VB.NET is too lax... MessageBox.Show is a shared member (static
in C#). That means it is not associated with any particular instance of
a class, but with the class it's self. The effect is that it is
unnecessary to create an instance (use new) to invoke a static member.
In C#, they correctly make this distinction by not allowing you to call
static members through instances of the object. I think this is good,
because it makes the code more explicit. The VB.NET way is sloppy in my
honest oppinion and can make the code harder to read since you can never
be sure if the method invoked is shared or not.


Well its not a good thing when all of the Dot Net documentation leads
you to believe that Shared in VB.Net is the same as static in C#, when
in reality they behave differently. Being able to instantiate a new
var of type MessageBox is more in like with "purist" OOP. C# seems to
want to carry forward the poorly implemented OOP features of C++.


Shared and static are essenitally the same thing. It's just that the
VB.NET compiler does not enforce the difference's properly, IMHO. Of
course that is by design - but a bad one. The point is that a static
member is not associated with any single instance of a class. So, it
makes little sense to allow the calling of these methods through and
instance. In this instance, C# get's it right. YMMV.

--
Tom Shelton [MVP]
Nov 20 '05 #8
The meaning of the keyword is the same.

It is only a matter of how the language let's you use it.

If you write a static method in C#, and try to use it from C#, it wil force
you to use the type name. If you use the same method from VB (original
method still written in C#), you can do it either way.

If you then rewrite the method as a shared method in VB, and try to use it
from VB, VB will let you do it either way. Try using that VB shared method
from C# - and you have the restriction again.

So in the end, no matter if you write it in C# and use 'static', or in VB
and use 'Shared', the meaning is the same. C# will always impose the
restriction no matter which language it was written in - and VB will not.
That is the whole point.

"VeebeeGeeBees" <vb**@vbgb.com> wrote in message
news:vd********************************@4ax.com...
On Wed, 14 Apr 2004 16:26:11 -0400, "Marina" <so*****@nospam.com>
wrote:
It leads you to believe this, because it is true - they are the same.

C# imposes a restriction, where you cannot use an instance to have access toa static member. VB has no such restriction and allows you to do it eitherway.

How can you say they are the same then say they are different,
separated only by a line break?

Nov 20 '05 #9
In article <vd********************************@4ax.com>, VeebeeGeeBees wrote:
On Wed, 14 Apr 2004 16:26:11 -0400, "Marina" <so*****@nospam.com>
wrote:
It leads you to believe this, because it is true - they are the same.

C# imposes a restriction, where you cannot use an instance to have access to
a static member. VB has no such restriction and allows you to do it either
way.

How can you say they are the same then say they are different,
separated only by a line break?


They are the same at the class level - it's just that the VB.NET
compiler does not enforce the distinction between shared and instance
members. Which is bad, IMHO.

--
Tom Shelton [MVP]
Nov 20 '05 #10
On Wed, 14 Apr 2004 13:39:11 -0700, Tom Shelton <to*@mtogden.com>
wrote:
In article <vd********************************@4ax.com>, VeebeeGeeBees wrote:
On Wed, 14 Apr 2004 16:26:11 -0400, "Marina" <so*****@nospam.com>
wrote:
It leads you to believe this, because it is true - they are the same.

C# imposes a restriction, where you cannot use an instance to have access to
a static member. VB has no such restriction and allows you to do it either
way.

How can you say they are the same then say they are different,
separated only by a line break?


They are the same at the class level - it's just that the VB.NET
compiler does not enforce the distinction between shared and instance
members. Which is bad, IMHO.


Actually I just realized whats going on is not what you guys are
saying.

Try this:

Dim x as MessageBox
Dim y as MessageBox

' Note you cannot instantiate a new
' one due to private constructor

x.Show("test")
y.Show(Cbool(x is y))
x and y refer to same instance ("true" is displayed on the second
line).
Without the first line you cannot compare x to y because they are not
yet set to the "global" static messagebox.

in VB.net, it appears that Shared gives you THE OPTION of calling a
method without instantiating an instance... but you can do so if you
want.

in C#, static means you MUST call the method without declaring an
instance.

Big difference.

My guess is that this is a language design flaw.. not really a flaw in
C# or VB.Net, but a flaw in the sense that the design team strived to
maintain the behavior people were used to in earlier versions of those
languages (VB6 and C++), thus the difference in behavior.

If you need proof, create a new vb class with a simple shared sub, try
instantiating an x and y variable. You can do so, and the test to see
if x = y above will return false because they are not the same, like
the case of the global MessageBox class.

My question now is why is the MessageBox class "system global" so to
speak, whereas the class created in VB.net is not?

Nov 20 '05 #11
* VeebeeGeeBees <vb**@vbgb.com> scripsit:
My question now is why is the MessageBox class "system global" so to
speak, whereas the class created in VB.net is not?


What do you mean by "system global"?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #12
* Tom Shelton <to*@mtogden.com> scripsit:
The point is that a static member is not associated with any
single instance of a class.


It's shared between /all/ instances, so callong these methods through an
instance makes sense (IMO).

Just my 2 cents.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #13
* Tom Shelton <to*@mtogden.com> scripsit:
In article <qc********************************@4ax.com>, VeebeeGeeBees wrote:

Why is it that you can do this in VB.net but not (the syntactical
equivilent of it in) C#?:

Dim m as MessageBox
m.Show("hello")
Because VB.NET is too lax... MessageBox.Show is a shared member (static
in C#). That means it is not associated with any particular instance of
a class, but with the class it's self. The effect is that it is


Basically I agree, but if you have a look at the keyword 'Shared' which
is used for static members. Shared members are semantically /shared/
between /all/ instances of a class. That's why accessing shared members
by using an instance variable IMO makes sense for VB.NET.
because it makes the code more explicit. The VB.NET way is sloppy in my
honest oppinion and can make the code harder to read since you can never
be sure if the method invoked is shared or not.


I agree too but then 'Shared' is IMO not the right term for a keyword
that marks static members :-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #14
In article <em**************@TK2MSFTNGP10.phx.gbl>, Herfried K. Wagner [MVP] wrote:
* Tom Shelton <to*@mtogden.com> scripsit:
The point is that a static member is not associated with any
single instance of a class.


It's shared between /all/ instances, so callong these methods through an
instance makes sense (IMO).

Just my 2 cents.


Well, I guess this is a topic that everyone is going to have their
personall preference about... I find the VB way to be unintuitive and
wrong.
--
Tom Shelton [MVP]
Nov 20 '05 #15
In article <rh********************************@4ax.com>, VeebeeGeeBees wrote:
On Wed, 14 Apr 2004 13:39:11 -0700, Tom Shelton <to*@mtogden.com>
wrote:
In article <vd********************************@4ax.com>, VeebeeGeeBees wrote:
On Wed, 14 Apr 2004 16:26:11 -0400, "Marina" <so*****@nospam.com>
wrote:

It leads you to believe this, because it is true - they are the same.

C# imposes a restriction, where you cannot use an instance to have access to
a static member. VB has no such restriction and allows you to do it either
way.

How can you say they are the same then say they are different,
separated only by a line break?
They are the same at the class level - it's just that the VB.NET
compiler does not enforce the distinction between shared and instance
members. Which is bad, IMHO.


Actually I just realized whats going on is not what you guys are
saying.


Yes it is...
Try this:

Dim x as MessageBox
Dim y as MessageBox

' Note you cannot instantiate a new
' one due to private constructor

Yep... You can not create an instance of messagebox at all.
x.Show("test")
y.Show(Cbool(x is y))
x and y refer to same instance ("true" is displayed on the second
line).
Without the first line you cannot compare x to y because they are not
yet set to the "global" static messagebox.

There is no global static message box. There is no instance of
messagebox. Both x and y will remain nothing.

If x is nothing then
x.show("Nothing")
end if

This is essentially a class like this in C#:

public class MessageBox : .....
{
private MessageBox() {}

public static Windows.Forms.DialogResult Show(....)
{
...
}
}

in VB.net, it appears that Shared gives you THE OPTION of calling a
method without instantiating an instance... but you can do so if you
want.

in C#, static means you MUST call the method without declaring an
instance.

Big difference.

Exactly. But it isn't a difference on the IL level. It is a difference
on the language level.
My guess is that this is a language design flaw.. not really a flaw in
C# or VB.Net, but a flaw in the sense that the design team strived to
maintain the behavior people were used to in earlier versions of those
languages (VB6 and C++), thus the difference in behavior.

As you say, it is a bow to langauge compatability. A bad one, but one
none the less.
If you need proof, create a new vb class with a simple shared sub, try
instantiating an x and y variable. You can do so, and the test to see
if x = y above will return false because they are not the same, like
the case of the global MessageBox class.
The reason that MessageBox return true is because you are comparing two
variables that are both nothing - and nothin is nothing.
My question now is why is the MessageBox class "system global" so to
speak, whereas the class created in VB.net is not?


There is no instance of MessageBox. Only static methods. Make your
constructor private so that you can't create an instance and you will
have the same effect...

public class TestClass
private Sub new()
end sub

public shared sub TestMethod()
Console.WriteLine("Hello")
end sub
end class

public sub main()
dim a as testclass
dim b as testclass

testclass.testmethod()
a.testmethod
b.testmethod
if a is b then
Console.writeline("a is b")
end if

if a is nothing then
console.writeline("a is nothing")
end if

if b is nothing then
console.writeline("b is nothing")
end if

if typeof a is testclass then
console.writeline("this will never happen")
end if
End sub

--
Tom Shelton [MVP]
Nov 20 '05 #16
My guess is that this is a language design flaw.. not really a flaw in
C# or VB.Net, but a flaw in the sense that the design team strived to
maintain the behavior people were used to in earlier versions of those
languages (VB6 and C++), thus the difference in behavior.


As you say, it is a bow to langauge compatability. A bad one, but one
none the less.


VB6 didn't have Shared members, so I don't see how that can be.
FWIW, Paul Vick disscusses this issue at
http://www.panopticoncentral.net/arc...08/07/172.aspx

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #17
On 2004-04-14, Mattias Sjögren <ma********************@mvps.org> wrote:
My guess is that this is a language design flaw.. not really a flaw in
C# or VB.Net, but a flaw in the sense that the design team strived to
maintain the behavior people were used to in earlier versions of those
languages (VB6 and C++), thus the difference in behavior.

As you say, it is a bow to langauge compatability. A bad one, but one
none the less.


VB6 didn't have Shared members, so I don't see how that can be.


Your correct of course... It's been almost 2 years since I've touched
VB6 seriously.

FWIW, Paul Vick disscusses this issue at
http://www.panopticoncentral.net/arc...08/07/172.aspx


Good article... I'm going to book mark that for future reference.
Thanks.

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
A dream will always triumph over reality, once it is given the chance.
-- Stanislaw Lem
Nov 20 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Christopher Benson-Manica | last post by:
Do you prefer *my_string or my_string? Why? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
17
by: William L. Bahn | last post by:
=========== (N869, p8) An object is a region of data storage in the execution environment, the contents of which can represent values. When explaining this to students, is it reasonable to say...
3
by: SomeGuyHere | last post by:
I'm studying for the Microsoft .NET 2.0 Application development foundation exam. I just finished the 1000 page study guide cover-2-cover doing all examples. My first try I failed the practice test...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.