newbie question: Array of classes

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,


I need to manage 5 different ftp-connections, each with a specific
timer, but generally doing all the same kind of work (connect, put,
quit,..)

instead of writing 5 times te same kind of code like

ftpclient ftpclient1 = new ftpclient();
ftpclient ftpclient2 = new ftpclient();
....

ftpclient1.TimerInterval = 60; //seconds
ftpclient2.TimerInterval = 150; //seconds

void ftpclient1.connect()
{//connect to client1 using class ftpclient1};

void ftpclient2.connect()
{//connect to client2 using class ftpclient2};

....

The question now is whether I could put all of this into an array(?)
so that I only need to define the methods only once? I suppose I would
get something like

ftpclient[] myclients = new ftpclient[5];

void myclients[client1].TimerInterval = 60;
void myclients[client2].TimerInterval = 150;

void myclients [client1].connect()
{//connect to client1}

Is this possible? Or is there a better way? All hints/tips
appreaciated. Thanks!

Chris
 
You can do exactly what you looking for, however the implementation would be
slightly different. You have your ftpclass named ftpclient, to create your
array of them, you would first use:

ftpclient[] myclients = new ftpclient[5];

Then you need to create an instance of the class at each index in the array,
to accomplish that you would use:

for(int x = 0; x < myclients.Count; x++)
{
myclients[x] = new ftpclient();
//Any other setup here for myclients[x]
}

After that, you simply use the desired index to access the correct element
in the array, so to set the TimerInterval on the first two clients, you would
use:

myclients[0].TimerInterval = 60;
myclients[2].TimerInterval = 60;

The beauty of such a class array, is that each instance of the class is
independent from the others, so no custom code within each class is needed to
track what index it is, only on the calling side would you have to.

Brendan
 
Chris said:
The question now is whether I could put all of this into an array(?)
so that I only need to define the methods only once? I suppose I would
get something like

ftpclient[] myclients = new ftpclient[5];

An ArrayList is probably better unless you know exactly how many you have and that number wont
change during use.



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
Thank you for these answers.

Ok, I can use arrays or arraylists to better organize the classes. So
I was on the right track after all...

I'm still confused how I can "trigger" specific methods within these
arrays, though. Let me explain: back in my Delphi days I wrote an
async FTPClient program. The app used the catch a reply (like 226,
transfer complete) in an onrequestdone handler. In this handler new
events were triggered using "postmessage" like postmessage(handle,
WM_QUIT,0,0) to disconnect after the last transfer. Is this
postmessage mechanism still valid in C#? Or is there a specific way
to do jst this?

Thanks again!

Chris
 
Chris said:
I'm still confused how I can "trigger" specific methods within these
arrays, though. Let me explain: back in my Delphi days I wrote an

Same as Delphi. ArrayList is like TList.
transfer complete) in an onrequestdone handler. In this handler new
events were triggered using "postmessage" like postmessage(handle,
WM_QUIT,0,0) to disconnect after the last transfer. Is this
postmessage mechanism still valid in C#? Or is there a specific way
to do jst this?

Delegates (Events in Delphi)


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 
You can use an arraylist and threading kind of like this:

System.Collections.ArrayList myConnections=new ArrayList();

myConnections.Add(new FTPInstructions("url","username","password"));
//repeat as many times as needed.

foreach (FTPInstructions c in myConnections)
{
System.Threading.Thread t=new Thread(new
ThreadStart(c.DoOperation()));
}


Your FTPInstructions class might look something like this:

public class FTPInstructions
{
string url, username, password;

FTPClient=new ftpClient();

public FTPInstructions(string url, string username, string password)
{
this.url=url;
this.username=username;
this.password=password;
}

DoOperation()
{
ftpClient.Connect(url,username,password);
ftpClient.SendCommand("command 1"); //I have never done ftp in c# so
don't take this litteral just look at structure.
ftpClient.CloseConnection();
}
}
 
Back
Top