So it turns out I am a buffoon, and can't figure out why the heck this isnt working. Does anyone here have any advice or suggestions on the subject, you guys are usually pretty sharp on such technological things.
I am currently using Visual C# 2005 Express to write my code in, and have had some success using the layout managers and on click events and so forth.
However I have currently been programming in a rather Fortran style, using functions ( I think C# wants me to call them methods?) and so forth and Id like to start using classes.
Lets say I have my base class where everything is at the moment called Form1 and set up like this...
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public void method1()
{
blah blah
}
public void method2()
{
blah blah
}
public void method3()
{
blah blah
}
}
}
Form1 is also defined a bit somewhere else, where it is called
partial class Form1
But all my new functions are defined in the code described above, or are declared to be public in the second part of Form1. (Form1.cs is what your default box is called in Visual C# 2005)
Now, as you can see Ive made lots of methods in this class, which Id like to use in some other classes. Which I have tried to do like this...
namespace WindowsApplication1
{
class Victory : WindowsApplication1.Form1
{
public void win()
{
method1();
method2();
method3();
}
}
I the go back to my previous code and edit it like this
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public void method1()
{
blah blah
}
public void method2()
{
blah blah
}
public void method3()
{
blah blah
}
// here is the change
public void method4()
{
Victory myVictory = new Victory();
myVictory.win();
}
}
}
Which as far as I can see should create a new method, method4(), which when called will execute all the previous methods at the same time.
In fact, what happens is nothing. myVictory is created as an instance of the victory class, and if I add the Windows.System.Forms.MessageBox.Show() command to the Victory class I can make it pop up a message box with text in when I use message4() in form1. However it will not use the methods I want it to inherit from form1.
What am I doing wrong here?
Any help would be appreciated. I'm not even sure if this is exactly what classes are for, Im just trying to figure out how to do things.
If this doesn't work how do I pass methods into a class and use them there?
Cheers
Posts
Also, if you're coming from a Fortran background, you should learn a bit about object-oriented programming and how it differs from procedural programming. Having a method of Form1 create a child of Form1 is very odd style, and could probably be done much better.
This looks like it's falling victim to trying to invoke methods within the main GUI thread (basically your form1) from an external thread.
To get around this, you can pass an instance of form1 to the victory class.
Really though you shouldn't do it this way. I would just have an external class that doesn't inherit from Form1, and use delegates to invoke methods within your Form1 class.....
Also if I put a message box in one of the methods in Form1.cs then it pops up too.
Hmm, most of my methods in form1.cs use this sort of command a lot...
world_area.Text += Writing;
(which is the thing that isn't working when called from outside form1, they work when the methods are called from inside form1)
where writing is a string, and world_area is a text box created in the design pane for form1. The world_area object is created in the other half of the file that defines the form1 class which is created automatically as you manage the layout and use the properties panes etc.
I bet something there is making that world_area object unavailable to a call to the method from another class? I have set all the text boxes to be public, shouldn't that make them accessible?
Perhaps I have to do all my writing to screen methods within form1.cs (the main class) and deal with any other methods in the other classes to try and keep form1.cs looking neat?
edit - it looks like Spackle has answered my question. Just to be clear, this effectively means that if a method wants to output to or read from the GUI it should be totally contained within form1.cs. Or whatever the main GUI code class may be?
From a design point of view, yes you want to keep gui related things within the GUI class, i.e. form1 and use your external classes to report back to you.
Something like
GUI wants something (an update)
instantiate your external class
call method in that class which returns some object
GUI can now use this object to update
When I write code in Fortran I will often use inter-related functions, so both function x and function y both use function z, so I thought I could do something similar with my new class.
And clearly I can, but I just cant write to, or read from the GUI from outside its primary class. Like you said.
I shall play around some more tomorrow, I'll probably start again to see if I can build it in a way which lends itself better to the class-method arrangement you described.
I would definitely recommend reading up on the basic theories of object oriented programming, because if you keep coding like that in C# you're going to be fighting against everything OO languages are designed around.
http://www.thelostworlds.net/
Your form1 updates are event driven, meaning lets say you have a button on your main form, and you want it to pop-up a message box. Add you button to the form, it will create the initial code in the *designer .cs file.
Double click the button and your cursor should be blinking at a button1_click function which is actually the code used when you call the click event button (when you click the button, this event is fired...). Anywho, put your MessageBox.Show("blah"); code here and you'll find magically it appears.
*with .NET 2.0 framework and studio 2k5, you can now create partial classes which allows developers to separate there classes into different files, compiling it will bring the class together. In this case, it defaults to the initial generated code in the designer .cs file, and your events in the normal .cs file....
err thats a mouthful. Good luck! feel free to contact me and I can whip up an example for ya if you need.
The partial class thing confused me too, what with express 2005 hiding stuff away in the form1.designer.cs file. For a while it was making it quite difficult to figure out what was going on till I figured that out since I couldnt for the life of me see where all my text box objects were being created!