VB vs. C# language challenge question

T

Tomasz Jastrzebski

Hello,

Below there are two equivalent(?) pieces of C# and VB.Net code.
While the C# version compiles with no warning, the VB.Net version does not
compile due to the compiler error BC30149: Class 'c2' must implement 'Sub
m1()' for interface 'i1'.

Does it mean that in VB interface must be implemented, even if it is already
non-explicitly implemented in the base class c1?

How to make this VB code compile *without altering the c1 class* ? - that is
the constraint!
I can not get it compile in VB, while in C# it is just a piece of cake.

Motivation: I want to access base class methods by interface specified in a
derived class.
As strange as it sounds, in C# it works just as expected - test yourself: i1
c = new c2(); c.m1();

Thank you,

Tomasz
PS. this message has also been posted to microsoft.public.dotnet.framework,
but this group may be a better place for this question.

// C# version
class c1 {
public virtual void m1() {
}
}

class c2 : c1, i1 {
}

public interface i1 {
void m1();
}

' VB version
Class c1
Public Overridable Sub m1()
End Sub
End Class

Class c2
Inherits c1
Implements i1
End Class

Public Interface i1
Sub m1()
End Interface
 
S

Stephany Young

Why have you bothered?

Your earlier identical post has been discussed in
microsoft.public.dotnet.framework some hours ago., and the responses there
were both correct and explained the situation comprehensively.
 
T

thomas

The problem seems to be quite obvious.
What I am looking for is a *solution* - this newsgroup might be a better
place.
Tomasz
 
M

Master Programmer

I would just avoid VB all together, its about to be dropped from Visual
Studio in the next release. They plan to replace it with another
language called D@

Steve Ray Irwin
 
R

RobinS

Ignore him, he's wrong. VB is not going to be dropped.
Robin S.
-----------------------------
 
H

Herfried K. Wagner [MVP]

Master Programmer said:
I would just avoid VB all together, its about to be dropped from Visual
Studio in the next release.

Which is again complete nonsense.
 
P

Phill W.

Tomasz said:
the VB.Net version does not compile due to the compiler error
BC30149: Class 'c2' must implement 'Sub m1()' for interface 'i1'.

Does it mean that in VB interface must be implemented, even if it is already
non-explicitly implemented in the base class c1?

"non-explicitly implemented in the base class"
There's no such thing. Try this:

Dim oc1 As i1 = New c1

It won't work. c1 /does not/ implement i1 because you have to
explicitly tell the compiler that it does so, with the Implements
statement on the class and the Implements clause on the relevant method(s).

Why should a method "implement" an item on an Interface just because it
happens to have the same name (actually, same signature)?
It would make adding a new Iterface to an existing class quite a
nerve-racking experience.
How to make this VB code compile *without altering the c1 class* ?

Not too difficult:
Class c1
Public Overridable Sub m1()
End Sub
End Class

Class c2
Inherits c1
Implements i1
Public Overrides Sub m1() Implements i1.m1
End Class

HTH,
Phill W.
 
H

Herfried K. Wagner [MVP]

Tomasz Jastrzebski said:
Below there are two equivalent(?) pieces of C# and VB.Net code.
While the C# version compiles with no warning, the VB.Net version does not
compile due to the compiler error BC30149: Class 'c2' must implement 'Sub
m1()' for interface 'i1'.

Does it mean that in VB interface must be implemented, even if it is
already
non-explicitly implemented in the base class c1?

The VB compiler doesn't perform such ugly method name matching to check if
an interface gets implemented. Instead, the 'Implements' keyword is used to
connect a method to a method of an interface it implements, which is a
cleaner approach. As an additional benefit this allows to choose more
meaningful names for the implemented members than those defined in the
interface. Check out the discussion below on how to archive behavior
similar to C#'s implicit interface implementation in VB:

<URL:http://groups.google.de/group/microsoft.public.dotnet.languages.vb/msg/97a686e3f36ba978>
 
T

thomas

Ha ha, you made my day.
Thank you!


Master Programmer said:
I would just avoid VB all together, its about to be dropped from Visual
Studio in the next release. They plan to replace it with another
language called D@

Steve Ray Irwin
 
P

Paul Clement

¤ I would just avoid VB all together, its about to be dropped from Visual
¤ Studio in the next release. They plan to replace it with another
¤ language called D@

Since you seem to have a keen interest in the next version of Visual Basic 9.0 (Orcas) you may want
to download the CTPs.

http://msdn2.microsoft.com/en-us/library/aa463382.aspx

If you have questions feel free to post. I'm sure we would be more than willing to help you out. ;-)


Paul
~~~~
Microsoft MVP (Visual Basic)
 
M

Mythran

Master Programmer said:
I would just avoid VB all together, its about to be dropped from Visual
Studio in the next release. They plan to replace it with another
language called D@

Steve Ray Irwin


I usually don't respond to flamers but came across this website. MASTER
PROGRAMMER, this is for you:

http://amasci.com/weird/flamer.html

Now we know what's wrong with you. It is a mental disorder. There is a
good plan freely available to help you deal with your flaming/spamming
problem...it's a 3 step program. Mature, grow up, mature.

HTH,
Mythran
 
A

aaron.kempf

it IS being dropped

what would you know about programming; go and play with your titties,
sissy-pants

-Aaron
 

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