The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C#, and my incompetance in writing it

tbloxhamtbloxham Registered User regular
edited March 2007 in Help / Advice Forum
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

"That is cool" - Abraham Lincoln
tbloxham on

Posts

  • JaninJanin Registered User regular
    edited March 2007
    If you put a message box in the various method#s, does it get shown? The code you posted looks like it should work, so my guess would be some weirdness in the called methods.

    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.

    Janin on
    [SIGPIC][/SIGPIC]
  • SpackleSpackle Registered User regular
    edited March 2007
    Hmmm

    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.....

    Spackle on
    Taco Bell does win the franchise war according to the tome of knowledge that is Demolition Man. However, I've watched Demolition Man more then a few times and never once did I see WoW. In conclusion Taco Bell has more lasting power then WoW.
    D&D Metal Thread: HERE
  • tbloxhamtbloxham Registered User regular
    edited March 2007
    If I put a message box inside the victory class (in victory.win()) then it pops up if thats what you mean.

    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?

    tbloxham on
    "That is cool" - Abraham Lincoln
  • SpackleSpackle Registered User regular
    edited March 2007
    Actually, I'm confused on what you are trying to accomplish. My answer may have been incorrect.

    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

    Spackle on
    Taco Bell does win the franchise war according to the tome of knowledge that is Demolition Man. However, I've watched Demolition Man more then a few times and never once did I see WoW. In conclusion Taco Bell has more lasting power then WoW.
    D&D Metal Thread: HERE
  • tbloxhamtbloxham Registered User regular
    edited March 2007
    It was just a test case really, Ive been writing methods within form1 which could update the objects seen on the screen according to which button you pushed or according to certain relationships between on screen variables and I thought it might be interesting to see if I could make a class to do a similar thing.

    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.

    tbloxham on
    "That is cool" - Abraham Lincoln
  • blincolnblincoln Registered User regular
    edited March 2007
    Yeah, what you're doing in that example doesn't make a whole lot of sense. If you want to have a class that can call three of its own methods in a row, you should just have it call three of its own methods in a row, not instantiate a child class that has a method which calls the three methods in the parent class.

    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.

    blincoln on
    Legacy of Kain: The Lost Worlds
    http://www.thelostworlds.net/
  • SpackleSpackle Registered User regular
    edited March 2007
    Yea it sounds like you understand writing code and developing applications, just the OOP design was confusing you.

    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.

    Spackle on
    Taco Bell does win the franchise war according to the tome of knowledge that is Demolition Man. However, I've watched Demolition Man more then a few times and never once did I see WoW. In conclusion Taco Bell has more lasting power then WoW.
    D&D Metal Thread: HERE
  • tbloxhamtbloxham Registered User regular
    edited March 2007
    I think you're both right, I was trying to use classes as if they were just another way of implementing a method but which let you put the code in another thread. Clearly however they are different to that.

    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!

    tbloxham on
    "That is cool" - Abraham Lincoln
Sign In or Register to comment.