0

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 :(

Ali.Rashidi
  • 1,284
  • 4
  • 22
  • 51
  • Your GetWork example isn't a method. Please clean up your code, so we may better understand it. – Nick Udell May 07 '14 at 11:46
  • http://stackoverflow.com/questions/5884319/c-function-in-function-possible – Guilherme Natal de Mello May 07 '14 at 11:47
  • You cannot declare a method within a method. That's absurd. `Work.GetWork.ToDays()` would be possible, if an object returned by `GetWork` would have `ToDays` method, but you'd still need to call it this way `Work.GetWork().ToDays()`. – Tarec May 07 '14 at 11:47
  • @Tarec - What about anonymous methods? Absurd is a little pointed. &Nick - its clear the OP may not may be well versed and, if contravening some of the forum rules, we can be a bit more polite to noobs to point them where to look. – brumScouse May 07 '14 at 11:50
  • @brumScouse I would still hope that, in having tried to code with two methods like their example, they would have fixed the basic compiler error that their example causes, and that is unrelated to their question. – Nick Udell May 07 '14 at 11:57
  • @Nick - You're right, Also I think I misread your comment. I might have had one eye open at the time.... – brumScouse May 09 '14 at 19:43

4 Answers4

2

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();
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
1

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
Bhushan Shah
  • 1,028
  • 8
  • 20
  • Avoid static as much as possible. – L-Four May 07 '14 at 12:01
  • @L-Three This is just so that the property can be called on `Work` class itself, instead of on an instance, as required by OP. Also, it makes sense to have it as static since the value will be same for all objects. – Bhushan Shah May 07 '14 at 12:18
1

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.

L-Four
  • 13,345
  • 9
  • 65
  • 109
0

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)

Xaruth
  • 4,034
  • 3
  • 19
  • 26