Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:03:08+00:00 2026-05-31T01:03:08+00:00

I am trying to find a better way to handle some growing if constructs

  • 0

I am trying to find a better way to handle some growing if constructs to handle classes of different types. These classes are, ultimately, wrappers around disparate value types (int, DateTime, etc) with some additional state information. So the primary difference between these classes is the type of data they contain. While they implement generic interfaces, they also need to be kept in homogeneous collections, so they also implement a non-generic interface. The class instances are handled according to the type of data they represent and their propogation continues or doesn’t continue based on that.

While this is not necessarily a .NET or C# issue, my code is in C#.

Example classes:

interface ITimedValue {
 TimeSpan TimeStamp { get; }
}

interface ITimedValue<T> : ITimedValue {
 T Value { get; }
}

class NumericValue : ITimedValue<float> {
 public TimeSpan TimeStamp { get; private set; }
 public float Value { get; private set; }
}

class DateTimeValue : ITimedValue<DateTime> {
 public TimeSpan TimeStamp { get; private set; }
 public DateTime Value { get; private set; }
}

class NumericEvaluator {
 public void Evaluate(IEnumerable<ITimedValue> values) ...
}

I have come up with two options:

Double Dispatch

I recently learned of the Visitor pattern and its use of double dispatch to handle just such a case. This appeals because it would allow undesired data to not propogate (if we only want to handle an int, we can handle that differently than a DateTime). Also, the behaviors of how the different types are handled would be confined to the single class that is handling the dispatch. But there is a fair bit of maintenance if/when a new value type has to be supported.

Union Class

A class that contains a property for each value type supported could be what each of these classes store. Any operation on a value would affect the appropriate component. This is less complex and less maintenance than the double-dispatch strategy, but it would mean that every piece of data would propogate all the way through unnecessarily as you can no longer discriminate along the lines of “I don’t operate upon that data type”. However, if/when new types need to be supported, they only need to go into this class (plus whatever additional classes that need to be created to support the new data type).

class UnionData {
 public int NumericValue;
 public DateTime DateTimeValue;
}

Are there better options? Is there something in either of these two options that I did not consider that I should?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:03:09+00:00Added an answer on May 31, 2026 at 1:03 am

    method 1, using dynamic for double dispatch (credit goes to http://blogs.msdn.com/b/curth/archive/2008/11/15/c-dynamic-and-multiple-dispatch.aspx).
    Basically you can have your Visitor pattern simplified like this:

    class Evaluator {
     public void Evaluate(IEnumerable<ITimedValue> values) {
        foreach(var v in values)
        {
            Eval((dynamic)(v));
        }
     }
    
     private void Eval(DateTimeValue d) {
        Console.WriteLine(d.Value.ToString() + " is a datetime");
     }
    
     private void Eval(NumericValue f) {
        Console.WriteLine(f.Value.ToString() + " is a float");
     }
    
    }
    

    sample of usage:

    var l = new List<ITimedValue>(){
        new NumericValue(){Value= 5.1F}, 
        new DateTimeValue() {Value= DateTime.Now}};
    
    new Evaluator()
        .Evaluate(l);
           // output:
           // 5,1 is a float
           // 29/02/2012 19:15:16 is a datetime
    

    method 2 would use Union types in c# as proposed by @Juliet here (alternative implementation here)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

any better way to write this ? $(this).parent().parent().find( dd ul).toggle(); update.. I am trying
I am trying to find the best way to handle creating an ISO 8601
I'm trying to find a better way to check for file access in a
I'm trying to find a better way to express this in jade: p.author.alignleft |
I'm trying to find a better way in expressing my cucumbers so I am
I am trying to find a better way to gather multiple input. At the
I'm hopelessly trying to find a better way of filling a range contents. This
I'm trying to find out the best way to handle transactions at object level
I'm trying to find a better way to move my Expression Engine 2 sites
I am trying to find a better what to do this. In Spring a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.