4 Replies - 430 Views - Last Post: 29 November 2013 - 12:31 PM Rate Topic: -----

#1 zb22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-September 13

object reference in winforms?

Posted 29 November 2013 - 10:29 AM

Hello.

I want to understand why in c# winforms, when I create an object inside a function(button 1 below), the object can be recognized outside the function?
I tried the same idea in the console, and it's impossible to recognize the object outside the function.

after button1 was clicked the object can be recognized in the entire form.
see code below:


namespace _1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Class1 a1;
        
        private void button1_Click(object sender, EventArgs e)
        {
            a1 = new Class1();
          
        }

        private void button2_Click(object sender, EventArgs e)
        {
         
            button2.Text = a1.st; // this will work if button1 was clicked at the first place.
        }

        
    }
}
 


Is This A Good Question/Topic? 0
  • +

Replies To: object reference in winforms?

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 686
  • View blog
  • Posts: 2,985
  • Joined: 03-December 12

Re: object reference in winforms?

Posted 29 November 2013 - 10:32 AM

That function is part of the form class, so it is part of the form. Can you give an example of the console?
Was This Post Helpful? 0
  • +
  • -

#3 jhouns  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 100
  • Joined: 21-December 09

Re: object reference in winforms?

Posted 29 November 2013 - 10:38 AM

What you're thinking of is slightly off. You believe that since the variable a1 is instantiated inside the button click that it should go out of scope when the function ends and be disposed of. However this is incorrect, a variable will only go out of scope if the entire set of curly braces it is in ends. Using your example.

namespace _1
{
    public partial class Form1 : Form
    {// <---------

        Class1 a1;
        
        private void button1_Click(object sender, EventArgs e)
        {
            a1 = new Class1();
          
        }

        
    }// <---------
}



The variable a1 is declared within the highlighted curly braces and as such will be out of scope when the class that body belongs to is set to null or garbage collected. Although the button click function ends, the block of memory that the new class is created in is located outside of the function and so won't be cleaned up when the function ends.

This code would do what you were expecting:
namespace _1
{
    public partial class Form1 : Form
    {
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 a1;
            a1 = new Class1();
          
        }

        
    }
}



If you don't understand any of what I said above or need clarification, I can draw a quick diagram that will clear it all up for you.

This post has been edited by jhouns: 29 November 2013 - 10:41 AM

Was This Post Helpful? 1
  • +
  • -

#4 zb22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-September 13

Re: object reference in winforms?

Posted 29 November 2013 - 11:50 AM

Yes, a diagram would be useful.

This post has been edited by andrewsw: 29 November 2013 - 11:50 AM
Reason for edit:: Removed huge quote

Was This Post Helpful? 0
  • +
  • -

#5 jhouns  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 100
  • Joined: 21-December 09

Re: object reference in winforms?

Posted 29 November 2013 - 12:31 PM

Posted Image

Okay, I know it's simple but bear with me.

I'm going to link it to the following piece of code:
int Main()
{
    MyOtherClass a = new MyOtherClass();
    a.SetB();
    a = null;
} // x

class MyOtherClass
{

  AThirdClass b;

  public void SetB()
  {
    SomeRandomObject c = new SomeRandomObject();
    b = new AThirdClass();
  } // y

} // z



Let me begin by saying that you don't really need to worry about the 'int Main()' part at the moment, it's just for reference.

When the main function is run above, we create a block of memory called a. A is conveniently the exact same size in memory as 'MyOtherClass' needs. If 'MyOtherClass' is 10 bytes in size, a is an empty block in memory 10 bytes in size (this is done automatically at runtime, you don't do this yourself). We then fill that block with an instance of MyOtherClass, this creates an object that copies MyOtherClass word for word (to be simple about it) and places it in the block of memory it said was empty.

When we instantiate MyOtherClass and store it in a on line 3, part of the creation process involves creating another piece of empty space for variable b, except big enough to hold a copy of 'AThirdClass'. We then call the function 'SetB()'. What this does is create YET another empty space in memory (line 15) and then (pretty much) instantly puts a copy of 'SomeRandomObject' in it. If we didn't fill that space in memory and tried to use that object, there would be nothing there (or null) and it would cause a NullReferenceException.

We then tell that space we made for b to be filled with a new instance of 'AThirdClass' so it's no longer empty. Then we reach the end of the function (y). When we reach that curly brace the function is finished and if there was any space in memory created inside of that function, empty or not (like c) then it is now 'out of scope' and is no longer needed. Therefore it can be deleted. POOF. That reserved block of memory is now free for another use. Efficient eh? But what about b? Well, if you look in the diagram above, b wasn't created inside of that function box, just given some data to fill it up, so that can still be used, we keep that.

So if you look at the diagram again, when each box is finished (the function has ended/the object is destroyed) then anything left inside it is also destroyed. But then what about b? When does it get removed from memory? Simple. Line 5.

When you reach line 5 we set the block of memory that a represents to nothing. We effectively clear the data from inside it(although this isn't actually what's happening as objects use references, for this example it will be sufficient, otherwise I would have to explain Garbage Collection, if you want to know about that ask me after). So now that we've emptied out that space, what about all the variables inside it just like b? Well, just like with our function, they are also destroyed because they're no longer needed.

In the same way, when the main function ends (at point x) the space we allocated for a is also freed so other programs can use it.

In summary, from the diagram, when the box is no longer needed or has finished running, any space in memory that was created inside it also ceases to exist (again, this is a gross oversimplification), but any data that was placed into a block of memory allocated (aka any variable declared) outside of that box stays put.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1