Solved

Some Newbie Questions

Posted on 2004-10-30
177 Views
Last Modified: 2010-04-05
1) Im doing
if edit1.text = '' or edit2.Text = '' or combobox1.Text = '' then begin

Im getting: Operator not applicable to this operand type

Why? and how to fix it?

2)In VB I used to do something like
Dim a() as String;

a is array but its not limited
how to do this is delphi?

3)In VB there was LONG and Integer
what is Long is Delphi?

4)Ive done this
U: array[0..500] of string;
F: TextFile;
x: Integer;
  x:=0;
  reset(f);
  repeat
    readln(F,U[x],P[x],D[x],M[x],Y[x]);
    x :=x+1;
  until Eof(f);
  ShowMessage(U[0]);
  ShowMessage(M[0]);

this text file has this
LINE1: Mike,Hello,3,5,1999
Line2: Jad,Hi,23,10,1980

its Username,Password,Day,Month,Year
all separated by commas
I thought the array would work, the username in U, Password in P and so on...
how to make it work???
0
Question by:peritoX
    13 Comments
     
    LVL 12

    Expert Comment

    by:esoftbg
    1).
    if (edit1.text = '') or (edit2.Text = '') or (combobox1.Text = '') then
    begin
    end;

    2).
    var
      SArr: array of string;
    begin
      SetLength(SArr, 32);
    end;

    3).
    Longword      0..4294967295      unsigned 32-bit
    Cardinal      0..4294967295      unsigned 32-bit

    0
     
    LVL 12

    Accepted Solution

    by:
    4).
    unit Unit1_Q_21188887;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;

    type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo: TMemo;
        MemoUsers: TMemo;
        procedure Button1Click(Sender: TObject);
        private
        { Private declarations }
        public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure SplitStrings(Delimiter: Char; Input: string; SS: TStrings);
    begin
       Assert(Assigned(SS));
       SS.Clear;
       SS.Delimiter := Delimiter;
       SS.DelimitedText := Input;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    type
      TUserRec = record
        U:       string[32];
        P:       string[32];
        Y:       string[4];
        M:       string[2];
        D:       string[2];
      end;
    var
      I:         Integer;
      J:         Integer;
      K:         Integer;
      N:         Integer;
      SL:        TStringList;
      URA:       array of TUserRec;
    begin
       SL := TStringList.Create;
       try
         Memo.Lines.LoadFromFile('UsersData.txt');
         K := Memo.Lines.Count;
         SetLength(URA, K);
         SplitStrings(',', Memo.Text, SL);
         Memo.Lines.Assign(SL);
         N := Memo.Lines.Count;
         J := 0;
         K := 0;
         for I := 0 to N-1 do
         begin
           case J of
             0:   URA[K].U := Memo.Lines[I];
             1:   URA[K].P := Memo.Lines[I];
             2:   URA[K].Y := Memo.Lines[I];
             3:   URA[K].M := Memo.Lines[I];
             4:   URA[K].D := Memo.Lines[I];
           end;
           if (J<4) then
             Inc(J)
           else
           begin
             J := 0;
             Inc(K);
           end;
         end;
         for I := 0 to K-1 do
         with URA[I] do
         begin
           MemoUsers.Lines.Add(U+';'+P+';'+Y+';'+M+';'+D+';');
         end;
       finally
       end;
    end;

    end.
    0
     

    Expert Comment

    by:bBK
    If the situation happened after you compiled and run your source code. My be Ctrl-F2 to reset and get out of the freezing state.

    Otherwise, you need to post all of the available processes on ur computer and cpu their CPU usage here to analyze more. In this case the reasons may be caused by viruses or spyware or other conflict software .......

    Regards

    ******************************
    1. The error you got is caused by the operator execute order is not like your thinking. So that, you must add parantheses to make sense.

    if (edit1.text = '') or (edit2.Text = '') or (combobox1.Text = '') then
    begin
      your-code;
    end;

    2.
    Static array:
    VB: Dim astr2(0 To 9) As String
    Delphi: Var astr2: array[0..9] of String

    Dynamic array:
    VB:
       Dim astr2() as String
       Redim astr2(10) --> assign 10 elements to the array
    Delphi:
       Var  astr2: array of String;
       Setlength(astr2, 10); --> assign 10 elements

    3.  I do not agree with esoftdb about this: Pls check the data type range.
    Integer(in VB) =  Smallint(Delphi) with range of:        -32768 to + 32767
    Long(in VB) = Integer(Delphi) = Longint(Delphi) with range of: –2147483648..2147483647

    4. Your code will give the data to array U only. Because each line correspond to one string, and so that U[x] will get all the line. P[x],D[x],M[x],Y[x] will all blank.
    So the best way is used a space between each data in your line instead of comma.
    Otherwise, uses TStringList to replace all of the comma by space
    Or read line to a string, and then getdata from that String and put to each element in each of your arrays.

    If you need code, response back. I will give it to you soon.

    Regards.

    NBT
    0
     

    Expert Comment

    by:bBK
    1. The error you got is caused by the operator execute order is not like your thinking. So that, you must add parantheses to make sense.

    if (edit1.text = '') or (edit2.Text = '') or (combobox1.Text = '') then
    begin
      your-code;
    end;

    2.
    Static array:
    VB: Dim astr2(0 To 9) As String
    Delphi: Var astr2: array[0..9] of String

    Dynamic array:
    VB:
       Dim astr2() as String
       Redim astr2(10) --> assign 10 elements to the array
    Delphi:
       Var  astr2: array of String;
       Setlength(astr2, 10); --> assign 10 elements

    3.  I do not agree with esoftdb about this: Pls check the data type range.
    Integer(in VB) =  Smallint(Delphi) with range of:       -32768 to + 32767
    Long(in VB) = Integer(Delphi) = Longint(Delphi) with range of: –2147483648..2147483647

    4. Your code will give the data to array U only. Because each line correspond to one string, and so that U[x] will get all the line. P[x],D[x],M[x],Y[x] will all blank.
    So the best way is used a space between each data in your line instead of comma.
    Otherwise, uses TStringList to replace all of the comma by space
    Or read line to a string, and then getdata from that String and put to each element in each of your arrays.

    If you need code, response back. I will give it to you soon.

    Regards.

    NBT
    0
     

    Expert Comment

    by:bBK
    Oh, sorry esoftdb. I do not see your last response when posting :)

    Regards.
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    I forgot to free dynamically created SL

    //........
       finally
         SL.Free;
       end;
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    download a full example about 4).
    from:        http://www.geocities.com/esoftbg/
      link:        Q_21188887.zip
    0
     

    Author Comment

    by:peritoX
    @esoftbg
    can you explain
    TRY
    Finally

    and

    TUserRec = record

    and the diffrence between putting deleratrations in
    type
    private
    or var (inside the procedure)

    and
    Assert(Assigned(SS));

    If you do I'll give you 50 more points.
    You got the first 100.


    @bBK
    [Q]So the best way is used a space between each data in your line instead of comma.[/Q]
    Its doing the same thing.
    0
     
    LVL 12

    Assisted Solution

    by:esoftbg
     SL := TStringList.Create; // Dynamicaly created component
      try
        //........ some code
      finally
        SL.Free;                       // Freeing the memory allocated from SL
      end;
    The above construction guarantees that SL.Free will be executed no matter of any exception between try .... finally
    ..................................................................................................................................................................................
    type
      TForm1 = class()
        private
          ArrLocal: TArr; // this variable is accessible from all the methods inside the every instance of
        public
          ArrGlobal: TArr; // this variable is accessible from all the Units of the Application (which included this Unit into uses clause)
      end;
    procedure TForm1.....
    var (inside the procedure)
      Arr: TArr; // this variable is alive only during every execution of this method
    begin
    //........
    end;
    ....................................................................................................................................................................................TUserRec = record
      //.......................
    end;
    You need a set of 5 different data about every User :
    1. U - name;
    2. P - passsword;
    3. D - day of registration;
    4. M - month of registration;
    5. Y - year of registration;
    I described all of them as string (different length), but it can be:
    type
      TUserRec = record
        U: string[64];
        P: string[128];
        D: Word;
        M: Word;
        Y: Integer;
      end;
    After you have this type declared it is very easy to declare a dynamic array so to be possible storing the information for every User:
    var
      URA: array of TUserRec;

    ................................................................................................................................................................................(Delphi's Help) Assert procedure
    Tests whether a Boolean expression is true.
    Description:
    In Delphi code, use Assert as a debugging tool to test that conditions assumed to be true are never violated. Assert provides an opportunity to intercept an unexpected condition and halt a program rather than allow execution to continue under unanticipated conditions.
    ..................................................................................................................................................................................
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    May be it is better to use:

    procedure SplitStrings(Delimiter: Char; S: string; const SS: TStrings);
    begin
      if (Assigned(SS)) then
      begin
        SS.Clear;
        SS.DelimitedText := S;
        SS.Delimiter := Delimiter;
      end;
    end;

    If it is passed a not assigned parameter SS, the procedure will not raise an exception, in this case nothing to be delimited ....
    0
     

    Author Comment

    by:peritoX
    Thank You A lot.
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    You are welcome !
    Emil
    0
     

    Expert Comment

    by:deathman5
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now