Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi people, I'm new to C# and i have a question.

when i have a class called: birthdayParty

and i use it in a form like this

C#
..........
using System.Windows.Forms;

namespace _Project
{
    public partial class Form1 : Form
    {
        birthdayParty BirthdayParty;
        public Form1()
        {
            InitializeComponent();
............


Why do i call that: birthdayParty BirthdayParty; line for ?

My book tells me to but i just wonder why i have to and what it exactly does.

Can anyone answer this for me ?

Thanks in advance
Posted

Hi,

This declaration is analogous to

int a;

where "a" is variable declaration of type int

From here, BirthdayParty is variable of user-defined type "birthdayParty"...
 
Share this answer
 
Comments
jackxl 19-Sep-11 21:57pm    
So i have to declare BirthdayParty as a variable of the type birthdayParty class in order to access methods wich are in the birthdayParty class.

This sounds weird to me.

Why is it not just posible to directly access the birthdayParty class.

( the point of declaring it is clear to me now.. thanks both of you guys. but my last question is just out of curiosity )
kris444 20-Sep-11 11:57am    
This is what object state and behavior talks about...I would suggest you also consider looking
http://msdn.microsoft.com/en-us/beginner/bb308750.aspx
jackxl 20-Sep-11 14:38pm    
Thanks that site is realy helpful. ill guess im gonna check more movies/lessons on it.
Your problem is that you don't understand what a variable is, an instance of a class and a type (a class or not). First of all, you can directly access the class, if it has static members. Each of such members represent only one single object (per Application Domain). All other members are called instance members and are created per instance, when an instance of the class is created (via its constructor).

As to the instance methods; why you cannot call them? they are just methods, right?

Not exactly. They are designed in a pretty simple way. From such method, you can access other instance members (call method and access instance fields and properties). How? You call a method with some instance, and this instance is passed to the method as some undeclared parameter called "this". Inside an instance method, you can access an instance member like this.myMember.

Please see my past solution on this topic for more information: What makes static methods accessible?[^].

—SA
 
Share this answer
 
Comments
jackxl 20-Sep-11 11:29am    
Thanks man. that makes sense :)
Sergey Alexandrovich Kryukov 20-Sep-11 11:59am    
You are welcome.
If you agree it makes sense, please accept my answer formally (green button). You can accept more than one. -- Thanks.
--SA
It might help you,

5. Variables [^]

:)
 
Share this answer
 
Comments
jackxl 19-Sep-11 21:59pm    
Thanks.. Just like my teacher in school always answered when i had a question;

Its all in the book ! ;)
Mohammad A Rahman 19-Sep-11 22:15pm    
:)
Prerak Patel 20-Sep-11 0:50am    
Nice +5.
Mohammad A Rahman 20-Sep-11 1:59am    
Thanks Prerak :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900