This example is being used several times in this tutorials: application, overloading constructor |
Namespaces or Project |
In other examples with Net Framework 2.0, console examples, we did use several namespaces . Here in this example, we are looking at a project level, where all the classes are created separately, but can be inherited or used as is you created multiples classes using a text editor like text pad or note pad. |
Step 1: I used express C# 2005, and created a project array_1, later
added a "windows.Form", and a class Multiply class. In this project
Program.cs is the default class originated while creating project array_1
|
Note the namespace. |
namespace remains same:
code using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace array_1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } } }
|
code for multiply.cs
|
Debuging window
|
Instanciating class multiply in multiply.cs
|
Now |
follow with the program. Below also shows how to overload overloading
constructors
|
provide inputs
|
The variable Z gets the value
|
The final output
|
Example below shows all the classes in namespace array_1 are put
together
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; //windowsform.cs namespace array_1 { public partial class Form1 : Form { public Form1() { //you need to remark when using framework SD 2.0 commandline // InitializeComponent(); } } class multiply { public int z; private int x, y; public multiply() { Console.WriteLine(" constructor original"); } public multiply(int input1, int input2) { //x = input1; // y = input2; { Console.WriteLine(" constructor overloaded "); } public int method_1(int xx, int yy) { x = xx; y = yy; z = x * y; return z; } } class test { static void Main(string[] args) { multiply mp = new multiply(); Console.Write("enter first numbers "); int n1, n2; n1 = Int32.Parse(Console.ReadLine()); Console.Write("enter second number "); n2 = Int32.Parse(Console.ReadLine()); Console.WriteLine("Total is : " + mp.method_1(n1, n2)); //string str = MessageBox.Show(" Total is " + mp.z); Console.ReadLine(); } } } |
Constructor overloading:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; //windowsform_2.cs namespace array_1 { public partial class Form1 : Form { public Form1() { //you need to remark when using framework SD 2.0 commandline // InitializeComponent(); } } class multiply { public int z; private int x, y; public multiply() { Console.WriteLine(" constructor original"); } public multiply(int input1, int input2) { //x = input1; // y = input2; { Console.WriteLine(" constructor overloaded " + input1 + "and " + input2 + " received");} } public int method_1(int xx, int yy) { x = xx; y = yy; z = x * y; return z; } } class test { static void Main(string[] args) { multiply mp = new multiply(); Console.Write("enter first numbers "); int n1, n2; n1 = Int32.Parse(Console.ReadLine()); Console.Write("enter second number "); n2 = Int32.Parse(Console.ReadLine()); Console.WriteLine("Total is : " + mp.method_1(n1, n2)); //string str = MessageBox.Show(" Total is " + mp.z); multiply mp2 = new multiply(n1,n2); Console.ReadLine(); } } } |
Constructor overload was not in operation, because we by first formed an object with first constructor and then used a parameterized method; however when we created the another object ( " multiply mp2 = new multiply(n1,n2);" and added two arguments that corresponded to the overloaded-parameters of the constructor, we note the output from overloaded constructor
|