Searching a Better Solution with Delegates

Posted by spagetticode on Stack Overflow See other posts from Stack Overflow or by spagetticode
Published on 2011-03-02T15:01:12Z Indexed on 2011/03/02 15:25 UTC
Read the original article Hit count: 156

Filed under:
|

Hey All, I am a newbie in C# and curious about the better solution of my case.

I have a method which gets the DataTable as a parameter and creates a List with MyClass's variables and returns it.

public static List<Campaigns> GetCampaignsList(DataTable DataTable)
    {
        List<Campaigns> ListCampaigns = new List<Campaigns>();

        foreach (DataRow row in DataTable.Rows)
        {
            Campaigns Campaign = new Campaigns();

            Campaign.CampaignID = Convert.ToInt32(row["CampaignID"]);
            Campaign.CustomerID = Convert.ToInt32(row["CustomerID"]);
            Campaign.ClientID = Convert.ToInt32(row["ClientID"]);
            Campaign.Title = row["Title"].ToString();
            Campaign.Subject = row["Subject"].ToString();
            Campaign.FromName = row["FromName"].ToString();
            Campaign.FromEmail = row["FromEmail"].ToString();
            Campaign.ReplyEmail = row["ReplyEmail"].ToString();
            Campaign.AddDate = Convert.ToDateTime(row["AddDate"]);
            Campaign.UniqueRecipients = Convert.ToInt32(row["UniqueRecipients"]);
            Campaign.ClientReportVisible = Convert.ToBoolean(row["ClientReportVisible"]);
            Campaign.Status = Convert.ToInt16(row["Status"]);

            ListCampaigns.Add(Campaign);
        }

        return ListCampaigns;
    }

And one of my another DataTable method gets the DataTable from the database with given parameters. Here is the method.

public static DataTable GetNewCampaigns()
    {
        DataTable dtCampaigns = new DataTable();

        Campaigns Campaigns = new Campaigns();
        dtCampaigns = Campaigns.SelectStatus(0);

        return dtCampaigns;
    }

But the problem is that, this GetNewCampaigns method doesnt take parameters but other methods can take parameters. For example when I try to select a campaign with a CampaignID, I have to send CampaignID as parameter. These all Database methods do take return type as DataTable but different number of parameters.

public static DataTable GetCampaignDetails(int CampaignID)
    {
        DataTable dtCampaigns = new DataTable();

        Campaigns Campaigns = new Campaigns();
        dtCampaigns = Campaigns.Select(CampaignID);

        return dtCampaigns;
    }

At the end, I want to pass a Delegate to my first GetCampaignList Method as parameter which will decide which Database method to invoke. I dont want to pass DataTable as parameter as it is newbie programming.

Could you pls help me learn some more advance features. I searched over it and got to Func<> delegate but could not come up with a solution.

© Stack Overflow or respective owner

Related posts about c#

Related posts about delegates