OK I have an array of tserversocket and am using the tag property to keep track of its index. When an event is fired off such as _clientconnect i am using Index := (Sender as TServerSocket).Tag; but i get an error that highlights that line and tells me its an invalid typecast. What am I doing wrong if all I want to do is get the tag property field? It works with other objects.
Are you sure Sender is the TServerSocket? Isn't the event defined like below:
procedure TfrmServer.sskServerClientConnect(Sender: TObject; Socket: TCustomWinSocket);
In which case your code probably should be: Index := (Socket as TServerSocket).Tag;
Have a look at Sender.ClassName to see what Sender really is.
I'm afraid you won't be able to find the TServerSocket tag property using this method. The reason is that Sender is an instance of TServerWinSocket and Socket is an instance of TCustomWinSocket - neither of these can be cast to a TServerSocket. Take a look in ScktComp.pas (in Source\Vcl). TServerSocket is simply a wrapper for an internal instance of TServerWinSocket.
You could do something like this:
TMyServerWinSocket = class(TServerWinSocket)
private
FServerSocket : TServerSocket;
public
destructor Destroy; override;
property Server : TServerSocket read FServerSocket write FServerSocket;
end;
TMyServerSocket = class(TServerSocket)
public
constructor Create(AOwner : TComponent);
end;
The implementation would look like this:
constructor TMyServerWinSocket.Destroy;
begin
FServer := nil;
end;
constructor TMyServerSocket.Create(AOwner : TComponent)
begin
FServerSocket := TMyServerSocket.Create(INVALID_SOCKET);
FServerSocket.Server := Self;
InitSocket(FServerSocket);
FServerSocket.ThreadCacheSize := 10;
end;
Then (whew, almost there) in your event handler you can do this:
procedure TfrmServer.sskServerClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
Index := (Sender as TMyServerWinSocket).Server.Tag;
end;
What this means is that instead of creating TServerSocket objects in your array you'll need to create TMyServerSocket instances.
Hope that helps.