I’m trying to write a software that transfers a file from the server to a client… This is how my software works.. On the client-side it gets an IP and a port then spawns a new object from the Client class to a new Thread and passes the ip and the port to the constructor and the client handles the connection and transfers the file to a byte[] variable.. Now I want to prompt the user with a saveFileDialog to save the file…But I don’t have access to the parent class which is my Form. so I can’t do something like savefiledialog.ShowDialog()… What is the solution to this problem?
Share
An event as @syned suggested is a good solution. It is kind of an observer, but in this example you need to interact with the observable class (the client).
You can declare an event in your client class, so when a file is to be received this event can be used. A normal approach is to have a special “context class” which you fill with the values you need.
Now, in your GUI class you can “listen” to this event, and set the filename to what you want.
So, what is an event? In simple terms it’s a function pointer (but please read up on it for more details). So, you tell your client class to call the function when something happens, when a file is to be received for instance. You use events pretty much all the time when using Windows Forms, such as when you assign a button click to a function in your code behind file.
Now, using this pattern, you can have more events, such as a
FileDownloadCompletedfor instance.Also note the
+=syntax. You don’t assign an event to a function, you tell the event to call the function when something happens. You can even have two functions on the same event if you want.