TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
andres.ar
NA
2
0
Translatinf from Delphi to C# - problem with objects ...
Feb 9 2004 11:56 PM
Is there any prior Delphi developers here ? I'm trying to translate an program from Delphi to C#. Most of the program is translater now, only the worst part is shown here. P.S. Even if youre not familiar with Delphi, please take a look anyway ... The problematic part in the code below is mostly the part GetBook procedure. It takes a record TBookInfo (struct in C#) from an array (ArrayList would have a best use in here) and creates and instance of TMyBook type and assignes it to a varable in the record. The tricky part here is that the instance is created the same type as another variable is - TMyBookClass (witch is class of TMyBook). I have no ideas how to do that in C#. Any ideas (some code would be very nice) ? Can anyone help me with this ? program Project; {$APPTYPE CONSOLE} uses SysUtils, Classes; type // TMyBook is like an basic adapter. It's decendants provide the application // with various functions and variables. TMyBook = class private FName: string; public constructor Create(const AName: string); virtual; property Name: string read FName; end; // TMyBookClass - a metaclass of TMyBook type ??? TMyBookClass = class of TMyBook; // A decendant of TMyBook, will override functions from TMyBook. TMyNewBook = class(TMyBook); // Basicly contains information to create, access and // manage the TMyBook instance. TBookInfo = record Name: String; BookInstance: TMyBook; BookClass: TMyBookClass; end; var // The currently active TMyBook is accessed throught this. ActiveBook: TMyBook; // An array where all the TBookInfos are stored. Books: array of TBookInfo; constructor TMyBook.Create(const AName: string); begin FName := AName; end; // Finds a TMyBook by name if there is one present. function FindBook(const AName: string): Integer; begin for Result := 0 to Length(Books) - 1 do if CompareText(Books[Result].Name, AName) = 0 then Exit; Result := -1; end; // Fills the TBookInfo with information about the TMyBook. // It also determines the class type witch is going to be used. procedure RegisterBookInfo(const AName: string; ABookClass: TMyBookClass); var Index: Integer; begin Index := Length(Books); SetLength(Books, Index + 1); with Books[Index] do begin Name := AName; BookClass := ABookClass; BookInstance := nil; end; end; // Returns the instance of a book by name. The hard part here // is that the instance is created of BookClass type. function GetBook(const AName: string): TMyBook; var Index: Integer; begin Index := FindBook(AName); with Books[Index] do begin Assert(BookInstance = nil); BookInstance := BookClass.Create(Name); Result := BookInstance; end; end; // Sets up a book instance and assigns it to ActiveBook. procedure SetActiveBook(const ABookName: string); begin // ReleaseActiveBook; ActiveBook := GetBook(ABookName); end; begin // First I register the info about the book, usually at program start. RegisterBookInfo('New Book', TMyNewBook); // And if I have a need, I activate it. I might as well use any other book // and switch between them at runtime with this clause. SetActiveBook('New Book'); end. Regards, Desmond
Reply
Answers (
0
)
How to judge a computer offline with C#?
Mouse clicks