Reputation: 1462
I have a class like this:
public Class Work
{
public DataTable GetWork()
{
//return a datatable which contains information about working hourse
}
}
I want to declare another method isnide the GetWork method.
Actually I want to create a class in which I can call something like this:
DataTable myData=Work.GetWork.ToDays()//Returns work days
DataTable myOtherData=Work.GetWork.ToHour();//Returns work hours
How can I achieve the above state? Thanks in advance and pardon my poor english :(
Upvotes: 0
Views: 120
Reputation: 13531
Create a custom DataTable
class in which you implement the ToDays
and ToHour
operations.
Example:
public class Work
{
public MyDataTable GetWork()
{
//return a datatable which contains information about working hourse
}
}
public class MyDataTable : DataTable
{
public int ToDays()
{
// todo: implement
}
public int ToHour()
{
// todo: implement
}
}
I favour this approach to extension methods as you put logic where it logically belongs and it's automatically available without the need to add a using statement.
Upvotes: 1
Reputation: 4104
You must create another class
public class Work
{
public DataTable GetWork()
{
DataTable result;
//create result
return result;
}
}
public class DataTable
{
// properties
// int or other type...
public int ToDays()
{
// return a property
return something;
}
// int or other type...
public int ToHours()
{
// return a property
return something;
}
}
You can call your result with :
var myData = Work.GetWork().ToDays()//Returns work days
var myOtherData =Work.GetWork().ToHour();//Returns work hours
(be sure that GetWork() return always something wich is not null)
Upvotes: 0
Reputation: 1038
You cannot define a method inside another method.
What you can instead do is create a property in your class, as follows:
public class Work
{
public static DataTable WorkProperty
{
get
{
//return a datatable which contains information about working hourse
}
}
}
Now, define your GetWorkInHours
and GetWorkInDays
methods so that they take a DataTable
as parameter, and also return a DataTable
, and do your stuff inside.
Now, you can do stuff like
DataTable myData=Work.WorkProperty.GetWorkInDays()//Returns work days
DataTable myOtherData=Work.WorkProperty.GetWorkInHours();//Returns work hours
Upvotes: 1
Reputation: 24385
You can use Extension Methods to achieve this.
Example:
public static class DataTableExtensionMethods
{
public static int ToDays(this DataTable dt)
{
// fetch and return the days.
}
}
Now you could call it with:
var myDays = Work.GetWork().ToDays();
Upvotes: 2