0

This is a bit confusing but will try best to explain it. please ask if you need more details.

First i have a class called TPlayers Like so..

TPlayers = class
   Private
      p : array[1..20] of TStringList;
      function GetPlayer(i:integer): TStringList;

    Public
      Property player[i : integer] : TStringList read GetPlayer;

           constructor Create;   virtual;
    implementation

    uses
    main;
    {constructor}
       constructor TPlayers.Create;
       begin
         p[1] := TStringList.Create;
         p[2] := TStringList.Create;
         p[3] := TStringList.Create;
         p[4] := TStringList.Create;
         p[5] := TStringList.Create;
         p[6] := TStringList.Create;
       end;

     function TPlayers.GetPlayer(i: integer): TStringList;
    begin
      Result := p[i];
    end;

I now have FTherePlayers := TPlayers.Create to create the class. First time i add to the stringlist like so

FTherePlayers.Player[strtoint(name2)].Add('posx='+inttostr(posL.x));

or with variables taken out

FTherePlayers.Player[1].Add('posx=15');

This seems to be fine, but next i try to update it like so

FTherePlayers.Player[strtoint(ID)].Values['posx='] := xpos;

or with variables taken out

FTherePlayers.Player[1].Values['posx='] := 12; 

but then i check that value after changing it and it still says 15, thus when i do

showmessage(fthereplayers.player[1].Values['posx']);

it returns 15 but it should be 12. Any idea why its not changeing? thanks Glen

4
  • 4
    You have a typo in FTherePlayers.Player[strtoint(ID)].Values['posx='] := xpos;, it should be FTherePlayers.Player[strtoint(ID)].Values['posx'] := xpos; instead. But it's not a good idea to expose those string lists, rather create method for adding, like TPlayers.AddPlayer and indexed property e.g. TPlayers.PlayerPosition[x], which will access the Values of the internal string list. Commented Nov 15, 2012 at 10:02
  • arg, how did i not see that. also thanks, i all ready have an addplayer but i figured the FTherePlayers.Player[x] was only accessed by the private get player and .p[x] thus ok, but adding a player position which will access the values would not be hard to do , just did not see the point..but again i am still kind of new to delphi and its common constructor Commented Nov 15, 2012 at 10:16
  • If you use above delphi2010 then look add the Generics.Collections. TList, TObjectList, TDictionary and TObjectDictionary Commented Nov 15, 2012 at 11:48
  • the typo was the answer if you want to make it an answer ill accept it. thanks Commented Nov 15, 2012 at 12:13

1 Answer 1

5

You have an extra equals sign at the end of the Name index value of the Values property. You need to use only the name portion of a name value pair without the equals sign. So, in your code just replace the following lines:

// here is an extra equals sign in 'posx=' index value
FTherePlayers.Player[1].Values['posx='] := 12;
FTherePlayers.Player[strtoint(ID)].Values['posx='] := xpos;

with this:

FTherePlayers.Player[1].Values['posx'] := 12; 
FTherePlayers.Player[strtoint(ID)].Values['posx'] := xpos;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.