| | |
Constructor Question?
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 29
Reputation:
Solved Threads: 0
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
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?
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,
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
C# Syntax (Toggle Plain Text)
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?
C# Syntax (Toggle Plain Text)
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,
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.
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.
•
•
•
•
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.
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
C# Syntax (Toggle Plain Text)
ClassA myClass = new ClassA();
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...
C# Syntax (Toggle Plain Text)
Class A { private string _myField; public A() { _myField = "Called Constructor!"; MessageBox.Show(_myField.ToString()); } }
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:
C# Syntax (Toggle Plain Text)
Class A { public A(int x) { int r = x + 3; // This will ouput 3 added to whatever you pass into it MessageBox.Show(r.ToString()); } public A(string s) { // This will output whatever text you pass into it MessageBox.Show(s.ToString()); } }
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.
C# Syntax (Toggle Plain Text)
public int MyMethod(int x) // Notice we put x in there { return = x + 2; } ... Some code here ... // Later on when you call your method you pass an integer into it via the variable x MessageBox.Show(MyMethod(3).ToString()); // 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.
![]() |
Similar Threads
- Newbie Constructor / Array question (C++)
- Virtual constructor (C)
- which one is better (C++)
- buttons and events? (Python)
- copy constructor problem (C++)
Other Threads in the C# Forum
- Previous Thread: Help !! visual studio or Sql mangament studio
- Next Thread: String indexing
Views: 2061 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box button buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagridview dataset datetime degrees draganddrop drawing encryption enum excel file files form format forms ftp function gcd gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






