0

I'm pretty new to C# and trying to make a method on how to change the name, address, etc. in a listview addressbook.

Have been scratching my head for a while on how to make the ".Name" part into a variable that can be passed through to the method as a parameter.

For my method to work, i need to be able to change the ".Name" part to: ".Address", ".Phone" and so on using parameters.

Box1 represents textboxes that are disabled and only show information about the selected item in the list view. Box2 represent the box in where the user can type the changes he wants to make.

The code is working, this is just to learn how to be more efficient and not duplicate.

private void CheckInput(string box1, string box2, string details)
    {
        if (box2 != box1 && box2 != "")                             
        {
            DialogResult dialogResult = MessageBox.Show(
                "Are you sure you want to change the " + details 
                + " from, " + box1 + ", to, " + box2 + "?", "Warning", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dialogResult == DialogResult.Yes)
            {
                people[listView1.SelectedItems[0].Index].Name = box2;

                if (box2 == "")
                {
                    listView1.SelectedItems[0].Text = box1;
                }
                else
                {
                    listView1.SelectedItems[0].Text = box2;
                }
            }
            if (dialogResult == DialogResult.No)
            {

            }
        }
    }

    class Person
    {
        public string Name
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
        public string Address
        {
            get;
            set;
        }
        public string Phone
        {
            get;
            set;
        }

    }
TheGeneral
  • 79,002
  • 9
  • 103
  • 141

1 Answers1

0

I would go with separate name and setter delegate:

private void CheckInput(string oldValue, string newValue,
          string propertyName, Action<string> setterDelegate)
{
    ...
    setterDelegate(newValue);
}

CheckInput("old", "new", nameof(value.Name), newVal => value.Name = newVal);

You can also use reflection if you want to stick with just name Set object property using reflection, but you'd still need to pass "Person" as argument. Another fancier option is to use Expression Tree similar how LINQ-to-SQL sets the queries and obtain name of the property that way - Retrieving Property name from lambda expression.

Note: generally frameworks provide they own mechanism for binding values and validation. If you need to practice reinventing the wheel look into annotating properties with attributes - Validation using attributes (be careful as it contains complete answer, so you may want to read on attributes in general first).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thank you for your quick answer. Your answer is a bit too complex for me to understand as I'm still very new to programming. If you have the time, could you please use the same names on the strings as i did or maybe explain your example code in more detail? – Tom Steele Feb 04 '18 at 23:38