Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Understanding the default code (http://www.programmingforums.org/showthread.php?t=13201)

357mag May 23rd, 2007 12:22 AM

Understanding the default code
 
Looking ahead into a simple Windows project that would just have a button on a form, and then when the button is clicked I would display a message or something. When I double-click the button the following code is presented in the editor window:

:

namespace Nice_Girl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}


Can someone explain what some of that stuff means? I know what a namespace is so I'm okay with that, but the rest of it nope. I've looked on the web but can't seem to find any clear cut answers that aren't way over my head.

crawforddavid2006 May 23rd, 2007 12:29 AM

:

  1. namespace Nice_Girl  //this is your project name.
  2. {
  3.     public partial class Form1 : Form // this is the class for your form.
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent(); // this is what starts your form
  8.         }
  9.  
  10.         private void button1_Click(object sender, EventArgs e)
  11.         {
  12.                 //anything in here is done when you click the button.
  13.         }
  14.     }
  15. }


does that help?

357mag May 23rd, 2007 1:04 AM

A little. The "partial" keyword means something about your class definition being able to be split into multiple physical files? That's what one site said about them anyway.

Why is it written Form1 : Form

and not

Form1?

Then moving on, is Public Form1() known as what is called a constructor? Although I really don't know what a constructor is or what purpose it serves.

Then I guess InitializeComponent() must be some kind of method.

Finally, what is the object sender, EventArgs e mean? I assume that must be some kind of parameter list, but if you could elaborate a little.

crawforddavid2006 May 23rd, 2007 1:20 AM

ok first, i dont know what "partial" means.

second, the ": Form" after Form1 makes it realize that this is a form being compiled. If i'm wrong, someone please correct me.

All a constructor is, is a method that creates a new instance of an object.

Yes InitializeComponent() is a method, that is built in.

and sorry, but I have no idea what object senger, EventArgs e means.

jaeusm May 23rd, 2007 1:21 AM

Quote:

I've looked on the web but can't seem to find any clear cut answers that aren't way over my head.
I think it may be in your best interest to purchase an introductory C# programming book. Many people like Jesse Liberty's "Programming C#" book. It will start you out at ground level and work up.

357mag May 23rd, 2007 1:31 AM

Actually I have the C# Primer Plus book by Klaus Michelsen. But in that book he just deals with the core language.

357mag May 23rd, 2007 1:34 AM

I read about the partial classes on www.devx.com/dotnet/Article/22603/1954?pf=true.

Booooze May 23rd, 2007 1:48 AM

Quote:

namespace Nice_Girl //this is your project name.
It's more than that, I think your project name can even be different than the namespace. I'm not very good at explaining it (perhaps one of the other members can do it). Namespaces allow for even more organization. You can even embed namespaces within each other. there are some other things it enables you to do, but I can't explain them. I never really use them too tell you the truth.

Quote:

private void button1_Click(object sender, EventArgs e)
The whole thing is an event/method. (I'm not great on terminology)
When button one gets clicked, the code in the brackets gets triggered. The object sender, EventArgs e are the arguments. In this case, the event takes on 2 arguments. It takes sender (which is an object), and e, which is a EventArgs. Everything in the whole system namespace (from what I remember) can be represented as an object (correct me if I'm wrong?). Eventargs e provides ( as described) extraarguments, for the event. Not very decriptive on my part I know. You have to explore it yourself. It all comes down to the hierarchal tree. Use the following code to see what object gets returned when you use this code under the button click event:

:

MessageBox.Show(sender.ToString());

Another note on arguments. it's no different from the following code:

:

public void test(string MyString1, string Mystring2){}

the above code is a function that does not return a value, but takes 2 strings as arguments.

Anyways, I hope that helped.


All times are GMT -5. The time now is 2:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC