Constructor Question?

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 29
Reputation: shsh_shah is an unknown quantity at this point 
Solved Threads: 0
shsh_shah shsh_shah is offline Offline
Light Poster

Constructor Question?

 
0
  #1
Apr 3rd, 2007
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
  1. Class A
  2. {
  3. int [] arr=new int[5];
  4. public A()
  5. {
  6. arr={1,8,96,6,3} ;
  7. }
  8. public static meth()
  9. {
  10. //code
  11. }

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?

  1. Class B
  2. {
  3. int [] arr=new int[5];
  4. public B(int i,int j)
  5. {
  6. int lo=i;
  7. int hi=j;
  8. }
  9. public static meth()
  10. {
  11. //code
  12. }

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,
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Constructor Question?

 
0
  #2
Apr 3rd, 2007
the C# compiler requires a class have a constructor, you don't *have* to use it or even type one if you don't need it. The C# compiler will add a blank constructor with no parameters at compile time if you don't type any constructor. Constructors are good for initialising classes to a certain minimum state of readyness if you like, it is dependant on your project and what you are implementing.

You can have more than one constructor, all must have different signatures (different parameter lists) this allows consumers of your class to instantiate an instance of it in different states again this depends on how complex a class you are creating and the problem you are trying to solve.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 29
Reputation: shsh_shah is an unknown quantity at this point 
Solved Threads: 0
shsh_shah shsh_shah is offline Offline
Light Poster

Re: Constructor Question?

 
0
  #3
Apr 3rd, 2007
Thanks a million.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 15
Reputation: DDoSAttack is an unknown quantity at this point 
Solved Threads: 2
DDoSAttack's Avatar
DDoSAttack DDoSAttack is offline Offline
Newbie Poster

Re: Constructor Question?

 
0
  #4
Apr 4th, 2007
Originally Posted by hollystyles View Post
the C# compiler requires a class have a constructor, you don't *have* to use it or even type one if you don't need it. The C# compiler will add a blank constructor with no parameters at compile time if you don't type any constructor. Constructors are good for initialising classes to a certain minimum state of readyness if you like, it is dependant on your project and what you are implementing.

You can have more than one constructor, all must have different signatures (different parameter lists) this allows consumers of your class to instantiate an instance of it in different states again this depends on how complex a class you are creating and the problem you are trying to solve.
To add to that...

Think of constructors as quite literally that: A method that constructs the class's insides when it is instantiated.
When a class is first called
  1. ClassA myClass = new ClassA();
the very first thing the compiler does is run the constructor.

What I use them for is to setup my fields and what-not so that everything is ready when I need it. Here is an example...
  1. Class A
  2. {
  3. private string _myField;
  4.  
  5. public A()
  6. {
  7. _myField = "Called Constructor!";
  8.  
  9. MessageBox.Show(_myField.ToString());
  10. }
  11. }

When you instiantate the class as I showed above (Class A myClass = new Class A) it will print out "Called Constructor!" when you run it!

What hollystyles is talking about when they mention having more than one constructor (which you can have more than one of any method) is called Overloading. It is a little advanced but the simple explanation is that you can have multiple methods within a class so that you can pass different types into it...

Here is an example:
  1. Class A
  2. {
  3. public A(int x)
  4. {
  5. int r = x + 3;
  6.  
  7. // This will ouput 3 added to whatever you pass into it
  8. MessageBox.Show(r.ToString());
  9. }
  10.  
  11. public A(string s)
  12. {
  13. // This will output whatever text you pass into it
  14. MessageBox.Show(s.ToString());
  15. }
  16.  
  17. }

Parameters are values that you can pass into and out of a method. So for example if you wanted to add a number (x) to 2 and show the output you would pass x as the parameter to the method.
  1. public int MyMethod(int x) // Notice we put x in there
  2. {
  3. return = x + 2;
  4. }
  5.  
  6.  
  7. ... Some code here ...
  8.  
  9.  
  10. // Later on when you call your method you pass an integer into it via the variable x
  11.  
  12. MessageBox.Show(MyMethod(3).ToString());
  13.  
  14. // This will output the result of MyMethod(), which in this case would be 5
Last edited by DDoSAttack; Apr 4th, 2007 at 10:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 29
Reputation: shsh_shah is an unknown quantity at this point 
Solved Threads: 0
shsh_shah shsh_shah is offline Offline
Light Poster

Re: Constructor Question?

 
0
  #5
Apr 5th, 2007
Thanks a million for such a good explaination.Thanks for ur help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2061 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC