Solved

Do I need to set again event handler in case I make object to null ?

Posted on 2009-05-10
8
181 Views
Last Modified: 2013-12-17
Hi experts!

  Im using C# 2008 , WinForm NET 2. I have object, which I create, than set dispose and then again create. Im interesting what happen with event holder, do I need to set again events holder, or not ? I ask because after once I commit goto I loose this handler,  software is working but I do not enter in this function (fileTransfer1_OnProgress)

public partial class frmFTPProgress : CommonSearchForm
    {
        
        public SocketTools.FileTransfer fileTransfer1;
 
 
public  Boolean SendFileViaFTP( )
        {
 
        ResumeFTP:
            fileTransfer1 = new SocketTools.FileTransfer();
            fileTransfer1.OnProgress += new SocketTools.FileTransfer.OnProgressEventHandler(fileTransfer1_OnProgress);
 
 
       // some code &
     If (err<>0) 
           {
          fileTransfer1.dispose();
// do I need againt to  fileTransfer1.OnProgress += new                          
//SocketTools.FileTransfer.OnProgressEventHandler(fileTransfer1_OnProgress)
 
                goto ResumeFTP; //                                 
}
 
        }

Open in new window

0
Comment
Question by:dvplayltd
  • 4
  • 4
8 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24348570
If you use an object that has a dispose() method, you should use "using" instead (and, in general, it is best to avoid "goto" with C#, it defies structured coding, but some argue that it's a matter of taste).

Is the above really your code? There are quite some things in that cannot compile: "If" instead of "if", "err" variable which isn't in declared, ".dispose()" which should be ".Dispose()" etc.

Below is an example of how to use the using-block in your case:



using(FileTransfer fileTransfer1 = new SocketTools.FileTransfer())
{
    // do your fileupload etc.
    fileTransfer1.OnProgress += new SocketTools.FileTransfer.OnProgressEventHandler(fileTransfer1_OnProgress);
 
}  // at this point, the compiler will make sure automatically that .Dispose and .Close are called

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24348574
Now, a small note on your question itself: "are the event handlers gone". The answer is: no, they are not. But after Dispose(), you should not use the object anymore. In fact, you will receive an error if you try to. So it doesn't really matter because the events will never be fired anyway.
0
 

Author Comment

by:dvplayltd
ID: 24348784
Yes, this is real code ..almost :-)

This is function which provide send to FTP host file with provide resume. Resume - this is same like start from same function.

 I publish all code (only this function. OK .. i see you profile and understand that you are professional .  
Look and offer change related with this question, I'm sure that you will easy navigate where is responsible code for this. Check function SendFileViaFTP()  and see will you somethink wrong ... i still after one call goto loose  fileTransfer1_OnProgress() ...


I need to use goto, other option is to make recall (function to call it self) which make more question than will solve & J
And  using is not practical  when you see full code will see why & 

Note: There other reason why do not work as expected .. may be FTP server in resume mode not provide information back?

frmFTPProgress.txt
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:dvplayltd
ID: 24348803
This is code for form, I do not send you other file (frmFTPProgress.Designer.cs,   frmFTPProgress.resx) for WInForm because have third part librabry ..DevComponent, Catalist FTP ...

0
 

Author Comment

by:dvplayltd
ID: 24348856
Other, is there diffrence between

fileTransfer1.Dispose()
and
fileTransfer1 = null

Which I should ?
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24348964
On goto: you are entitled to you opinion of course, but the construction you are using can be achieved by using a while-loop. Recursion (calling oneself) is not a good idea in this case. For quite a while, actually, since the sixties, GOTO is considered "harmful". Here's the original paper: http://www.ecn.purdue.edu/ParaMount/papers/dijkstra68goto.pdf, but this is an easier read: Or, more to the point or modern OO: http://visualbasic.about.com/od/imhoinmyhumbleopinion/a/GraGoTo01.htm


> Other, is there diffrence between

yes, there is. Like I said in my original answer, you need to apply a using-statement. Setting it to null is not effective enough, and will cause the .Dispose not to work anymore. You need to Close() and then Dispose if you do it by hand and you must do so in finally-block of a try/catch/finally or a try/finally. You can do that by hand, but like I already said, there's an easier way to do so" the using-statement.

Here's how you can do it by hand, to see what is actually going on under the hood or the using-statement:



FileTransfer fileTransfer1;
try 
{
   fileTransfer1 = new SocketTools.FileTransfer();
   // ... do your things
}
finally 
{
    fileTransfer1.Close();
    fileTransfer1.Dispose();
}

Open in new window

0
 

Author Closing Comment

by:dvplayltd
ID: 31579919
Question is solved in other way .. however thank for you time :-)
P.S: About goto - it is not for common use, but in some situation is best and I use it. Yes, it can be solved via while, but I need to make two while which break each other .. then goto is best.
0
 
LVL 39

Expert Comment

by:abel
ID: 24372290
(from grading comment)
> Question is solved in other way ..


Glad you found your way. Pity you can't use the OO methods of programming, but I know, it takes quite some time to get used to it..

I'm curious about your "other way" though. Because there's little else you can do with objects that expose the IDispose interface, without risking loosing resources. This method is there for a reason. (also read: http://discuss.joelonsoftware.com/default.asp?dotnet.12.387957.17)

On a side note and off-topic: be careful about B- or C- grading, esp. if someone gives you a quick, thorough and to the point answer. You don't have to agree with an expert, but that doesn't mean the answer is less good. Remember: the expert you grade today is the one you may need tomorrow. See the 10-pts must principle of EE.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question