Domanda

I would like to create the function and pass the values to reduce the duplicate codes in my coding.

It will be called like this in GridView ItemDataBound event

DataRowView drv = e.Row.DataItem as DataRowView;
MyFunc(drv["HasDonePart1"]);
MyFunc(drv["HasDonePart2"]);
MyFunc(drv["HasDonePart3"]);
etc...

The function will be like the following

private void MyFunc ((ClassName) item)
{
    if (item == DBNULL.value)
    {
       //do something
    }
    else
    {
       string myValue = item.ToString();
       //do other things
    }
}

But I don't know what should I put in my method to accept the object.

È stato utile?

Soluzione

Change MyFunc to accept object, instead:

private void MyFunc(object item)
{
    if (item == DBNull.Value)
    {
        // Do something
    }
    else if (item is string)
    {
    }
    else if (item is decimal)
    {
    }

    // etc.
}

Altri suggerimenti

DataRowView.Item indexed property returns object, so that's what your method should take as parameter. See http://msdn.microsoft.com/en-us/library/0k5w061k(v=vs.110).aspx

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top