Asim Sajjad
Asim Sajjad

Reputation: 2534

How To Pass Class As Params to Function

How can I pass the Parameter to a function. for example

 public void GridViewColumns(params ClassName[] pinputparamter)
 {
 }

and Class is as given below

public Class ClassName
{
     public string Name{get;set;}
     public int RecordID{get;set;}
}

can anyone has idea?

Upvotes: 0

Views: 1028

Answers (3)

iburlakov
iburlakov

Reputation: 4232

Also you can pass instances of ClassName as Array:

ClassName[] arr = new ClassName[]{new ClassName(), new ClassName()};
GridViewColumns(arr);

More details here.

Upvotes: 0

manuel
manuel

Reputation: 184

First thing first, you have to create an object of the class in your main().

ClassName myObject = new ClassName();

then you can pass it as a parameter in your function.

GridViewColumns(myObject);

Hope this helps..

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039538

params means that the method can accept any number of parameters of type ClassName. Example of calling it with two instances of ClassName:

GridViewColumns(new ClassName(), new ClassName());

or

ClassName a = new ClassName();
ClassName b = new ClassName();
ClassName c = new ClassName();
GridViewColumns(a, b, c);

Upvotes: 4

Related Questions