Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: create c# like classes in delphi

  1. #1

    create c# like classes in delphi

    In C# you have special classes like String that work like this

    Code:
    String MyString = "    This is an test string   ";
    Console.WriteLine(MyString.Trim());
    It class like the String class is created like this:
    http://www.codeproject.com/useritems...t_operator.asp

    Is it possible to use something like this in Delphi ((win32) 2005/turbo)? I now 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

  2. #2

    create c# like classes in delphi

    code for Turbo Delphi win32
    Code:
    type
     MyString = record
      data: string;
      function Trim: MyString;
      class operator implicit(a: string): MyString;
      class operator implicit(a: MyString): String;
     end;
    
    { MyString }
    
    class operator MyString.implicit(a: string): MyString;
    begin
       result.data := a;
    end;
    
    class operator MyString.implicit(a: MyString): String;
    begin
       result := a.data;
    end;
    
    function MyString.Trim: MyString;
    begin
       //trim stuff here
       result := self;
    end;
    
    begin
       a := '    This is an test string   ';
       writeln(string(a.Trim()));
    end;
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  3. #3

    create c# like classes in delphi

    Where can i find a tutorial on this class operator constructions? I get lost in the delphi help.

    PS: i seem to have posted this in the wrong forum. It should have been posted in the delphi forum. Sorry for that.
    http://3das.noeska.com - create adventure games without programming

  4. #4
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    create c# like classes in delphi

    [size=9px]Easily moved. [/size]
    Jason McMillen
    Pascal Game Development
    Co-Founder





  5. #5

    create c# like classes in delphi

    I haven't found any valuable online resources. I learnt everything through the help file. Atm it doesn't work but searching for operator overloading with filter set to language: delphi you should find what you need.

    if you need a list of operators available to overload then heres a shortened list which is contained in my Tvector4f record:
    Code:
      class operator add(a,b: tvector4f): tvector4f;
      class operator add(a: tvector4f; b: single): tvector4f;
      class operator add(a: single; b: tvector4f): tvector4f;
      class operator Subtract(a,b: tvector4f): tvector4f;
      class operator Subtract(a: tvector4f; b: single): tvector4f;
      class operator Subtract(a: single; b: tvector4f): tvector4f;
      class operator Multiply(a,b: tvector4f): tvector4f;
      class operator Multiply(a: tvector4f; b: single): tvector4f;
      class operator Multiply(a: single; b: tvector4f): tvector4f;
      class operator Divide(a,b: tvector4f): tvector4f;
      class operator Divide(a: tvector4f; b: single): tvector4f;
      class operator Divide(a: single; b: tvector4f): tvector4f;
      class operator Equal(a,b: tvector4f): boolean;
      class operator NotEqual(a,b: tvector4f): boolean;
      class operator Implicit(a: tvector4f): string;
    If you use operator overloading for classes then you'll have to create the result yourself
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  6. #6

    create c# like classes in delphi

    That new feature of operator overloading in Delphi is very interesting.

    I would really like to have arithmetic assigners, like +=, *= in Java. But how would them be in Pascal? Something like +:= or :*=... hehe, looks like a rabbit :lol:

  7. #7

    create c# like classes in delphi

    fpc supports prefix operators(?) like c like +=, -=, *=,...
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  8. #8

    create c# like classes in delphi

    can you also do operator overloading in fpc?
    http://3das.noeska.com - create adventure games without programming

  9. #9

    create c# like classes in delphi

    Quote Originally Posted by noeska
    can you also do operator overloading in fpc?
    Yes it's been part of FPC way before Delphi got it.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  10. #10

    create c# like classes in delphi

    operator overloading in fpc is done outside type definitions like this

    [pascal]interface

    type
    tvector = ...
    end;

    operator /(a: tvector4f; b: single)result: tvector4f;
    operator *(a: tvector4f; b: single)salmon: tvector4f;

    implementation

    operator /(a: tvector4f; b: single)result: tvector4f;
    begin
    result := vector4f(a.x/b,...);
    end;

    operator *(a: tvector4f; b: single)salmon: tvector4f;
    begin
    salmon := vector4f(a.x*b,...);
    end;[/pascal]
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •