Solved

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

Posted on 2009-05-10
8
179 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
 

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
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: 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now