IngTun2018
IngTun2018

Reputation: 329

C# Loop through an enum and a list of properties in a class

I have an enum DescriptionType and a class WsDescriptionBofcObject which has 12 properties. Another class User contains the WsDescriptionBofcObject class.

My objective is to set to every property of the WsDescriptionBofcObject class from another result.

Here is my code:

public class User
{
   [DataMember]
   public WsDescriptionBofcObject WsObjectDescriptions { get; set; }
   [DataMember]
   public int id { get; set; }
   [DataMember]
   public string  name  { get; set; }
}

[DataContract]
public class WsDescriptionBofcObject
{
    [DataMember]
    public string ShortDesc1;
    [DataMember]
    public string ShortDesc2;
    [DataMember]
    public string ShortDesc3;
    [DataMember]
    public string ShortDesc4;
    [DataMember]
    public string ShortDesc5;
    [DataMember]
    public string ShortDesc6;
    [DataMember]
    public string LongDesc1;
    [DataMember]
    public string LongDesc2;
    [DataMember]
    public string LongDesc3;
    [DataMember]
    public string LongDesc4;
    [DataMember]
    public string LongDesc5;
    [DataMember]
    public string LongDesc6;
}

public enum DescriptionType
{
    Long = 0,
    Short = 1,
    xxx = 2,
    yyy = 3
}

The code for set the values to all these properties:

     private List<User> listUsers = new List<User>();     
     User userTemp = new User();
     foreach (var itemsyUser in collectionSyUser)
     {              

         userTemp.WsObjectDescriptions.ShortDesc1 = itemsyUser.Description(DescriptionType.Short, WorkingLanguage.Lang1);
         userTemp.WsObjectDescriptions.ShortDesc2 = itemsyUser.Description(DescriptionType.Short, WorkingLanguage.Lang2);
         userTemp.WsObjectDescriptions.ShortDesc3 = itemsyUser.Description(DescriptionType.Short, WorkingLanguage.Lang3);
         userTemp.WsObjectDescriptions.ShortDesc4 = itemsyUser.Description(DescriptionType.Short, WorkingLanguage.Lang4);
         userTemp.WsObjectDescriptions.ShortDesc5 = itemsyUser.Description(DescriptionType.Short, WorkingLanguage.Lang5);
         userTemp.WsObjectDescriptions.ShortDesc6 = itemsyUser.Description(DescriptionType.Short, WorkingLanguage.Lang6);

         userTemp.WsObjectDescriptions.LongDesc1 = itemsyUser.Description(DescriptionType.Long, WorkingLanguage.Lang1);
         userTemp.WsObjectDescriptions.LongDesc2 = itemsyUser.Description(DescriptionType.Long, WorkingLanguage.Lang2);
         userTemp.WsObjectDescriptions.LongDesc3 = itemsyUser.Description(DescriptionType.Long, WorkingLanguage.Lang3);
         userTemp.WsObjectDescriptions.LongDesc4 = itemsyUser.Description(DescriptionType.Long, WorkingLanguage.Lang4);
         userTemp.WsObjectDescriptions.LongDesc5 = itemsyUser.Description(DescriptionType.Long, WorkingLanguage.Lang5);
         userTemp.WsObjectDescriptions.LongDesc6 = itemsyUser.Description(DescriptionType.Long, WorkingLanguage.Lang6);

         listUsers.Add(userTemp);
         userTemp = new User();

     }

As you can see here I have a lot of repetition, and I am confused to create a loop to do all this insertion. So how can I make a loop to do my insertion? Any advice? Thanks.

Upvotes: 0

Views: 98

Answers (3)

MRD79
MRD79

Reputation: 36

As answered before, you could use reflection for this. But I would not.

Using reflection here would generate complicated and messy code. It's not that bad to have to repeat the code for each property. It's the case that developers hate: almost the same but not quite ^-^

Your actual code is simple and easy to read (and this is always the sign that the code is good). As a bonus, accessing the members directy in code (not through reflection) allows you to search through references where your member is set.

Upvotes: 1

Michał Turczyn
Michał Turczyn

Reputation: 37337

You should encapsulate such code in mapping function, so you can then use it instead of writing it all again and again. Moreover, this algorithm doesn't have repetitions, it's just looks "big":

public WsDescriptionBofcObject MapToWsDescriptionBofcObject(object mapFrom)
{
    return new WsObjectDescriptions()
    {
        ShortDesc1 = mapFrom.Description(DescriptionType.Short, WorkingLanguage.Lang1);
        ShortDesc2 = mapFrom.Description(DescriptionType.Short, WorkingLanguage.Lang2);
        ShortDesc3 = mapFrom.Description(DescriptionType.Short, WorkingLanguage.Lang3);
        ShortDesc4 = mapFrom.Description(DescriptionType.Short, WorkingLanguage.Lang4);
        ShortDesc5 = mapFrom.Description(DescriptionType.Short, WorkingLanguage.Lang5);
        ShortDesc6 = mapFrom.Description(DescriptionType.Short, WorkingLanguage.Lang6);

        LongDesc1 = mapFrom.Description(DescriptionType.Long, WorkingLanguage.Lang1);
        LongDesc2 = mapFrom.Description(DescriptionType.Long, WorkingLanguage.Lang2);
        LongDesc3 = mapFrom.Description(DescriptionType.Long, WorkingLanguage.Lang3);
        LongDesc4 = mapFrom.Description(DescriptionType.Long, WorkingLanguage.Lang4);
        LongDesc5 = mapFrom.Description(DescriptionType.Long, WorkingLanguage.Lang5);
        LongDesc6 = mapFrom.Description(DescriptionType.Long, WorkingLanguage.Lang6);
    };
}

Note that you need to change parameter type and name appropriately :)

Then your code becomes clean:

private List<User> listUsers = new List<User>();     

foreach (var itemsyUser in collectionSyUser)
    listUsers.Add(new User()
    { 
        WsObjectDescriptions = MapToWsDescriptionBofcObject(itemsyUser) 
    });

Or even:

private List<User> listUsers = collectionSyUser
  .Select(itemsyUser => new User() {WsObjectDescriptions = MapToWsDescriptionBofcObject(itemsyUser) })
  .ToList();

You also could take a look at AutoMapper, which is great with such tasks :)

Upvotes: 1

Piotr Szuflicki
Piotr Szuflicki

Reputation: 84

You should use the reflections on that one. Using that library, you can get all properties and fields in class, and change values of them. Then in loop, just use SetValue method. The Enum values, are represented by byte, so that shouldn't be problem.

Upvotes: 0

Related Questions