How to add a new value into a object

Lim Soon Yi

The type of "input" is a object (local variable). I am trying to add some attribute into the object.

I am doing like this :-

object input = null;
if(input = conditionA)
input = new { placeholder = 'A' };
if(input = conditionB)
input = new { onchange = 'conditionB' };

So I am trying to make the input are able to have both attribute, if the input meet both requirement, the conditionB's input are able to include conditionA's input attribute, For example like this :-

object input = null;
if(input = conditionA)
input = new { placeholder = 'A' };
if(input = conditionB)
input = new { input + onchange = 'conditionB' };

The other way I am able to do is like this :-

object input = null;
if(input = conditionA)
input = new { placeholder = 'A' };
if(input = conditionB)
input = new { placeholer = 'A', onchange = 'conditionB' };

but I feel there might have a better way to do than the way I do.

Anyone who have experience, Please help me, thank you

there

Generally speaking using object means that things are not going well.

Let's try with a class.

class MyInput {
 public string Placeholder {get;set;}
 public string Onchange {get;set;}
}

It can be used as follows:

var input = new MyInput();

if (input = conditionA)
    input.Placeholder = "A";

if (input = conditionB)
    input.Onchange = 'conditionB';

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324194515&siteId=291194637