Page 1 of 1
C# passing an instance of a class
Posted: Fri Sep 30, 2011 7:48 pm
by hallsofvallhalla
So if a have a form called charcreation and in it have a class called playercharacter and I create a instance of the class called player1 when rolling stats.
After the stats are rolled and player named you hit play and it hides the charcreation form and opens up mainscreen form. How do I now access that instance of the player class?
Re: C# passing an instance of a class
Posted: Fri Sep 30, 2011 8:45 pm
by Jackolantern
Uggg, I hate Windows Forms because it adds an extra layer of BS on top of the language. It has been so long since I used them, I am straining to remember how you are supposed to handle this.
This may be a bootleg way of handling it, but there should be some kind of code inside your form that creates a new instance of the new form you are wanting to display. You can pass the object you are wanting in the new form into the new form's constructor (which you will have to write a new version which takes your object as an argument; just be sure you still call InitializeComponent()), and then in that custom constructor you would set a field or property in the new form to the passed object so you effectively have transferred it to the new form. Maybe the code to your new form may look like this:
Code: Select all
namespace AppWindowsForms {
public partial class Form1 : Form {
private playercharacter playerData;
public Form1() {
InitializeComponent();
this.playerData = null;
}
public Form1(playercharacter inPlayer) {
InitializeComponent();
this.playerData = inPlayer;
}
//additional code here
}
}
This has made me think maybe it is time to take another look into Windows Forms. As much as I dislike its muddling-up of the clean OO environment of .NET, it is mighty handy when compared to something like Java Swing and Qt. I didn't realize I had forgotten so much about how to work WF.
Re: C# passing an instance of a class
Posted: Fri Sep 30, 2011 8:56 pm
by hallsofvallhalla
yeah it doesn't make sense why they would not have something built in. I mean most apps at least I would think most apps would have several forms and would have to pass the variables across the forms. Especially instances of classes!
Re: C# passing an instance of a class
Posted: Fri Sep 30, 2011 9:15 pm
by Jackolantern
Windows Forms in .NET are supposed to act mostly like code, so they didn't want to add dangling "compartments" to hold variables that do not appear in other forms of C# applications. The thing I dislike about Windows Forms is how quite often the thing about Forms acting like code doesn't always stay true. A lot is hidden away from you, and sometimes you can trip off problems in the hidden partial classes.
Anyway, typically the best way to handle problems like this is to just treat it like the Forms aren't there (which is basically what I did in my post above, which I would hope should work fine).
Re: C# passing an instance of a class
Posted: Fri Sep 30, 2011 11:49 pm
by hallsofvallhalla
gonna give it a try, thanks!
Re: C# passing an instance of a class
Posted: Sat Oct 01, 2011 12:03 am
by Jackolantern
My method hinges on the fact that, from my memory, you have to create an object of a new window to show it. That is correct, right? If so, then the constructor method should work.
Re: C# passing an instance of a class
Posted: Sat Oct 01, 2011 2:50 pm
by hallsofvallhalla
well I have seen others say to just build a get and set method in the original class then use it to get and set the data in the second class.
This is all about me getting better with C# so I am going to go through a few things. Any explanation helps. I am surprised on how fast i am picking things up with C#, I rarely ever used it other than some XNA with a xbox app but it was toying more than anything.
Re: C# passing an instance of a class
Posted: Mon Oct 03, 2011 5:54 pm
by hallsofvallhalla
just got back to working on this. Got it working.
I created the instance of the class outside the constructor to make it public within the class. Then on the click event that loads the new page I created the new form and passed the class data to the new form. I create a new instance of the class in the constructor of the form then assigned the passed data to the new class.
this is actually quite fun

Re: C# passing an instance of a class
Posted: Mon Oct 03, 2011 6:21 pm
by Jackolantern
Yep! That is pretty much what I was meaning by "you just pretend the forms aren't there". You create the class that corresponds to the form, and pass in the data you need as if the class you were creating wasn't tied to a form.
Re: C# passing an instance of a class
Posted: Tue Oct 04, 2011 3:12 pm
by hallsofvallhalla
wow you cannot not naturally return two variables from a method either??
I found a "hack" by turning it into an array first but WOW.