Results 1 to 8 of 8

Thread: Type mismatch

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default Type mismatch

    I get this error:
    Code:
    [Error] C:\Simba\Scripts\Responder.simba(26:34): Type mismatch at line 25
    when I try to use this


    Simba Code:
    If Text = (['Hi','Hey','Sup']) Then

    I don't get what im doing wrong

  2. #2
    Join Date
    Feb 2012
    Location
    Wonderland
    Posts
    1,988
    Mentioned
    41 Post(s)
    Quoted
    272 Post(s)

    Default

    Simba Code:
    var
      text : string;
    begin
      text := 'hi';
      if (text = 'hi') then
        writeln('these strings match');
    end.

    OT: you can't compare a string to a string array without using a loop.

    Simba Code:
    var
      text : string;
      i : integer;
      texts : TStringArray;
    begin
      text := 'hi';
      texts := ['ha', 'he', 'hi'];
      for i := 0 to 2 do
        if (text = texts[i]) then
          writeln('these strings match'); // could break out too
    end.

  3. #3
    Join Date
    Nov 2012
    Posts
    2,351
    Mentioned
    55 Post(s)
    Quoted
    603 Post(s)

    Default

    I believe the brackets must be around the entire if statement, because brackets normally imply that whatever's inside them must be calculated first, when doing math for example 3x3x3 vs (3x3)x3, and if you do it the way you did it, I believe the interpreter thinks the array is a bool, so you are trying to conpare a string to a bool, it may work without any brackets, but like Jingle did, Put the brackets around the entire statement and its fine

    If that makes any sense 3am


    Programming is like trying keep a wall of shifting sand up, you fix one thing but somewhere else starts crumbling

  4. #4
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    If you're trying to see check if Text (which I assume is a string) is one of those three strings, you can use StrInArr. Does just what Jingle said, but it's easier to use.

    EDIT:

    Quote Originally Posted by DannyRS View Post
    I believe the brackets must be around the entire if statement, because brackets normally imply that whatever's inside them must be calculated first, when doing math for example 3x3x3 vs (3x3)x3, and if you do it the way you did it, I believe the interpreter thinks the array is a bool, so you are trying to conpare a string to a bool, it may work without any brackets...
    This isn't correct. Parentheses don't do any type conversion or anything, it just affects precedence. He's getting the error because he's trying to compare a string to an array of string.

    Just to show you a comparison, this works fine:

    Simba Code:
    begin
      if [5] = ([6]) then
        Writeln('wat')
      else
        Writeln('ok');
    end.
    Last edited by i luffs yeww; 02-06-2013 at 02:22 AM.

  5. #5
    Join Date
    Nov 2012
    Posts
    2,351
    Mentioned
    55 Post(s)
    Quoted
    603 Post(s)

    Default

    Quote Originally Posted by i luffs yeww View Post


    This isn't correct. Parentheses don't do any type conversion or anything, it just affects precedence. He's getting the error because he's trying to compare a string to an array of string.

    Just to show you a comparison, this works fine:

    Simba Code:
    begin
      if [5] = ([6]) then
        Writeln('wat')
      else
        Writeln('ok');
    end.
    Ah my mistake, didn't see OP was an array, but i'm sure something along the lines of this gave me errors before

    Code:
    If string = (string) Then
    Maybe it was using not

    Code:
    If Not string = string Then
    Something with a string requires correct brackets, I just can't remember what lol


    Programming is like trying keep a wall of shifting sand up, you fix one thing but somewhere else starts crumbling

  6. #6
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    Quote Originally Posted by DannyRS View Post
    Ah my mistake, didn't see OP was an array, but i'm sure something along the lines of this gave me errors before

    Code:
    If string = (string) Then
    Maybe it was using not

    Code:
    If Not string = string Then
    Something with a string requires correct brackets, I just can't remember what lol
    It must've been something else. Like I said, parentheses by themselves will never change the type of what's inside.

    Simba Code:
    if 'hi' = ('hi') then
      Writeln('true');

    All that parentheses are used for is precedence.

  7. #7
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by DannyRS View Post
    Code:
    If Not string = string Then
    Something with a string requires correct brackets, I just can't remember what lol
    Yeah that's probably the cause of your error, since the 'not' keyword takes precedence over all other Boolean operators, which in turn take precedence over all arithmetic operators, hence giving an error as Boolean operators cannot be used on strings in Pascal. (Hence the use of parenthesis to override these orders)

  8. #8
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    thanks

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
  •