Results 1 to 6 of 6

Thread: Delphi error, isn't very informative; need help.

  1. #1
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,553
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Delphi error, isn't very informative; need help.

    Hey,
    So I am getting Delphi classes at school, piece of cake ofcourse and I enjoy it a lot.
    But I have an error which doesn't give me enough info to fix it.
    Can you help me?


    Code:
    //I have this in the global scope
    const
      AllowedChars : array[0..10] of char = ('1','2','3','4','5','6','7','8','9','.','0') ;
    
    //the procedure
    function Allow(str:string):string;
    var
      //AllowedChars: array of char;
      AllowedLength, i, i2 :integer;
      Switch : Boolean;
    begin
      AllowedLength := 11;
      for i := 1 to Length(str) do
      begin
        for i2 := 0 to (AllowedLength - 1) do
        begin
    ###     if (str[i] = AllowedChars[i2]) then Switch := true;//acces violation error.
          if (not((i2 =AllowedLength - 1) and (Switch = true))) then Delete(str, i, 1);
        end;
        switch:=false;
      end;
    end;
    
    //The event(on edit text change)
    //when I type a char as 9 it crashes immidiately 
    procedure TForm1.txtLengthChange(Sender: TObject);
    var
      Str:string;
    begin
      Str := txtlength.text;
      txtlength.text := Allow(Str);
    end;
    I commented the line which gives me the error.
    I marked it with ###(the line which gives me the error)

    My goal is to chech the input and I want it to be one of my AllowedChars(array of char) so I loop the input string and but before any logical error can be created it already crashes on the first loop. It isn't a synatax error neither logical so something between.

    In short: how can I fix the acces violation error on the ### marked line.

    I haven't been visiting forums, especially english forums, anymore so my English sucks a lot. I will put this through a spell checker when I get home.

    Greetings,
    Hermen
    ~Hermen

  2. #2
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    You delete something from Str so the length of Str will change, but the loop has the length cached, so it'll loop too far. Try switching to a while statement or perform the deletion on another (temporary) variable.

  3. #3
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,553
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    The fact is even when I give a number it shouldn't delete I get this error but I will try a temporary string! I appreciate your help!
    Wait you're right! when I delete the deletion part it actually works!(it doesn't give a error at least)

    edit:
    I've got it working, thank you very very much!
    ~Hermen

  4. #4
    Join Date
    Mar 2010
    Location
    New Hampshire, U.S.A
    Posts
    586
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    const
    AllowedChars : array[0..10] of char = ('1','2','3','4','5','6','7','8','9','.','0') ;
    u sure that shouldnt be brackets[...] around the array instead of parenthesis(...)?

    AllowedLength, i, i2 :integer;
    try change the 2 to a b or something, i dont know but i never use numbers in my variables, even if you do it alot, you should try that, it could be something you are skipping causing the error. i do that alot sometimes...

    for i := 1 to Length(str) do
    try length(str)-1

    also add a try statement...
    Code:
    try
    //code in here shouldnt give any runtime errors, instead they are caught
    except
    //if the code above gave a runtime error, then this code will run, like writeln('ERROR!')
    finally //this is optional
    //i think this is run as cleanup after an error, but thats just a guess
    end;
    in basic it is 'try... catch exception...finally...end try' but im not positive for delphi, someone else can confirm.

    also assign a value to switch and see what happens, also maybe rename the var, it could possibly be built into delphi or something.
    Sell botted goods at mid to high prices!!! Else we lose our profit AND ruin the game!!!!

  5. #5
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    He already got in to work

    In Delphi, you can initialize variables with parenthesis.
    Variable names with numbers in them are perfectly fine as long as you don't start with one (this also goes for SCAR/Simba).
    Another thing is that string indices start with 1, so String[Length(String)] is, in fact, the last character of the string.
    The try statement is a bit different in Delphi: you can have either try..finally or try..except, not both except and finally

  6. #6
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,553
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Ha, thanks mates!

    My program made some hell of progress today , I have been long out of the Delphi business but I am happy with the result I am just having one silly error.

    I would love if you could help me!

    Code:
    function CalculateBMI(LengthStr, MassStr:string; Woman:boolean):double;
    var
      Length2, RealLength: double;
    begin
        if Woman then Length2 := StrToFloat(LengthStr);
        RealLength := ((Length2 - 6) / 100);
        result := (StrToFloat(MassStr)) / (RealLength * RealLength);
    end;
    when I use Breakpoint debug mode it shows to me that LengthStr = '100'
    but Length2 contains some super weird number =S and then my Delphi gives an error. when I try to use the Length2 variable, help please!

    edit: sec I think I've found the problem such an idiot.
    ~Hermen

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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