Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
491 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Multi Lingual BoolToStr() and SexToStr() ConversionsFormat this article printer-friendly!Bookmark function is only available for registered users!
String Coversion
Product:
Delphi all versions
Category:
Object Pascal
Skill Level:
Scoring:
Last Update:
10/08/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet "String Conversions" BoolToStr SexToStr
Times Scored:
1
Visits:
2462
Uploader: Mike Heydon
Company: EOH
Reference: mheydon@pgbison.co.za
 
Question/Problem/Abstract:
Borland has IntToStr(),FloatToStr() etc. It would be nice to have other conversions such as Boolean and Sex.

This article demonstrates how to implement this. The functions are useful to add to your "Little Black Box" but also serve to demonstrate to beginners how to use Enum types and constant arrays in loops.

It is easy to expand them to add additional languages. Simply extend the TEnumLang type and add the language specific text to the Text arrays. Set MaxLang to the last type in list.
Answer:



type
      TSex       = (sxMale,sxFemale);
      TEnumLang  = (elGeneric,elEnglish,elAfrikaans);

const
      BoolText : array [elGeneric..elAfrikaans,false..true] of string =
                    (('FALSE','TRUE'),
                     ('NO','YES'),
                     ('NEE','JA')
                    );

      SexText  : array [elGeneric..elAfrikaans,sxMale..sxFemale] of string =
                    (('M','F'),
                     ('MALE','FEMALE'),
                     ('MANLIK','VROULIK')
                    );

      MaxLang = elAfrikaans;  // Set to last enum if modified above


function BoolToStr(BoolVal : boolean; Language : TEnumLang) : string;
begin
  Result := BoolText[Language,Boolval];
end;


function StrToBool(BoolStr : string) : boolean;
var BStr : string;
    i : TEnumLang;
    ii,
    Retvar : boolean;
begin
  Retvar := false;
  BStr := UpperCase(BoolStr);

  for i := elGeneric to MaxLang do begin
    for ii := false to true do begin
       if Uppercase(BoolText[i,ii]) = BStr then begin
         Retvar := ii;
         break;
       end;
    end;
  end;

  Result := Retvar;
end;



function SexToStr(Sex : TSex; Language : TEnumLang) : string;
begin
  Result := SexText[Language,Sex];
end;


function StrToSex(SexStr : string) : TSex;
var SStr : string;
    i : TEnumLang;
    ii,
    Retvar : TSex;
begin
  Retvar := sxMale;
  SStr := UpperCase(SexStr);

  for i := elGeneric to MaxLang do begin
    for ii := sxMale to sxFemale do begin
       if Uppercase(SexText[i,ii]) = SStr then begin
         Retvar := ii;
         break;
       end;
    end;
  end;

  Result := Retvar;
end;






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Comments
    Dmitri Papichev (Oct 4 2002 6:24PM)

There is now BoolToStr function in Delphi (at least in version 6).
Also, you may take a look at my Article 2850 and comments there regarding BoolToStr.
Respond

RE: Delphi 6
Mike Heydon (Oct 7 2002 10:25AM)

You are correct, Delphi 6 does now have BoolToStr(). The article however should still be of use to users with < D6 or users wishing to implement Multi Lingual Enum to String functions.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
R. Lefter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)