c# - Pass a class to a method
I have a about 80 properties in a class, i assign each value in another class. I want to put the values into a class and pass the class to a method where i save each property into a database table.
How can I do that?
EDIT: How do I do if I want to put all the properties in a List? Do I put them in the lis开发者_开发问答t in the property class or in some other method?
Thanks in advance.
Sounds like you're creating what is often called a Data Transfer Object or DTO.
80 properties sounds a bit bonkers....
Apologies, I don't have my IDE handy on my Ipad, but basically something like
class DTO
{
//All you properties go here
}
class databaseConnector
{
public void saveToDB (DTO dto){
//whatever you want to do here...
}
}
If you are asking about how to pass the class to a method, You need to have the class as part of the method signature and then pass your instance of the class to the method:
Method signature:
public void exampleMethod(YourClassName classVariable)
{
//Do your database stuff here
}
Variable declaration
YourClassName exampleClass = new YourClassName();
Do what you need to with it to initialize or set variables then call your method and pass it to it:
exampleMethod(exampleClass);
精彩评论