Sunday, April 29, 2012

A bit puzzled

Ok I got my program to get the end result the same as the text file. However, I think I might have done something wrong since I didn't use a class unless the 2nd form counts as a class. I think mostly its cause I use 95 lines of code total in the MainForm to finish a final project which seems a bit low for me. Part of me wants to turn this in and get studying for the final the other part thinks something is wrong having too few lines and possibly excluding a class from the answer. Maybe I am just not quite understanding what exactly a class is and have the correct solution. Regardless, I am going to take a break from programming for today to start studying for other classes. Later in the week I may revisit the code but for now I am partially content that it does display the name, email, and phone number.

4 comments:

  1. Wayne,

    Yes, Forms are classes.

    This is why...

    // Create a new instance of the form.
    Form form1 = new Form();

    creates a new form ;-)

    "Form Form1" is like saying "Make me a variable called "Form1" and make it of the "Form" type" (rather than int, string, double, etc).

    The "= new Form();" says "I want the variable over there on the left side of the = sign to be equal to a NEW copy of the Form() class".

    Now, because your Form1 "variable" was cast as the "Form" type (Form Form1, the first part of that code), that means that it's type matches the type of the class that it is to be a new copy of (Form()).

    If you did this, for instance...

    // Create a new instance of the form.
    String form1 = new Form();

    You would get an error, because you are telling it to make a new STRING variable called "Form1", and trying to make it copy the Form() class.

    Classes are like "templates", so you have the template of the class Form(), but it doesn't actually "exist" as an object in your code until you create a new copy of the template (which gets inserted into your "variable", form1).

    Email me if you need more explanation.

    You can read more about the Forms() class here:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx

    ReplyDelete
  2. Thanks Ken,

    I got it now, I wasn't sure if we were supposed to add a class through visual studio or use another form like the dorm/meal calculator. My misunderstanding was probably due to not reading over classes too much. This is good news though, my program is finished. I always thought another form was a partial class not a regular class like the car class problem. However, your post and the white pages cleared things up. Thanks again.

    ReplyDelete
  3. No problem!
    Keep in mind that even if you go modal for forms, each form is still a full class. I don't think they covered that in the book? http://www.homeandlearn.co.uk/csharp/csharp_s13p2.html

    ReplyDelete
  4. I understand your frustration. I have been working with this project for days now and I cannot understand why it won't debug. The errors help but there are still a few I do not know how to fix. I hope everything worked out for you!

    ReplyDelete