-1

I have a dotnet core application. I added model class everytime with fixed prefix. It goes very lengthy and unhandled way. Is there any better way to handle this?

public class Category
{
    public string? gmailIN{ get; set; }

    public string? aolIN{ get; set; }
    public string? outlookIN{ get; set; }
    public string? yahooIN{ get; set; }
    public string? hotmailIN{ get; set; }
    public string? ZohoIN; set; }

    public string? aolAU{ get; set; }
    public string? outlookAU{ get; set; }
    public string? yahooAU{ get; set; }
    public string? hotmailAU{ get; set; }
    public string? ZohoAU{get; set; }
    public string? gmailAU{ get; set; }

    public string? aolUK{ get; set; }
    public string? outlookUK{ get; set; }
    public string? yahooUK{ get; set; }
    public string? hotmailUK{ get; set; }
    public string? ZohoUK{get; set; }
    public string? gmailUK{ get; set; }
        
    public string? gmailUS{ get; set; }
    public string? aolUS{ get; set; }
    public string? outlookUS{ get; set; }
    public string? yahooUS{ get; set; }
    public string? hotmailUS{ get; set; }
    public string? ZohoU{get; set; }
}

Prefix is always the same. Is there any better way to add variables instead of adding above way?

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
vct
  • 73
  • 1
  • 10
  • Editing multiple lines at a time will probably help you: https://stackoverflow.com/questions/42837208/how-to-edit-multiple-lines-in-visual-studio-2017-at-once – Timothy G. Aug 29 '22 at 14:38
  • 1
    Do all these properties need to remain in the same class? Does it even need to be properties? Is there a common pattern for determining a property value based on its name? How are you defining "better way"? – gunr2171 Aug 29 '22 at 14:39

1 Answers1

4

If possible split Category and Country.

You can create a class like:

public class Category
{
   public string? gmail{ get; set; }    
   public string? aol{ get; set; }
   public string? outlook{ get; set; }
   public string? yahoo{ get; set; }
   public string? hotmail{ get; set; }
   public string? Zoho{get; set; }
}

and then one Dictionary for mapping it with country

public Dictionary<string,Category> Categories;

or add the Country property inside the Category class itself.

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
Qwerty
  • 429
  • 2
  • 8