0

Imagine I have created an object name of oItem. And this has a lot of attributes such as ItemCode, ItemName.....(around 300). And I want to assign new values to some of these attributes which were selected by the user of my application. User will give these attribute names as strings.

Ex: string attribute1 = "ItemCode".

Now what I want to do is assign a value to this attribute like:

oItem.attribute1 = "01234";

Is there a way to do something like this? I know you can convert a c# function call to a string. Therefore I think this should be possible too. Any help would be highly appreciated. Thanks!

UPDATE: This is a part of my SAP add-On. So these attributes are from a database table. The hard part is user can add more columns(user defined fields) to this which increase the number of attributes as well as I only know the original 300 attributes.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Isuru
  • 430
  • 5
  • 21

3 Answers3

1

If you have 300 properties you should really refactor this class. I think that you can use a Dictionarystring, string> in this case.

Dictionary<string, string> Items = new Dictionary<string, string> 
{
    {"attribute1", "01234"}, {"attribute2", "56789"}, {"attribute3", "76543"}, // ...
};

You can access the values very efficiently:

string attribute1 = Items["attribute1"];

or add/modify them in a similar way:

Items["attribute4"] = "23456";
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @TimSchmelter I can't re-factor this class because I'm programming an addOn to SAP b1. And oitem is an instance of Item Master Data – Isuru Nov 24 '15 at 09:39
  • @HimBromBeere: what do you mean? If OP could replace the properties with a single dictionary this approach would allow to use one property for infinite attributes. – Tim Schmelter Nov 24 '15 at 09:42
  • Obv. OP can´t (for whatever reason) refactor, so your approach is more or less meaningless. – MakePeaceGreatAgain Nov 24 '15 at 09:48
  • @HimBromBeere: he didn't tell it in the question and even if it's meaningless for him it might be helpful for others with 300 properties. Using reflection should be the last-ditch attempt. But maybe it's OP's only viable approach. – Tim Schmelter Nov 24 '15 at 09:50
0

To make it easier for you there's a "dynamic" keyword in C#. A sample from msdn dynamic object article:

// The class derived from DynamicObject.
public class DynamicDictionary : DynamicObject
{
    // The inner dictionary.
    Dictionary<string, object> dictionary
        = new Dictionary<string, object>();

    // This property returns the number of elements
    // in the inner dictionary.
    public int Count
    {
        get
        {
            return dictionary.Count;
        }
    }

    // If you try to get a value of a property 
    // not defined in the class, this method is called.
    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        // Converting the property name to lowercase
        // so that property names become case-insensitive.
        string name = binder.Name.ToLower();

        // If the property name is found in a dictionary,
        // set the result parameter to the property value and return true.
        // Otherwise, return false.
        return dictionary.TryGetValue(name, out result);
    }

    // If you try to set a value of a property that is
    // not defined in the class, this method is called.
    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        // Converting the property name to lowercase
        // so that property names become case-insensitive.
        dictionary[binder.Name.ToLower()] = value;

        // You can always add a value to a dictionary,
        // so this method always returns true.
        return true;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Creating a dynamic dictionary.
        dynamic person = new DynamicDictionary();

        // Adding new dynamic properties. 
        // The TrySetMember method is called.
        person.FirstName = "Ellen";
        person.LastName = "Adams";

        // Getting values of the dynamic properties.
        // The TryGetMember method is called.
        // Note that property names are case-insensitive.
        Console.WriteLine(person.firstname + " " + person.lastname);

        // Getting the value of the Count property.
        // The TryGetMember is not called, 
        // because the property is defined in the class.
        Console.WriteLine(
            "Number of dynamic properties:" + person.Count);

        // The following statement throws an exception at run time.
        // There is no "address" property,
        // so the TryGetMember method returns false and this causes a
        // RuntimeBinderException.
        // Console.WriteLine(person.address);
    }
}

// This example has the following output:
// Ellen Adams
// Number of dynamic properties: 2
norekhov
  • 3,915
  • 25
  • 45
0

It is really easy to do this for SAP B1 addon using c# with the provided library functions. Simply include using SAPbobsCOM;

And then you can do it as follows

yourobject.UserFields.Fields.Item("attribute").Value = "value";
Isuru
  • 430
  • 5
  • 21