Solved

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

Posted on 2009-05-10
8
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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