Thread: C# Scoping Delinma

  1. #16
    Registered User
    Join Date
    Jan 2011
    Posts
    31
    I have a silly question (it sounds silly enough at least), how do you de-allocate heap memory?
    I've looked online which has directed me to Dispose or GC.Collect() but I just want to clean up memory that I've used for objects such as textBoxes.

  2. #17
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You don't; the GC handles that for you. Just make sure you don't have any references to what you want to clean up, and the GC will (one day, hopefully) take care of it.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #18
    Registered User
    Join Date
    Jan 2011
    Posts
    31
    Weird.

    So what if I call new twice on the same reference... see the following code

    Code:
    TextBox variable = new TextBox();
    variable = new TextBox();
    I notice that no warnings or errors but are there any problems behind the scenes?

  4. #19
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Using sender to represent code-based changes in event handlers is problematic and does not really follow the design of Windows Forms. However since Microsoft's solution is not much better I'm open to anything that works.

  5. #20
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by JohnLeeroy View Post
    I notice that no warnings or errors but are there any problems behind the scenes?
    Nope, that's fine. Given that was the entirety of the program, notice that there are no references to the object created from the first new after the second new has been performed. The GC will eventually wake up and clean up the unreferenced objects.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #21
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There are some exceptions with objects that need to call Dispose() to be deallocated.
    You can google about it to learn more.

    Think of this code:
    Code:
    private void textBox_TextChanged(object sender, EventArgs e)
    {
       // probably sent from code
       if(sender == null)
       {
          // do code-wise stuff
       }
       else
       {
          TextBox b= sender as TextBox;
           b.Text = "You just changed me";
       }
    }
    The b.Text line will call again the text event, looping forever. The sender will always be the same TextBox, except if I am missing something. To solve this you will need to use a boolean like:
    Code:
    bool enableChange = true;
    private void textBox_TextChanged(object sender, EventArgs e)
    {
       if (!enableChange) return;
       // probably sent from code
       if(sender == null)
       {
          // do code-wise stuff
       }
       else
       {
          TextBox b= sender as TextBox;
          enableChange = false;
          b.Text = "You just clicked me";
          enableChange = true;
          }
       }
    }
    So basically you temporarily switch off the event.
    You could also use a line like
    Code:
               
    b -= textBox_TextChanged;
    b.Text = "You just clicked me";
    b += textBox_TextChanged;
    [/CODE]
    Which is probably slower, but a bit more straightforward, since it blocks the event for that TextBox only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors: ISO scoping, obsolete binding??
    By -JM in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2005, 08:56 PM
  2. scoping visibility
    By dirgni in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2002, 04:16 PM