开发者

How To Pass Class As Params to Function

开发者 https://www.devze.com 2022-12-26 09:21 出处:网络
How can I pass the Parameter to a function. for example public void GridViewColumns(params Clas开发者_运维问答sName[] pinputparamter)

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

 public void GridViewColumns(params Clas开发者_运维问答sName[] pinputparamter)
 {
 }

and Class is as given below

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

can anyone has idea?


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);


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..


Also you can pass instances of ClassName as Array:

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

More details here.

0

精彩评论

暂无评论...
验证码 换一张
取 消