Board index » delphi » Re: C# does not have virtual methods and virtual constructors?

Re: C# does not have virtual methods and virtual constructors?


2004-01-30 12:36:15 AM
delphi241
"Barry Kelly" <XXXX@XXXXX.COM>writes
Quote
On Thu, 29 Jan 2004 15:10:37 -0000, "Ian K" <XXXX@XXXXX.COM>
writes:

>but I can not see the difference (conceptual or practical) between a C++/C#
>static method and a non-virtual Delphi class method. As C# has the
former,
>doesn't that mean it has class methods for all practical purposes?

No. In Delphi, inside a class method, the 'Self' symbol refers to the
metaclass itself. The metaclass is like a unique singleton factory
class instance. The metaclass can have virtual methods which can be
overridden by descendant classes, etc.

In C#, static methods have no 'this' symbol which they can refer to.
Ah, I see - had completely overlooked that!
Thanks,
Ian
 
 

Re: C# does not have virtual methods and virtual constructors?

Nick Hodges (TeamB) writes:
Quote
Rudy Velthuis (TeamB) writes:

>Nor has it named indexers, or ways to define an array type. <g>

Cool, I am keeping a list.
You can also add default arguments, then.
-Craig
--
Craig Stuntz [TeamB] ?Vertex Systems Corp. ?Columbus, OH
Delphi/InterBase Weblog : delphi.weblogs.com
InterBase PLANalyzer 1.1 -- Free InterBase query optimization
tool: delphi.weblogs.com/IBPLANalyzer
 

Re: C# does not have virtual methods and virtual constructors?

Ian Mar{*word*249}s writes:
Quote
The best reason I can think of (and it is a lame excuse) is
compatibility with VB(Visual Basic) and the others .NET languages (the same problem
Delphi.NET is facing now).
There's no compatibility problem caused by having non-CLS features
available unless you choose to use them. C# does have non-CLS
features, so not being CLS is hardly a reason to omit them.
My *utter speculation* is that the C# designers felt they weren't
necessary. I find it hard to believe they weren't considered.
-Craig
--
Craig Stuntz [TeamB] . Vertex Systems Corp. . Columbus, OH
Delphi/InterBase Weblog : delphi.weblogs.com
How to ask questions the smart way:
www.catb.org/~esr/faqs/smart-questions.html
 

Re: C# does not have virtual methods and virtual constructors?

Ian K writes:
Quote
"Rudy Velthuis (TeamB)" <XXXX@XXXXX.COM>writes
news:xn0ddur5x1emht001rudyspc@medion3000...
>Randall Parker writes:
>
>>In the thread entitled "Why Use Delphi on .NET" Joanna Carter
>>opined: So what else doesn't C# have?
>
>C# does not have virtual class methods (it doesn't even have normal
>class methods, like Delphi has).

Rudy, my C++ days are some way behind me, and my C# days have yet to
start, but I can not see the difference (conceptual or practical) between
a C++/C# static method and a non-virtual Delphi class method.
Delphi 8 has normal class methods, and static class methods. The static
class methods do not have a hidden self parameter, just like in C# or in
C++.
But the non-static class methods, which Delphi has had since day 1, get a
reference to the *class*, called Self too. But don't confuse that
reference with instance references like this in C# or C++ (and Self in
Delphi), for instance methods. It points to the class, not to an instance.
Especially in combination with virtual constructors or other virtual
class methods, class methods can prove very useful. TObject has a few of
them, like ClassName or InheritsFrom.
--
Rudy Velthuis (TeamB)
"Glory is fleeting, but obscurity is forever."
-- Napoleon Bonaparte (1769-1821)
 

Re: C# does not have virtual methods and virtual constructors?

Barry Kelly writes:
Quote
On Thu, 29 Jan 2004 15:10:37 -0000, "Ian K" <XXXX@XXXXX.COM>
writes:

>but I can not see the difference (conceptual or practical) between a
>C++/C# static method and a non-virtual Delphi class method. As C#
>has the former, doesn't that mean it has class methods for all
>practical purposes?

No. In Delphi, inside a class method, the 'Self' symbol refers to the
metaclass itself. The metaclass is like a unique singleton factory
class instance. The metaclass can have virtual methods which can be
overridden by descendant classes, etc.

In C#, static methods have no 'this' symbol which they can refer to.
And in Delphi for .NET, you also have static class methods, which don't
have a Self reference either.
--
Rudy Velthuis (TeamB)
"Everything has been figured out, except how to live."
- Jean-Paul Sartre (1905-1980)
 

Re: C# does not have virtual methods and virtual constructors?

Nick Hodges (TeamB) writes:
Quote
Brian Cook writes:

>Ms. Carter is, the vast majority of the time, correct so I suspect
>that C# does indeed not have virtual class methods nor virtual
>constructors.

She is correct. C# also doesn't have sets, named constructors, class
reference types, nor typeless var params.
Nor has it named indexers, or ways to define an array type. <g>
--
Rudy Velthuis (TeamB)
"A poem is never finished, only abandoned."
-- Paul Valery (1871-1945)
 

Re: C# does not have virtual methods and virtual constructors?

