Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I Don't know how to tackle in delphi 1

Status
Not open for further replies.

edgarasm

Programmer
Oct 29, 2002
26
BR
id:=Length(oper)+1;
SetLength(oper, id );
oper[Length(oper)-1].id := id-1;
oper[Length(oper)-1].group:=0;
oper[Length(oper)-1].msg:='';

I have a problem that I Don't know how to tackle in delphi.
Many programs, connected to a main program, will execute this peace of code above in order to be added as a newest operator when he connected; the problem is, when the main program verifies what is the length of the oper (array) and add one so that it can get the next free position to held a new operator, this new operator might be getting the same index(id) of another one and it would be cause a lot a problems. I'd like some suggestion of how I could solve it. I Don't know if I could explain well my problem, but I hope whom is going to help me doesn't need to strain much about I've wanted to say.
 
Sorry, but I don't really understand what you are asking.

If the problem is about multiple programs (processes / threads / whatever ) requesting the length and updating the array at the same time, look for a solutions using mutexes (google for "delphi mutex") (or semaphores, or critical sections). This way you can 'lock' the array while updating.

The problem is, I don't think that is what you are asking at all. Maybe I'm just stupid, or maybe you could rephrase your question a little.
 
Another thing that might help is using objects instead of arrays.
Code:
type
  TMyItem = class(TCollectionItem)
  private
      FGroup : Integer;
      FMessage : String;
  public
      constructor Create(ACollection : TCollection); override;
      property Group : Integer read FGroup write FGroup;
      property ItemMessage : String read FMessage write FMessage;
  end;
...

constructor TMyItem.Create(ACollection : TCollection);
begin
   inherited;
   FGroup := 0;
   FMessage := '';
end;

//////////////////////////////////////

//  to use:

constructor TForm1.Create(AOwner : TOwner);
begin
   inherited;
   FItemCollection := TCollection.Create(TMyItem);
end;

destructor TForm1.Destroy;
begin
   FItemCollection.Free;
   inherited;
end;

procedure TForm1.AddNewItem;
begin
   FItemCollection.Add;
end;

Each member of FItemCollection already has an ".Index" property that will serve the same as your ".ID".

Freeing the collection automatically frees every member.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top