Public needs more public!

Re: Public needs more public!

Postby dzienny » 20 Jun 2013, 21:24

@Conor
Only the new keyword creates a new instance. The rest of the time you are just dealing with the instance that you created earlier. In the case of mainGUI m = new mainGUI(); a new instance of mainGUI is created and the *reference* to this instance is saved to m. So, the instance is created somewhere in the memory. You then get the address of the place where it is located. And this address(reference) is saved to m. Untill you assign something else to m it will keep pointing to the same place in the memory.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Public needs more public!

Postby Conor » 20 Jun 2013, 22:07

Ohhh I see. So mainGUI mg = (mainGUI) gui; will be the current mainGUI loaded?
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Public needs more public!

Postby dzienny » 20 Jun 2013, 22:42

mainGui is a reference type.
MSDN http://msdn.microsoft.com/en-us/library/ms173105.aspx wrote:For reference types, an explicit cast is required if you need to convert from a base type to a derived type:
Code: Select all
// Create a new derived type.
Giraffe g = new Giraffe();

// Implicit conversion to base type is safe.
Animal a = g;

// Explicit conversion is required to cast back
// to derived type. Note: This will compile but will
// throw an exception at run time if the right-side
// object is not in fact a Giraffe.
Giraffe g2 = (Giraffe) a;

A cast operation between reference types does not change the run-time type of the underlying object; it only changes the type of the value that is being used as a reference to that object. For more information, see Polymorphism (C# Programming Guide).

In our case UserControl is a base type and mainGui is a derived type.

Conor wrote:Ohhh I see. So mainGUI mg = (mainGUI) gui; will be the current mainGUI loaded?

Although the type of the value gui is UserControl, it still points to the instance of mainGUI. Knowning that you can safely cast gui to the mainGUI type. All the time it's the same instance, only the type of the value changes.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Public needs more public!

Postby Conor » 20 Jun 2013, 22:56

I'm a little confused, I don't understand all of the terminology but I think I see what you're saying.

So, the mainGUI class is a UserControl - inherits from UserControl?

Is this the same principles as what you're illustrating?

Code: Select all
public class Start
{
    Car c = new Car();
    Bus b = new Bus();

    // You could simply do this?
    Vehicle vc = c;
    Vehicle vb = b;

    // To go back to a Car or Bus you'd have to go...  ?
    Car c2 = (Car)vc;
    Bus b2 = (Bus)vb;

    // And the above doesn't clear any values, it just changes the 'type' of the instance?

    // So maybe you could change the type from Car to Bus also?? And this is like 'mainGUI m = (mainGUI)gui;' ?
   Car c3 = (Car)b2;
}

public class Bus : Vehicle
{
     public Bus()
     {
     }
}

public class Car : Vehicle
{
     public Car()
     {
     }
}

public class Vehicle
{
    public Vehicle()
    {
    }
}


Maybe that is the same sorta thing? I think I understand but its a little confusing for me. I don't know many complex words related to computer science as I'm self taught :?
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Public needs more public!

Postby dzienny » 20 Jun 2013, 23:13

It's an analogous case.
But,
// And the above doesn't clear any values, it just changes the 'type' of the instance?

it doesn't change the type of the instance, it just changes the type of the field. The underlying instance is untouched, only the way you "interpret" it changes.

// So maybe you could change the type from Car to Bus also?? And this is like 'mainGUI m = (mainGUI)gui;' ?
Car c3 = (Car)b2;

It won't work, because Bus is not a Car in this case. Bus doesn't inherit from a Car type so it can't be casted to this type. Whereas gui under the hood is of type mainGUI, so there's no problem in casting it to that type.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Public needs more public!

Postby Conor » 20 Jun 2013, 23:19

I didn't think the last part would work as I've never tried to do it before, this thing is confusing though, well thanks a lot for your help, it looks like I need to go and read some books :geek:
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Public needs more public!

Postby dzienny » 20 Jun 2013, 23:46

Conor wrote:I need to go and read some books :geek:

It's always a good idea :)

You probably want to make sure that you know what is a reference and how it works. And then the concept of casting reference types may become a bit clearer.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Previous

Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 6 guests

cron