|
In C# and C++ a method that has the same name as the class itself is called a "constructor"...
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: method that has the same name as the class itself is called a "constructor"...
Thanks, but I already know that.
|
|
|
|
|
Yes, I actually hoped it was so
What I meant is that the limitation is somehow "logical". I don't find logical to have a class named "Fibonacci" that has a method named "Fibonacci". I would rather expect methods like "generate" or "next".
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: Yes, I actually hoped it was so
Yeah. With a Master's in CSCI, teaching CSCI at the college level, and over 15 years IT experience, one should know what a constructor is
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
No, at least in C#, a constructor is named .ctor :
System.DivideByZeroException: Attempted to divide by zero.
at Template.Template..ctor(Int32 x)
Like the indexer, the developer doesn't give it a name, only the return type and parameters.
|
|
|
|
|
Ok, it's time for a little nitpicking then (or, as we say in French, cutting hairs in four). Beware, I've 44 years of experience at that game It means that everything I say here has absolutely no importance at all, and is by no way an attack on anyone. It's just saying something for the pleasure of saying something.
The ".ctor" is not a C# name, it is a CLR/MSIL/whatever name. When I write a constructor for my C# class, I dont type ".ctor", I type the name of my class as the name of the method. Now, whether the compiler has an urgent need to create MSIL code named ".ctor" or "Groborozgruduruk" is totally irrelevant. When I program in C#, and also when I read C# code written by anyone else, I know that if a method is declared that has the same name as the class, it is a constructor. In Delphi, type casts use the same syntax as a function call, so having a function and a type with the same name is not a good idea there neither. Other languages may still have other good reasons to abhor that. This is a good reason why I agree with the compiler when it insists that I don't call my method the same name as the class.
This brings the second question: as the compiler internally creates a method named "Item" as the implementation of the default indexer, does it really impose an unbearable burden on the creativity of programmers? Of course, it means that you cannot have a class named "Item" that has an indexer. But wait! Do you really think a good design would involve a class named very generically "Item" that has a default indexer (and so is itself composed of a collection of something else.) What are these called then? "SubItem" maybe? What if SubItem has itself an indexer? "SubSubItem"? "YetAnoterSubItem"? If you don't give meaningful names to your classes and methods, you are of course welcome to do so, but please don't say the compiler is restricting your creativity
On the other hand, now we know where the coding horror is
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: The ".ctor" is not a C# name, it is a CLR/MSIL/whatever name
Which is the only name that matters.
WilliamSauron wrote: I know that if a method is declared that has the same name as the class, it is a constructor.
That's funny, I know that if a method is declared that has no name and returns an instance of the class, it is a constructor.
|
|
|
|
|
PIEBALDconsult wrote: Which is the only name that matters
Unless you target another architecture than the CLR, of course, in which case you are free to name generated symbols as you see fit. Who said that it is forbidden to directly generate IBM 360 assembly from C# ?
PIEBALDconsult wrote: That's funny, I know that if a method is declared that has no name and returns an instance of the class, it is a constructor.
Funnier, my copy of "The C# Programming Language", ISBN 0-321-15491-6 by Anders Hejlsberg, Scott Wiltamuth & Peter Golde, on chapter 10.10 "Instance Constructors", page 344 has strong hints that they understand the constructor name to be similar to a method name, not a type name:
The identifier of a constructor-declarator must name the class in which the instance constructor is declared. If any other name is specified, a compile-time error occurs.
Similarly, my Visual Studio highlights the constructor as a method name, not as a type. But hey, Visual Studio is a Microsoft product, these are known not to follow recognized standards. And who knows, maybe this Hejlsberg guy is just a newbie
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: Unless you target another architecture than the CLR, of course
True, but it would still likely generate the same name, for convenience.
My copies of the MS and ECMA specs say the same thing... it's an identifier, not a name.
|
|
|
|
|
PIEBALDconsult wrote: True, but it would still likely generate the same name, for convenience
Yes, and since in some assembler languages (the one that comes to mind is Compass but there are others,) labels beginning with a dot must be local, that would make sure your constructors cannot be called from outside your module.
PIEBALDconsult wrote: My copies of the MS and ECMA specs say the same thing... it's an identifier, not a name.
Yes, if you prefer calling that an identifier instead of a name (let's not reach for the scissors and talk about the subtle semantic differences between a name and an identifier.) But still a method identifier, not a type identifier. Last time I looked, the identifier denoting a constructor in my Visual Studio editor had the same black color as another method identifier, not the greenish color of my class identifier. Are they all color blind at Microsoft?
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: Visual Studio editor had the same black color
Tools|Options|Environment|Fonts and Colors|Text Editor|Identifier
VS doesn't discriminate between these identifiers, but I could write an editor that does. (I'll stick with Edit.)
P.S. I finally got around to installing Leppie's xacc.ide -- it displays constructor identifiers in the greenish color.
modified on Friday, November 28, 2008 1:07 PM
|
|
|
|
|
WilliamSauron wrote: This brings the second question: as the compiler internally creates a method named "Item" as the implementation of the default indexer, does it really impose an unbearable burden on the creativity of programmers?
No, they provide an easy way around it. See IndexerNameAttribute .
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
There is no need to have a way around it through an attribute. If a class has an indexer, then it is a collection of some other things; "Item" is therefore not a good name for that class. It is a Coding Horror. Give the class an appropriate name, and you don't need the attribute...
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: "Item" is therefore not a good name for that class.
No, it's simply your opinion.
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Okay, your mission, should you choose to accept it, is to provide one single example where "Item" is an appropriate name for a collection class If you get caught or killed, the moderator will disavow any knowledge of this thread (which will self destruct in five seconds anyway)
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
First of all thanks for your reply it was really interesting to read it, especially:
WilliamSauron wrote: but please don't say the compiler is restricting your creativity
WilliamSauron wrote: On the other hand, now we know where the coding horror is
I have a lot to say, but Midterm is next week so I can't write a lot now.
BYE
Best wishes,
Redwan Al-Bougha
|
|
|
|
|
Try this:
public class Item
{
[IndexerName("IRDUM")]
public int this[int y]
{
get
{
return 0;
}
}
}
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Thanks leppie,
Rob Graham already told me about IndexerNameAttribute .
But thanks for your interest
Best wishes,
Redwan Al-Bougha
|
|
|
|
|
That's right, imagine this:
class Dog
{
public string Dog
{
get
{
return "Fido";
}
}
}
The only member names that can be the same as their enclosing type are constructors. In your case the indexer, named Item, is named like its enclosing type and is not a constructor, so, that's where your problem is coming from.
To fix this, change The name of the class
|
|
|
|
|
Thanks Camilo,
I already know that, but I post this since it has some taste of horror.
Best wishes,
Redwan Al-Bougha
|
|
|
|
|
I cam across this C++ gem today in some code that a co-worker wrote. In his defense, the bulk of his programming experience is in OS/390 Assembler.
char *ShowDrive( TAPEDRIVE *DRV, char *out ) {
int status;
char *msg;
status = 0x2D2D;
if ( DRV->stat & POWERON ) status = 0x4E4F;
if ( DRV->stat & MOUNTED ) status = 0x544D;
if ( DRV->stat & STARTED ) status = 0x5453;
if ( DRV->stat & INTREQD ) status = 0x5249;
sprintf_s( out, 80,"%2d %4.4s %4.4s %2.2s %6.6s %8.8s %8.8s\n ",
DRV->slot, DRV->host, DRV->name, &status, DRV->vols, DRV->msg1, DRV->msg2 );
msg = out + strlen( out );
sprintf_s( msg, 80," Block=%d\n", DRV->blockID );
return out + strlen( out );
}
Where do I begin? I especially like the int status variable that he uses in the sprintf_s with a format specifier of "%2.2s". It took me a while to realize that since he used "&status" as the argument, he was forcing sprintf_s to use it as a char pointer, and that the value of the int was two ASCII characters (reversed, because of Little Endianness).
I also like the return of the char pointer that points to the middle of the constructed string. (In his code the return value was ignored.) I'm not wild about the way it plays willy-nilly with the char pointers, etc. Too much horrible crap to even go into it all. My head hurts.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
There are 10 kinds of people in the world: People who know binary and people who don't.
|
|
|
|
|
Tom Delany wrote: I also like the return of the char pointer that points to the middle of the constructed string.
Tom Delany wrote: return out + strlen( out );
I'd have thought that would return a pointer to the character after the constructed string. Could be excusable if he's intending to add further text to the buffer , although judging by your comments that isn't the case.
|
|
|
|
|
|
PIEBALDconsult wrote: To avoid the Shlemiel the painter's algorithm[^] problem.
As noted, it is often useful to have functions that append to strings return either a count of the number of bytes written or a pointer to the end of the new data. While C-style string handling is often dangerous because of a general lack of bounds checking, suitable coding techniques can at least make it efficient.
BTW, the painter's problem could be adjusted to give whatever performance dropoff is desired between day #2 and day #3 by specifying that the paint dries out with time. When the painter makes shorter trips, he can use up all the paint on his brush before it dries out. After a certain point, however, drying will become a factor. Depending upon whether the paint is useful until it dries out and then becomes useless, or whether painting becomes slower as the paint gets drier, different performance characteristics may be achieved.
|
|
|
|
|
supercat9 wrote: the painter's problem could be adjusted to give whatever performance dropoff is desired between day #2 and day #3 by specifying that the paint dries out with time. When the painter makes shorter trips, he can use up all the paint on his brush before it dries out. After a certain point, however, drying will become a factor. Depending upon whether the paint is useful until it dries out and then becomes useless, or whether painting becomes slower as the paint gets drier, different performance characteristics may be achieved.
I imagine an optimalization task based on that in an exam of computers' architecture (+ a bunch of extra triple integrals to do).
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|