Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Di Jun 04, 2024 16:12

Foren-Übersicht » English » English Programming Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 3 Beiträge ] 
Autor Nachricht
 Betreff des Beitrags: C# like classes in delphi?
BeitragVerfasst: Do Dez 21, 2006 20:13 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
In C# you have special classes like String that work like this

Code:
  1.  
  2. String MyString = "    This is an test string   ";
  3. Console.WriteLine(MyString.Trim());
  4.  


It class like the String class is created like this:
http://www.codeproject.com/useritems/Cs ... erator.asp

Is it possible to use something like this in Delphi ((win32) 2005/turbo)? I know something like this is possible for arrays. E.g. MyObject[i] instead of MyObject.Lines[i].

Thanks for your answers in advance.

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Dez 21, 2006 21:52 
Offline
DGL Member
Benutzeravatar

Registriert: Sa Dez 28, 2002 11:13
Beiträge: 2244
Delphi 2006/ Turbo Delphi has operator overloading for .Net(class and record) and Win32(record only, not Delphi2005). This includes the implicit conversion operator.

Code:
  1.  Test=record
  2.    class operator Implicit(t:Test):string;
  3.  end;
  4.  
  5. class operator Test.Implicit(t: Test): string;
  6. begin
  7. end;


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Dez 21, 2006 22:17 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
I got the following answer on the pascalgamedevelopment forum:

Code:
  1.  
  2. type
  3.  MyString = record
  4.   data: string;
  5.   function Trim: MyString;
  6.   class operator implicit(a: string): MyString;
  7.   class operator implicit(a: MyString): String;
  8.  end;
  9.  
  10. { MyString }
  11.  
  12. class operator MyString.implicit(a: string): MyString;
  13. begin
  14.    result.data := a;
  15. end;
  16.  
  17. class operator MyString.implicit(a: MyString): String;
  18. begin
  19.    result := a.data;
  20. end;
  21.  
  22. function MyString.Trim: MyString;
  23. begin
  24.    //trim stuff here
  25.    result := self;
  26. end;
  27.  
  28. begin
  29.    a := '    This is an test string   ';
  30.    writeln(string(a.Trim()));
  31. end;
  32.  


This seem to do what i want, thanks jsoftware.
Also thanks to Lars.

This allow to do some good things in delphi e.g. for vectors, martices etc...

Where can i find explanations of all operators for records in the delphi help, i cannot find it as i get lost in the delphi help.

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 3 Beiträge ] 
Foren-Übersicht » English » English Programming Forum


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 4 Gäste


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
cron
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.040s | 15 Queries | GZIP : On ]