Ian Mar{*word*249}s writes:
Quote
Would you write a component library with public class methods and
virtual constructors?
Why not, if they're useful? What I wouldn't do is make a component
library which absolutely depended upon their use. I would make them
available as a feature to people who had the tools to use them.
Note that it is possible to call a virtual class method and the like
from a C# app, although the code to do so looks a bit odd.
-Craig
--
Craig Stuntz [TeamB] . Vertex Systems Corp. . Columbus, OH
Delphi/InterBase Weblog : delphi.weblogs.com
How to ask questions the smart way:
www.catb.org/~esr/faqs/smart-questions.html
 

Re: C# does not have virtual methods and virtual constructors?

Craig Stuntz [TeamB] writes:
Quote
Nick Hodges (TeamB) writes:

>Rudy Velthuis (TeamB) writes:
>
>>Nor has it named indexers, or ways to define an array type. <g>
>
>Cool, I am keeping a list.

You can also add default arguments, then.
I *knew* there was at least one thing more. <g>
--
Rudy Velthuis (TeamB)
"How wrong it is for a woman to expect the man to build the world she
wants, rather than to create it herself."
- Anais Nin (1903-1977)
 

Re: C# does not have virtual methods and virtual constructors?

Alessandro Federici writes:
Quote
Once you do that, there's no difference.
Yes there is; you can not use a metaclass in the same way. IOW, you
need to know what kind of class you want at the point you call the
constructor, which isn't true with a virtual constructor.
You can work around this, but it is .... different.
-Craig
--
Craig Stuntz [TeamB] . Vertex Systems Corp. . Columbus, OH
Delphi/InterBase Weblog : delphi.weblogs.com
Want to help make Delphi and InterBase better? Use QC!
qc.borland.com -- Vote for important issues
 

Re: C# does not have virtual methods and virtual constructors?

Quote
>...but I have worked out how to emulate both virtual constructors and class
>methods in C# :-)

Virtual class methods? Tell! Tell!
Yes, please Joanna. Virtual class methods emulated would be very nice!
--
Hannes Danzl [NexusDB Developer]
Newsgroup archive at www.tamaracka.com/search.htm
 

Re: C# does not have virtual methods and virtual constructors?

Quote
named constructors
public class MyClass
{
public static MyClass Create(){
return new MyClass();
}
public static MyClass CreateSpecial(object somethingSpecial){
return new MyClass(somethingSpecial);
}
}
Of course, you still have to define the constructors referenced.
This would look much more Delphi-like too:
MyClass MyObject = MyClass.Create();
:-)
--
-Jimmy
 

Re: C# does not have virtual methods and virtual constructors?

Jimmy [Used-Disks] writes:
Quote
>You can also add default arguments, then.

In C# that is done with overloading:
No, that is overloading. :)
You can do more or less the same thing with either technique, although
an individual developer may prefer one style or the other. In Delphi,
support for default arguments is important for legacy code
compatibility.
-Craig
--
Craig Stuntz [TeamB] . Vertex Systems Corp. . Columbus, OH
Delphi/InterBase Weblog : delphi.weblogs.com
Useful articles about InterBase and Delphi development:
delphi.weblogs.com/articles
 

Re: C# does not have virtual methods and virtual constructors?

"Randall Parker" <XXXX@XXXXX.COM>wrote in
message news:XXXX@XXXXX.COM...
Quote
[..] virtual constructors>

Is this true?
The rest is but the above can be easily circumvented by putting a call to a
virtual method in your constructors.
Once you do that, there's no difference. In Delphi would be something like
this:
type
TSomemthing = class
protected
procedure MyVirtual(ParameterA : integer); virtual;
public
constructor Create;
constructor Create(aParameter : integer);
end;
constructor TSomething.Create;
begin
MyVirtual(-1);
end;
constructor TSomething.Create(aParameter : integer);
begin
MyVirtual(aParameter);
end;
procedure TSomething.MyVirtual(ParameterA : integer);
begin
<..whatever..>
end;
Your descendant classes can now simply override MyVirtual and you're good to
go
 

Re: C# does not have virtual methods and virtual constructors?

Craig:
Would you write a component library with public class methods and virtual
constructors? I mean, if you want to sell it to people who don't work with
Delphi.NET but C# or VB.NET or whatever... you will be limiting your customer
base. Btw, there's a related problem regarding languages with covariant
redefinitions, like Eiffel: if you use one of these classes from C# or
VB.NET, you could easily defeat the type system. I am an enthusiastic
advocate of .NET, but I admit these problems limit what other languages can
do to enhance the basic CLS object model. And, before you ask, I am glad
Borland kept class methods in Delphi.NET.
Ian
 

Re: C# does not have virtual methods and virtual constructors?

Quote
>Cool, I am keeping a list.

You can also add default arguments, then.
In C# that is done with overloading:
public void foo(object bar, object biz, object baz){
// do something useful...
}
public void foo(object bar, object biz){
this.foo(bar, biz, null);
}
public void foo(object bar){
this.foo(bar, null);
// or...
// this.foo(bar, null, null);
}
--
-Jimmy