Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
class1 objcls1= new class1 (param1, param2);

see above code ? i want to know what does the above code mean ?
I suspect below is the meaning of this code piece.

class1 contains a function with name class1 and parameters param1, param2.


CSS
public class class1 
{
   
    public class1 (string param1, string param2)
    {


Is my guess is true ?
Posted
Comments
Member 10506003 15-Dec-14 6:37am    
public class class1
{

public class1 (string param1, string param2)
{
// do something
}
}

Above piece of code You have created a Constructors and Constructor is a special method of a class which will invoke automatically whenever instance or object of class is created. Constructors are responsible for object initialization and memory allocation of its class.
So, You had just initialization the Class1 and allocated memory for the class1.
Am Gayathri 15-Dec-14 7:12am    
Thanks

When you use new class1(param1, param2) you create a class1 object and the code in the public class1(string param1, string param2) constructor is called. So if you would put something in your constructor that does something with those strings, then that code is executed when you create the object.

Simple example:
C#
// class:
public class class1
{
    public string aString;
    public string anotherString;
    public class1(string param1, string param2)
    {
        this.aString = param1;
        this.anotherString = param2;
    }
}

// code:
class1 objcls1 = new class1 ("string 1", "string 2");

Then objcls1.aString is "string 1" and objcls1.anotherString is "string 2".
 
Share this answer
 
v2
Comments
Thanks7872 15-Dec-14 6:56am    
Wonderful....+5
Thomas Daniels 15-Dec-14 7:27am    
Thank you!
This method is a constructor of the class with two parameters and is used to create an object (of class type) by using the given parameters.

For more details see the next article: An Intro to Constructors in C#[^]
 
Share this answer
 
v2
Comments
Thanks7872 15-Dec-14 6:55am    
Nice..+5
Raul Iloc 15-Dec-14 7:00am    
Thank you for your vote!
In C#, method that has the same name as the class is called constructor. It serves as...well...constructor of the class instance (when you call new). Any parameters passsed into it are available at the moment of class creation and serve for initialization of the instance state.

Example would be...hm...you double click on the grid containing list of orders from a client. Double click opens new form and passes in orderID like this

C#
frmOrderDetails items = new frmOrderDetails (dgvOrder.CurrentRow["order_id"])


Then you can in details form save that ID and recover from the database everything you need to fill that particular order details.

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
Comments
Am Gayathri 15-Dec-14 7:13am    
Thanks
public class class1
{

public class1 (string param1, string param2)
{
// do something
}
}

Above piece of code You have created a Constructors and Constructor is a special method of a class which will invoke automatically whenever instance or object of class is created. Constructors are responsible for object initialization and memory allocation of its class.
So, You had just initialization the Class1 and allocated memory for the class1.
 
Share this answer
 

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