© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
4 replies
Matt

❔ Better way of handling this?

switch (syncMessage.type)
{
  case "Obj1": 
    Obj1.HandleUpdate(syncMessage.data);
    break;
  case "Obj2": 
    Obj2.HandleUpdate(syncMessage.data);
    break;
  case "Obj2": 
    Obj2.HandleUpdate(syncMessage.data); 
    break;
  default: 
    break;
}
switch (syncMessage.type)
{
  case "Obj1": 
    Obj1.HandleUpdate(syncMessage.data);
    break;
  case "Obj2": 
    Obj2.HandleUpdate(syncMessage.data);
    break;
  case "Obj2": 
    Obj2.HandleUpdate(syncMessage.data); 
    break;
  default: 
    break;
}

Is there a better way of handling this? The objects all inherit from the same base abstract class which contains the HandleUpdate method. The syncMessage.type is a string and I don't think I can change that (not 100% sure)
It feels like the strategy pattern might be what I'm after but this implementation already uses 3 classes per object and I'd rather not add more. Any ideas?

Edit: Cleaned code up to remove information irrelevant to the question
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

better way to do this?
C#CC# / help
3y ago
Is there a better way to do this
C#CC# / help
3y ago
Better way to check a list of characters?
C#CC# / help
2y ago
Compare Strings better way.
C#CC# / help
13mo ago