2

I'm starting my adventure with C# and don't know all the technics, but I already know what am I trying to achive:

public class Product
{
    public string Name { get; set; }
    public double Price { get; set; }
    //public ??? Category { get; set; }
}

'Category' type should be a custom type (?) which has 8 possible string values for names (Food, clothes etc) and icons specifically for those names (Food - apple.jpg, Clothes - tshirt.jpg and so on)

How do I do that?

rory.ap
  • 34,009
  • 10
  • 83
  • 174
bazeusz
  • 75
  • 1
  • 6

3 Answers3

4

Often, when working with fixed size categories (8 in your case) we use enum type:

  public enum ProductCategory {
    Food,
    Clothes,
    //TODO: put all the other categories here
  }

To add up icons, strings etc. we can implement extension methods:

  public static class ProductCategoryExtensions {
    // please, notice "this" for the extension method
    public static string IconName(this ProductCategory value) {
      switch (value) {
        case ProductCategory.Food:
          return "apple.jpg";
        case ProductCategory.Clothes:
          return "tshirt.jpg";
        //TODO: add all the other categories here

        default:
          return "Unknown.jpg"; 
      }
    }
  }

Finally

  public class Product {
    public string Name { get; set; }
    public double Price { get; set; } // decimal is a better choice
    public ProductCategory Category { get; set; }
  }

Usage

  Product test = new Product();

  test.Category = ProductCategory.Clothes;

  Console.Write(test.Category.IconName());
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You can define the Category class like the following with predefined values:

public class Category
{
    public static Category Food = new Category("Food", "apple.jpg");
    // rest of the predefined values

    private Category(string name, string imageName)
    {
        this.ImageName = imageName;
        this.Name = name;
    }

    public string Name { get; private set; }
    public string ImageName { get; private set; }
}

public class Product
{
     public string Name { get; set; }
     public double Price { get; set; }
     public Category Category { get; set; }
}

Then, in your code you can set a product like this:

var product = new Product {Name= "product1", Price=1.2, Category = Category.Food};
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
  • having 8 static members for the various categories in the class is not a good idea. – sinitram Apr 13 '17 at 11:38
  • @sinitram why not? – Massimiliano Kraus Apr 13 '17 at 11:39
  • @MassimilianoKraus because there are enums and they are great for lightweight state information. – sinitram Apr 13 '17 at 11:45
  • 1
    @sinitram here we have the need to handle not a single value but 2 strings for every `Category` instance: a `Name` and an `IconName` (kind of). How do you handle this with an enum? Through attributes and reflection? It's a more complicated way (both to implement and to understand)... and retrieving the values can be slower than querying simple properties... So I think it's more suitable a little custom class with 2 readonly properties. – Massimiliano Kraus Apr 13 '17 at 11:50
  • @MassimilianoKraus I agree with you. So I think the best solution would be a little custom class `Category` with the property `CategoryType Type` where `CategoryType` is an enum. – sinitram Apr 13 '17 at 12:18
  • @sinitram the OP is talking about predefined names and images, not only names. Enums will help you with names only. – Ofir Winegarten Apr 13 '17 at 12:27
  • @OfirWinegarten, yes. Thus as I wrote above using both enum and class is the best solution. – sinitram Apr 13 '17 at 12:37
  • @sinitram anyway an enum does not protect you against invalid value at all. If I remember well, when you have `enum E { One = 1, Two = 2 }`, you can write `E e = (E)14;` and no error is raised. So what's the difference in using a string chosen in a static list of available strings, or using an enum? What advantage do you see in an enum? Keep in mind that a string can contain white spaces, and other strange chars that an enum cannot. – Massimiliano Kraus Apr 13 '17 at 12:50
-3

So first of all you create a new Class, your custom type

public class Category {
  // I recommend to use enums instead of strings to archive this
  public string Name { get; set; }
  // Path to the icon of the category
  public string Icon { get; set; }
}

Now in your product, you can change the line commented out to:

// First Category is the type, the second one the Name
public Category Category { get; set; }

Now you can create a new Product with a category:

var category = new Product() {
  Name = "ASP.NET Application",
  Price = 500,
  Category = new Category() {
    Name = "Software",
    Icon = "Software.jpg"
  }
}

Now when you want to create another Product with another category, just repeat the proccess. You can also create an array of Categories and then use the array elements e.g. Category = Categories[3]. So you create one Category for food, one for Clothes etc, store them all in the array and use them for your products.

Larce
  • 841
  • 8
  • 17