VB vs C#

C

cody

However for newer languages which should not have to do with that, can
follow in my opinion the normal languages rules be used as in all languages
which uses Latin characters.


German is case sensitive. The word "Rennen" is a noun, whereas "rennen" is a
verb.
 
C

cody

C# is much more OO than VB.
Can you clarify that? As far as I am aware, both languages support the
full set of CLR object features.

Maybe my knowlegde is not up to date. last time I saw VB it was a hybrid
language like C++,
that allowed normal functions or variables not to belong to a class.
Again, in what way? Use Option Strict, and VB.NET's type checking is
every bit as rigorous as C#'s.

But you can also leave it out or forget it and you have a problem. What if
the implementor of the library you are using leaves it out?
For some things. For others, it's just the reverse. It all comes out
as MSIL in the end. As has been said 500 times on these newsgroups, there
is no significant performance difference between VB.NET and C#.

VB calls its methods with late binding that means you can call methods that
are not in this type (I make the example with C# syntax since I do not know
VB):

sub Foo ByRef param as Object
param.DoIt(); ' although System.Object does
not support DoIt() makes a dynamic lookup
end sub
And VB.NET supports lots of features that C# doesn't have: better
interface implementation support (in VB.NET, any method with any scope and
name can implement one or more interface methods -- this is a HUGE plus for
me, and the primary reason I chose VB.NET over C#),

Can you explain this feature or give an example? How can I implement a
interface when I don't have a method with a suiting signature?
With blocks, etc. There are pros and cons to both languages.

with blocks are stupid and lead to sloppy coding.

int R;
with color1,color2
R = 1; ' which R is used here?
end with

a long time ago when I used pascal I also liked this feature but I also saw
that
it leads to unreadable and ambigous code. now Iam glad that neither C/C++
nor Java or C# have such a feature.
Also, don't put too much emphasis on IDE features, but don't ignore them
either. Under VS.NET 2003, coding VB.NET is much easier and much less
error-prone than C#. Compiler errors are spotted in real time, and the
Intellisense support is much more thorough.

In C#, if you leave a semicolon out the error is instantly highlighened. In
Whidbey
there are new IDE features like templates so you can write "for" and the IDE
builds
you automatically a complete for loop.
 
D

Daniel O'Connell [C# MVP]

Jeremy Todd said:
And VB.NET supports lots of features that C# doesn't have: better
interface implementation support (in VB.NET, any method with any scope and
name can implement one or more interface methods -- this is a HUGE plus
for
me, and the primary reason I chose VB.NET over C#), With blocks, etc.
There
are pros and cons to both languages.
To show a bit of a difference in peoples opinions, the above features are
two of things I really dislike about VB. Enough so that I would prefer not
to use VB or use classes written using the interface mapping.
However, support for exception filters, safer event raising, and a MyClass
are things I'd like to have equivilents of in C# (although exception filters
and MyClass accesses are things I wouldn't use often at all, I do wish they
were there).
 
J

Jeremy Todd

Maybe my knowlegde is not up to date. last time I saw VB it was
a hybrid language like C++, that allowed normal functions or
variables not to belong to a class.

In .NET, all classes inherit from Object, so you can do the same thing
you can in C# and assign any type to an Object variable.
But you can also leave it out or forget it and you have a problem. What if
the implementor of the library you are using leaves it out?

If the implementer of the library you're using is a bad programmer, then
he's a bad programmer. Using a library written by someone who doesn't know
what he's doing will cause just as many problems no matter what language
he's using.
VB calls its methods with late binding that means you can call
methods that are not in this type

You can do late binding if Option Strict is disabled, but only idiots do
that. And, like I said, idiots will find ways to screw up in C# just as
easily as they will in VB.
Can you explain this feature or give an example? How can I implement a
interface when I don't have a method with a suiting signature?

The method has to have a matching signature, but you can use any name or
scope you want. Here's the classic example:

Public Class TestClass
Implements IDisposable

Public Sub Close() Implements IDisposable.Dispose
...
End Sub
End Class

Dim test1 As TestClass = New TestClass
Dim test2 As TestClass = New TestClass

' These statements are equivalent
test1.Close
DirectCast(test2, IDisposable).Dispose

You could make the method which implements IDisposable.Dispose have any
scope and name you want. You could also have the same method implement more
than one interface member:

Public Class MultiClass
Implements MyInterface1, MyInterface2

Private Sub MyMethod Implements MyInterface1.Method1, MyInterface2.Method2
...
End Sub
End Class

It's a very useful mechanism, and can potentially save a lot of coding.
Being able to declare Private methods to implement the interface is useful
if you don't want to expose those methods outside the interface context; for
example, when you have an object which implements many interfaces and don't
want 100 methods cluttering things up when you'll never use them unless
you're already casting to that interface type.
with blocks are stupid and lead to sloppy coding.

int R;
with color1,color2
R = 1; ' which R is used here?
end with

That's not how With blocks work in VB.NET. They apply to only one
object, and members of that object are prefixed with a period. So:

With color1
.R = 1
End With
a long time ago when I used pascal I also liked this feature but I also saw
that it leads to unreadable and ambigous code. now Iam glad that neither
C/C++ nor Java or C# have such a feature.

VB.NET doesn't have such a feature, either. VB's With mechanism is
neither unreadable nor ambiguous. In fact, I find

Dim myVariable As ListItem = New ListItem

With myVariable
.Text = "New Item"
.ForeColor = Color.Red
.BackColor = Color.LightGray
.Tag = 5
End With

to be more readable than

Dim myVariable As ListItem = New ListItem
myVariable.Text = "New Item"
myVariable.ForeColor = Color.Red
myVariable.BackColor = Color.LightGray
myVariable.Tag = 5
In C#, if you leave a semicolon out the error is instantly
highlighened.

I'm not just talking about syntax errors, I'm talking about all compiler
errors: invalid casts, typos, case mismatches, improper type conversions,
using For Each on an object that doesn't implement IEnumerable, etc. The
minute you type an invalid line, it highlights it for you. These are things
that you have to build your C# project to detect. Not a big deal, of
course, but a helpful little feature that saves some time and teaches you to
avoid those mistakes more effectively.

I appreciate that you like C#, and there's nothing wrong with that.
It's a perfectly decent language. But you seem to have decided that VB.NET
is inferior without actually knowing that much about it. I think it at
least deserves a fair hearing, don't you?

Jeremy
 
J

Jeremy Todd

To show a bit of a difference in peoples opinions, the above features are
two of things I really dislike about VB. Enough so that I would prefer not
to use VB or use classes written using the interface mapping.

Just out of curiosity, what exactly bothers you about the interface
mapping? It seems very intuitive to me.

Jeremy
 
D

Daniel O'Connell [C# MVP]

Jeremy Todd said:
Just out of curiosity, what exactly bothers you about the interface
mapping? It seems very intuitive to me.
I shouldn't say interface mapping as a whole, but I abhor the ability to
rename interface methods, it tends to break my expectations of the class.
For example if you use Close() to implement IDisposable.Dispose I'd be
against it. Hiding it is one thing(which I'm not to keen on either, fyi),
but I dislike the notion of renaming an interface method.
 
C

Cor Ligthert

Hi Cody,
German is case sensitive. The word "Rennen" is a noun, whereas "rennen" is a
verb.
If that was in all with latin written languages than it could be a very good
extention to all program languages.

That could make sense in my opinion when it was in program languages the
same. However I never have seen that. (And although German is the major
language in the EU, I think it is very rare for other people).

Cor
 
J

Jon Skeet [C# MVP]

Jeremy Todd said:
In .NET, all classes inherit from Object, so you can do the same thing
you can in C# and assign any type to an Object variable.

What does that have to do with variables and methods not belonging to a
class?
If the implementer of the library you're using is a bad programmer, then
he's a bad programmer. Using a library written by someone who doesn't know
what he's doing will cause just as many problems no matter what language
he's using.

True. "Option strict" not being the default was a bad idea though, IMO.
It's a very useful mechanism, and can potentially save a lot of coding.
Being able to declare Private methods to implement the interface is useful
if you don't want to expose those methods outside the interface context; for
example, when you have an object which implements many interfaces and don't
want 100 methods cluttering things up when you'll never use them unless
you're already casting to that interface type.

You can do that with explicit interface implementation in C#.
Personally I don't like it, but there we go.
VB.NET doesn't have such a feature, either. VB's With mechanism is
neither unreadable nor ambiguous. In fact, I find

Dim myVariable As ListItem = New ListItem

With myVariable
.Text = "New Item"
.ForeColor = Color.Red
.BackColor = Color.LightGray
.Tag = 5
End With

to be more readable than

Dim myVariable As ListItem = New ListItem
myVariable.Text = "New Item"
myVariable.ForeColor = Color.Red
myVariable.BackColor = Color.LightGray
myVariable.Tag = 5

The key is to name your variables better, of course. I dislike "with"
blocks myself, like Cody.

<snip>
 
C

cody

And VB.NET supports lots of features that C# doesn't have: better
To show a bit of a difference in peoples opinions, the above features are
two of things I really dislike about VB. Enough so that I would prefer not
to use VB or use classes written using the interface mapping.
However, support for exception filters, safer event raising, and a MyClass
are things I'd like to have equivilents of in C# (although exception filters
and MyClass accesses are things I wouldn't use often at all, I do wish they
were there).


What do you mean by Exception filters and MyClass? Could you give an
example?
 
J

Jon Skeet [C# MVP]

If that was in all with latin written languages than it could be a very good
extention to all program languages.

That could make sense in my opinion when it was in program languages the
same. However I never have seen that. (And although German is the major
language in the EU, I think it is very rare for other people).

Here's another example: "Reading" vs "reading" - Reading is a town,
reading is an activity. You can easily distinguish between the two
within an English sentence (assuming it's not the first word of the
sentence) by case.
 
C

Cor Ligthert

Hi Jon,
Here's another example: "Reading" vs "reading" - Reading is a town,
reading is an activity. You can easily distinguish between the two
within an English sentence (assuming it's not the first word of the
sentence) by case.

I assume this one is in every language written with Latin characters (I know
it from 6) . And as I said, I think that it would be a greath extention to
all program languages if the German way was used. There could be even done
checking on it when typing it in the IDE.

However I have not seen in a language ReaDing or things like that.

Cor
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
I assume this one is in every language written with Latin characters (I know
it from 6) . And as I said, I think that it would be a greath extention to
all program languages if the German way was used. There could be even done
checking on it when typing it in the IDE.

However I have not seen in a language ReaDing or things like that.

No - but that's because in computer languages we use other conventions
than spaces to separate words within tokens, using spaces to separate
actual tokens.
 
C

Cor Ligthert

No - but that's because in computer languages we use other conventions
than spaces to separate words within tokens, using spaces to separate
actual tokens.

For me that proves that we are still in the middle ages with programming
languages.
The same things are in the books from those times.

:)

Cor
 
D

Daniel O'Connell [C# MVP]

cody said:
What do you mean by Exception filters and MyClass? Could you give an
example?

Exception Filters: http://blogs.msdn.com/toub/archive/2004/03/05/84698.aspx

MyClass on the other hand allows direct access to the current classes
members without considering virutality. Where a virtual method called on
this results in the most derived method being called, when its called on
somthing like MyClass, the version defined in the class that makes the call
is used. If that makes any sense...
 
J

Jeremy Todd

What does that have to do with variables and methods not belonging to a
class?

Ah, I misunderstood the question. I thought he was referring to late
binding again. Yes, VB.NET does allow methods and variables outside of a
class definition, though I'm not sure how this is implemented internally. I
agree this is a bad idea, so I never use it myself.
True. "Option strict" not being the default was a bad idea though, IMO.

Agreed. I have no idea why they chose to do it this way.
The key is to name your variables better, of course. I dislike "with"
blocks myself, like Cody.

Well, Cody's reasons for disliking them didn't apply to VB.NET. What
don't you like about them, if I may ask?

Jeremy
 
C

cody

What do you mean by Exception filters and MyClass? Could you give an
Exception Filters:
http://blogs.msdn.com/toub/archive/2004/03/05/84698.aspx

I cannot imaginge when this can ever be useful. You can use a rethrow
instead.
I read that the debugger cannot find the orignin of rethrown exceptions but
you can
set the debugger that it catches exception first-chance.
MyClass on the other hand allows direct access to the current classes
members without considering virutality. Where a virtual method called on
this results in the most derived method being called, when its called on
somthing like MyClass, the version defined in the class that makes the call
is used. If that makes any sense...

Interesting feature. I found that in C# there is _no way_ to call the
original method that was overridden!
I tried everything but nothing worked (as expected):

class A
{
public A()
{
a();
this.a();
((A)this).a();
}
public virtual void a(){Console.WriteLine("a");}
}

class B : A
{
public override void a(){Console.WriteLine("b");}
}

But there are certainly very rare circumstances where you really would need
that feature
and I think you can still solve it using reflection if you really want.
 
J

Jeremy Todd

I shouldn't say interface mapping as a whole, but I abhor the ability to
rename interface methods, it tends to break my expectations of the class.
For example if you use Close() to implement IDisposable.Dispose I'd be
against it.

I don't know if there's any getting around it, really, since I believe
many of the classes in the .NET Framework itself do this. System.IO.Stream
certainly behaves this way, though I don't know if the Close method actually
implements IDisposable.Dispose, or merely calls a hidden implementation of
it. Same difference to the developer, though.

In cases where I want to make it absolutely clear that I'm intending to
call an interface method, I always cast the object to that interface type
first:

DirectCast(MyObject, IDisposable).Dispose
Hiding it is one thing(which I'm not to keen on either, fyi),
but I dislike the notion of renaming an interface method.

This preference of mine probably stems from one particularly huge
project I worked on. There were a good, oh, 50 or 60 interfaces used
internally by the project, but that didn't need to be exposed to consumers
of the assembly. Any given class could implement 10 or more of these
interfaces. If the interface implementation hadn't been hidden, the classes
would've had dozens of methods that not only cluttered things up, but
actually exposed functionality that the consumer wasn't supposed to have
access to. I found VB.NET to be much more convenient in handling this than
C#, especially since a lot of those interfaces had methods in common, and
being able to use a single method to implement multiple interfaces made
maintenance easier in this case. For cases when you wouldn't want to do
that, because the behavior of one of those interfaces may change down the
line, you of course have the option of doing it the old-fashioned way.

MANY classes in the Framework have hidden interface implementations,
too -- just about any implementation of IList hides most of its members, for
example.

Jeremy
 
D

Daniel O'Connell [C# MVP]

Jeremy Todd said:
I don't know if there's any getting around it, really, since I believe
many of the classes in the .NET Framework itself do this.
System.IO.Stream
certainly behaves this way, though I don't know if the Close method
actually
implements IDisposable.Dispose, or merely calls a hidden implementation of
it. Same difference to the developer, though.

It calls an explicit implementation of IDisposable.Dispose(or Dispose calls
Close, I forget which way it goes, that doesn't really matter). Although I'd
rather have not seen Dispose hidden, that was a choice made by the
developers, probably to avoid confusing consumers.
In cases where I want to make it absolutely clear that I'm intending to
call an interface method, I always cast the object to that interface type
first:

DirectCast(MyObject, IDisposable).Dispose

That is generally the prefered way, although I don't really like the VB cast
operators either(not that C#'s are much better, I think I prefer the newer
C++ cast operators actually).
This preference of mine probably stems from one particularly huge
project I worked on. There were a good, oh, 50 or 60 interfaces used
internally by the project, but that didn't need to be exposed to consumers
of the assembly. Any given class could implement 10 or more of these
interfaces. If the interface implementation hadn't been hidden, the
classes
would've had dozens of methods that not only cluttered things up, but
actually exposed functionality that the consumer wasn't supposed to have
access to. I found VB.NET to be much more convenient in handling this
than
C#, especially since a lot of those interfaces had methods in common, and
being able to use a single method to implement multiple interfaces made
maintenance easier in this case. For cases when you wouldn't want to do
that, because the behavior of one of those interfaces may change down the
line, you of course have the option of doing it the old-fashioned way.

MANY classes in the Framework have hidden interface implementations,
too -- just about any implementation of IList hides most of its members,
for
example.

As I said, hiding is one thing(although I rarely like hiding part of the
interface and exposing the rest), but renaming is quite another. While the
convience of implementing multiple similar methods is nice, it makes me
wonder why you had so many identical methods across so many interfaces. That
bothers me. But I'm not here to critique a project I've never seen.
In most cases, I don't think that interface members should be hidden, with
the exception of IDisposable when a Close method exists, IList(when in the
presence of strongly typed equivilent members, IList<T> will probably remove
all need to hide its members completely), and a very, very, small number of
others(which I can't think of right now).
 
D

Daniel O'Connell [C# MVP]

cody said:
http://blogs.msdn.com/toub/archive/2004/03/05/84698.aspx

I cannot imaginge when this can ever be useful. You can use a rethrow
instead.
I read that the debugger cannot find the orignin of rethrown exceptions
but
you can
set the debugger that it catches exception first-chance.

Thats not entirely true, rethrowing an exception changes things. The primary
use(which in VB I consider a bit of a malformation) is to access the
exception without the change in the stack frame which occurs when you
actually enter a catch block. Also, rethrowing doesn't continue down the
catch handlers in the current try\catch set, while returning false from a
filter does.
Interesting feature. I found that in C# there is _no way_ to call the
original method that was overridden!
I tried everything but nothing worked (as expected):

class A
{
public A()
{
a();
this.a();
((A)this).a();
}
public virtual void a(){Console.WriteLine("a");}
}

class B : A
{
public override void a(){Console.WriteLine("b");}
}

But there are certainly very rare circumstances where you really would
need
that feature
and I think you can still solve it using reflection if you really want.

There is no real way to do it in C#. I have considered proposing the use of
the keyword class for this several times, I've yet to take the time to
implement it to test its fundamentals and see if it works.

Using reflection or the new dynamic methods in .net 2.0 it wouldn't be hard
at all. A dynamic method could be built with just the proper IL to make the
call to the method you want.
--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
 
J

Jeremy Todd

As I said, hiding is one thing(although I rarely like hiding part of the
interface and exposing the rest), but renaming is quite another. While the
convience of implementing multiple similar methods is nice, it makes me
wonder why you had so many identical methods across so many interfaces.

Because they weren't necessarily identical for all implementations.
Suppose you had interfaces IMass, which has a property which returns the
mass of some item, and IWeight, which returns its weight. For class
GoldBrick, the implementations of those would be (mostly) identical. For
class HotAirBalloon, they'd be very different.

That's horribly oversimplified, of course, but it gives the general
idea.
In most cases, I don't think that interface members should be hidden, with
the exception of IDisposable when a Close method exists, IList(when in the
presence of strongly typed equivilent members, IList<T> will probably remove
all need to hide its members completely), and a very, very, small number of
others(which I can't think of right now).

What about implementations of non-public interfaces? :)

Jeremy
 

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