-2

Prior to this approach, I had some methods that included four or five parameters, so I want to condense the majority of them into an object. The TestOptions() object has additional optional values, like 'name' or 'location'.

I'm having trouble retrieving the object values individually. How can I use the specified values from the TestOptions() object in the Setup() method?

    public async Task Test_One()
    {
        await Setup(new TestOptions() { brand = "Brand1", id = 10 }, new List<string> { "user1@abc.com" });
    }

    public async Task Setup(object values, List<string> emailAddresses)
    {
        //Do work here that uses 'brand' and 'id' individually
    }

public class TestOptions
{
    public string brand
    {
        get; set;
    }

    public string id
    {
        get; set;
    }
}

Thank you.

Dan
  • 29
  • 6

2 Answers2

2

You can either make the signature for Setup take a strongly typed object:

public async Task Setup(TestOptions values, List<string> emailAddresses)
{
    //Do work here that uses 'brand' and 'id' individually
    var brand = values.brand;
}

Or cast the value:

public async Task Setup(object values, List<string> emailAddresses)
{
     //Do work here that uses 'brand' and 'id' individually
     var typed = (TestObject)values;
     var brand = typed .brand;
}
RQDQ
  • 15,461
  • 2
  • 32
  • 59
  • 1
    Thanks so much, your first option worked perfectly. I'm self-taught, and still pretty new, so the simple answer is much appreciated! – Dan Dec 05 '18 at 17:25
0

I dont know if I have undestood your question, but if you want to retrieve the properties of TestOptions throw a object variable, you need to cast it first, something like this:

string brand = ((TestOptions)values).brand;
string id = ((TestOptions)values).id;
// etc...

Anyway, I recommend you to change your method by receiving a TestOption value instead of a generic object, or maybe creating diferent implementations of the same method for each diferent object.

Ilconzy
  • 21
  • 1