Click here to Skip to main content
15,905,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a windows forms application in C# 2008.
I have a datatable in memory with persons.
Fields like sex, firstname, lastname, partner name,nameto use etc.
I want to create an email:

Dear Mr Johnston, 
We would like to check our data:
Your first name: Leo


Etc.

The sex is stored as 1 and 0, so I created a function:
if(sex = 0, "Mr", "Mrs")

But this becomes very complicated and has to be created for every mail.

So I use placeholders like:
Dear %sexU% %LastnameUse%
%sexU% means sex written out with first capital letter.
%LastnameUse% means the last name the person wants to use (eg married women)


The disadvantage is that I have to check the code to know what placeholders can be used, so I tried to make a table of placeholders:

SexU       iif(datarow[Sex] == 1, "Mr", "Mrs")
LastnameUse iif(datarow[partner name] != "", Partner name, Last name)


The problem is: how can I execute the string contained in this table as a function?
I've tried generics delegates, but I couldn't get it to work.

I don't know where to search so a pointer in the correct direction could be all that is necessary!

Thanks

Rob


Lets say I want to make a function:
C#
///FunctionDescription can be: "iif(sex==1, "Mr", "Mrs"); 
///FunctionDescription can be: "iif(datarow[partner name] != "", Partner name, Last name)"
///sex, partnername, lastname are all fields in datatable person
private string ReplaceFields(datatable person, string functiondescription)
{
    return execute(function); 
}
Posted
Updated 8-Dec-11 19:01pm
v4
Comments
[no name] 8-Dec-11 4:51am    
not so clear.
RobScripta 8-Dec-11 6:08am    
You're right, but I don't know how to make it clearer.

And I've made a mistake: i've tried delegates (and nog generics as stated in my question).

Short version of my question: I want a table with functions that return strings, and I want to be able to execute these functions.

Rob
[no name] 8-Dec-11 21:30pm    
EDIT: added "pre" tag

Hi,

you can create one more table for keyValue pair for such SexU and LastNameUse.

there will be three column
1)which field you are looking for ex:SexU
2)key value you need to search for ex : 1,0
3)corresponding value that you need to replace ex: Mr,Mrs

from this way you may create one generic function.

hope this information helpful to you,

thanks
-amit.
 
Share this answer
 
Comments
RobScripta 8-Dec-11 6:06am    
I can see how this works for sex, but I also showed my problem with LastNameUse.
Depending whether a partner name is available, the last name used is the own name or the name of the partner, and I can't think of a way to do that in your proposed way.

I'm really looking for something like
LastName = convertstringtofunction(string);
Rob
AmitGajjar 8-Dec-11 9:39am    
Hi,

ok, so you can do one more thing in the value column add your StoredProcedure name instead of static string. so depending on key value corresponding stored procedure will call and return your required string.by that way you can create your generic function.

now is that work for you ?
My question wasn't very clear because I didn't know what I was looking for, but reflection was the answer!

C#
    private void button1_Click(object sender, EventArgs e)
    {

        DataTable dtPerson = new DataTable("Person");
        dtPerson.Columns.Add(new DataColumn("LastName", typeof(string)));
        dtPerson.Columns.Add(new DataColumn("FirstName", typeof(string)));
        dtPerson.Columns.Add(new DataColumn("PartnerName", typeof(string)));
        dtPerson.Rows.Add(new object[] { "Johnson", "John", "" });
        dtPerson.Rows.Add(new object[] { "Rosevelt", "Mary", "Johnson" });
        dtPerson.Rows.Add(new object[] { "Clinton", "Bill", "" });

        string[] Fields = { "LastName", "FirstName", "PartnerName", "UseName" };

        DelegateDemo myClass = new DelegateDemo();
        for (int j = 0; j < dtPerson.Rows.Count; j++)
        {
            DataRow drPerson = dtPerson.Rows[j];
            object[] mParam = new object[] { drPerson };
            for (int i = 0; i <= Fields.GetUpperBound(0); i++)
            {
                System.Reflection.MethodInfo myMethodInfo = myClass.GetType().GetMethod(Fields[i]);
                MessageBox.Show(myMethodInfo.Invoke(myClass, mParam).ToString());
            }
        }
    }
}

class DelegateDemo
{
    public virtual string LastName(DataRow drPerson)
    {
        return drPerson["LastName"].ToString();
    }

    public virtual string FirstName(DataRow drPerson)
    {
        return drPerson["FirstName"].ToString();
    }

    public virtual string PartnerName(DataRow drPerson)
    {
        return drPerson["PartnerName"].ToString();
    }

    public virtual string UseName(DataRow drPerson)
    {
        if (drPerson["PartnerName"].ToString() == "")
        {
            return drPerson["LastName"].ToString();
        }
        else
        {
            return drPerson["PartnerName"].ToString();
        }
    }
}


Thanks for your efforts to help me!

Rob
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900