Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Constructor Question? (http://www.go4expert.com/forums/constructor-question-t3729/)

shah123 3Apr2007 14:56

Constructor Question?
 
Hi,

I am going to ask a question which is small bit silly but for me not as i want to grasp this information in mind.

The question is i have seen samples of different codes which have as following
Code:

Class A
{
int [] arr=new int[5];
public A()
{
arr={1,8,96,6,3} ;
}
public static meth()
{
//code
}

I dont understand few things in it e.g.

1. Why needed to constructor
2. Why we define array in class and initialize in Constructor?

Code:

Class B
{
int [] arr=new int[5];
public B(int i,int j)
{
int lo=i;
int hi=j;
}
public static meth()
{
//code
}

In Class B:

3. Why we pass paramreters and assign to local variables what we think when we do that?


Can you please answer my questions in nice way instead of being angry on my silly questions.I want to undertand this concept fully when to do or not to do like this?

Thanks,
Regds,

shabbir 3Apr2007 15:26

Re: Constructor Question?
 
Quote:

Originally Posted by shah123
1. Why needed to constructor

To initialize the member variables
Quote:

Originally Posted by shah123
2. Why we define array in class and initialize in Constructor?

Thats what the task of the constructor is.
Quote:

Originally Posted by shah123
3. Why we pass paramreters and assign to local variables what we think when we do that?

To initialize the members with the values we have from the external functions (where we are creating the object).

I guess you need some more info about the constructors and I will provide them for you.

In OOP, a constructor in a class is a special block of statements called when an object is created, either when it is declared. A constructor is similar to a class method, but it differs from a method in that it never has an explicit return type, it's not inherited, and usually has different rules for modifiers. Constructors are often distinguished by having the same name as the declaring class. Their responsibility is to pre-define the object's data members.

shah123 3Apr2007 15:45

Re: Constructor Question?
 
Thanks Shabbir. Thanks a million


All times are GMT +5.5. The time now is 06:20.