DGL
https://delphigl.com/forum/

C# like classes in delphi?
https://delphigl.com/forum/viewtopic.php?f=19&t=6232
Seite 1 von 1

Autor:  noeska [ Do Dez 21, 2006 20:13 ]
Betreff des Beitrags:  C# like classes in delphi?

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.

Autor:  LarsMiddendorf [ Do Dez 21, 2006 21:52 ]
Betreff des Beitrags: 

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;

Autor:  noeska [ Do Dez 21, 2006 22:17 ]
Betreff des Beitrags: 

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.

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/