 |
| General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category.
** PLEASE BE SPECIFIC WITH YOUR QUESTION **
When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 21st, 2004, 04:57 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
C# would certainly be easier if you are coming from a C++ background. No question there.
Regarding operator overloading, this is a language capabilities issue and really not a language readability issue.
Regarding explicit interface implementation, I'm not sure why you beleive that you can't explicitly implement an interface in VB.NET. I have done it on many occasions.
|
|

December 21st, 2004, 05:23 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Quote:
quote:Originally posted by Hal Levy
There are so many things I see as "backward" in c#- like the dimensioning of variables...
|
I was going to keep my trap shut about more detailed aspect of my personal opinion of readability, but since you brought it up...
Yes, it is by far much easier to pick out a series (or a single) Dim statement than the non- VB method. Particularly when you're in an IDE that has code coloring or you work with people who make a habit of plopping initialization statements in any old place (no offense if you are one of those).
|
|

December 22nd, 2004, 03:44 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Quote:
quote:Originally posted by planoie
Regarding explicit interface implementation, I'm not sure why you beleive that you can't explicitly implement an interface in VB.NET. I have done it on many occasions.
|
How do you implement this code in VB.NET?
Code:
interface IMyItf
{
int Func(int i,int j);
}
public class Derived:IMyItf
{
public int Func(int i,int j)
{
return 100;
}
int IMyItf.Func(int i,int j)
{
return 1000;
}
}
_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
|
|

December 23rd, 2004, 01:37 AM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:How do you implement this code in VB.NET?
Code:
interface IMyItf
{
int Func(int i,int j);
}
public class Derived:IMyItf
{
public int Func(int i,int j)
{
return 100;
}
int IMyItf.Func(int i,int j)
{
return 1000;
}
}
|
i implement it in vb this way:
in IMyItf.cls i write:
Code:
public fuction func(i as int,j as int)as int
end function
in Derived.cls i write:
Code:
implements IMyItf
private function IMyItf_func(i as int,j as int)as int
IMyItf_func=1000
end function
public function func(i as int,j az int)az int
func=100
end function
i dont know about vb.net!
|
|

December 28th, 2004, 04:20 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
mehdi,
I see what you are driving at with this. Here is how I would implement it, pretty much the same as mmrzadeh:
Interface IMyItf
Function Func(i As Integer, j As Integer) As Integer
End Interface
Public Class Derived
Implements IMyItf
Public Func(i As Integer, j As Integer) As Integer
Return 100
End Function
Public IMyItf_Func(i As Integer, j As Integer) As Integer Implements IMyItf.Func
Return 1000
End Function
End Class
HOWEVER: I would never write a class that had two different methods with the same name. That's one of the things I like about the way you implement interfaces in VB. You don't have to have methods that match the interface name. Admittedly, you usually would implement the method with the same name.
IMO, the example you posted is a poor design (despite it's intension as an example). Is this an example they have given you at school?
|
|

December 29th, 2004, 10:36 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Peter,I mistook in my previous post I didnt mean that code,please see below code,
Code:
interface IMyItf
{
int Func(int i,int j);
}
public class Base1:IMyItf
{
int IMyItf.Func(int i,int j)//a private member
{
return 100;
}
}
//reimplementing the IMyItf in Derived class
public class Derived:Base1,IMyItf
{
public int Func(int i,int j)
{
return 1000;
}
}
as you see in above code if Derived class wants to use IMyItf it should implement the IMyItf,but this ability is just available in C#,you know mainly VB.NET doesnt let us reimplement interfaces in subclasses,maybe you notice me why your example implements the interface in Base class explicitly,the answer is sometimes maybe we want to use some classes in .NET framework and we should extend them,but some interfaces have been implemented explicitly(private) so we have to implement them in Derived class,but as I mentioned VB.NET compiler throws an error:
Interface 'IMyItf' is already implemented by base class 'Base1'.
(for example take a look at this link, http://weblogs.asp.net/fbouma/archiv.../12/37199.aspx)
it seems this ability comes to VB.NET in Whidbey.
Quote:
|
quote:Is this an example they have given you at school?
|
no,it wasnt.
_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
|
|

December 29th, 2004, 10:44 PM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
|
you know mainly VB.NET doesnt let us reimplement interfaces in subclasses
|
Please excuse me mehdi,are you sure about it?
I can simply reimplement interfaces in subclasses in vb .
I think ,it hasnt problem in vb.net
|
|

December 30th, 2004, 10:03 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I think it has some problem in VB.NET,see below code,
Code:
//C# Syntax
interface IMyItf
{
int Func(int i,int j);
}
public class Base1:IMyItf
{
int IMyItf.Func(int i,int j)
{
return 100;
}
}
//Reimplementing the IMyItf in Derived class permited
public class Derived:Base1,IMyItf
{
public int Func(int i,int j)
{
return 1000;
}
}
'VB.NET Syntax
Public Interface IMyItf
Function Func(ByVal i As Integer, ByVal j As Integer) As Integer
End Interface
Public Class Base1
Implements IMyItf
Private Function IMyItf_Func(ByVal i As Integer, ByVal j As Integer) As Integer Implements IMyItf.Func
Return 100
End Function
End Class
'Reimplementing the IMyItf in Derived class not permited
Public Class Derived : Inherits Base1
Implements IMyItf
Public Function Func(ByVal i As Integer, ByVal j As Integer) As Integer
Return 1000
End Function
End Class
VB.NET compiler throws:
Interface 'IMyItf' is already implemented by base class 'Base1'.
_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
|
|

December 31st, 2004, 10:00 PM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
mehdi,
Thank you for your practical example.It clearly show that
Quote:
quote:
C# is more ... and represents OOP concepts better...
|
however i believed before, but you surprised me!
|
|

January 5th, 2005, 12:48 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Mehdi,
I think I see what you are driving at here. What you are showing is:
1) We have interface 'IMyItf' (with 'int Func(int i,int j)')
2) Class 'Base1' implements 'IMyItf.Func()'
3) 'Derived' inherits from Base1
4) 'Derived' reimplements the method 'Func()'.
Is the intent of 'Derived.Func' to override 'Base1.Func'? In VB, this can be achieved with the 'Overrides' keyword. In C#, the same can be achieved by implementing the same interface on the derived class and reimplementing the interface method.
Here's what I tried in VB:
Imports System
Public Interface IMyItf
Sub MyMethod()
End Interface
Public Class Base1
Implements IMyItf
Public Overridable Sub MyMethod() Implements IMyItf.MyMethod
Console.WriteLine("Base1.MyMethod")
End Sub
End Class
Public Class Derived : Inherits Base1
Public Overrides Sub MyMethod()
Console.WriteLine("Derived.MyMethod")
End Sub
End Class
Module Module1
Public Sub Main()
Dim iTest As IMyItf
iTest = New Base1
iTest.MyMethod()
iTest = New Derived
iTest.MyMethod()
End Sub
End Module
And translated into C#:
using System;
namespace MyInterfaceTest{
public interface IMyItf{
void MyMethod();
}
public class Base1 : IMyItf{
void IMyItf.MyMethod(){
Console.WriteLine("Base1.MyMethod");
}
}
public class Derived : Base1, IMyItf{
void IMyItf.MyMethod(){ //
Console.WriteLine("Derived.MyMethod");
}
}
class Module1{
public static void Main(){
IMyItf iTest;
iTest = new Base1();
iTest.MyMethod();
iTest = new Derived();
iTest.MyMethod();
}
}
}
The output of each of these is:
Base1.MyMethod
Derived.MyMethod
|
|
 |