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 7986905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:59:15+00:00 2026-06-04T11:59:15+00:00

What is the best solution to this problem? I’m trying to create a function

  • 0

What is the best solution to this problem? I’m trying to create a function that has several optional parameters of class types for which null is a meaningful value and cannot be used as a default. As in,

public void DoSomething(Class1 optional1, Class2 optional2, Class3 optional3)
    {
        if (! WasSpecified(optional1)) { optional1 = defaultForOptional1; }
        if (! WasSpecified(optional2)) { optional2 = defaultForOptional2; }
        if (! WasSpecified(optional3)) { optional3 = defaultForOptional3; }

        // ... do the actual work ...
    }

I can’t use Class1 optional1 = null because null is meaningful. I can’t use some placeholder class instance Class1 optional1 = defaultForOptional1 because of the compile-time constant requirement for these optional parameters I’ve come up with the following options:

  1. Provide overloads with every possible combination, which means 8 overloads for this method.
  2. Include a Boolean parameter for each optional parameter indicating whether or not to use the default, which I clutters up the signature.

Has anyone out there come up with some clever solution for this?

Thanks!

edit: I ended up writing a wrapper class for so I didn’t have to keep repeating Boolean HasFoo.

    /// <summary>
    /// A wrapper for variables indicating whether or not the variable has
    /// been set.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public struct Setable<T>
    {
        // According to http://msdn.microsoft.com/en-us/library/aa288208%28v=vs.71%29.aspx,
        // "[s]tructs cannot contain explicit parameterless constructors" and "[s]truct
        // members are automatically initialized to their default values."  That's fine,
        // since Boolean defaults to false and usually T will be nullable.

        /// <summary>
        /// Whether or not the variable was set.
        /// </summary>
        public Boolean IsSet { get; private set; }

        /// <summary>
        /// The variable value.
        /// </summary>
        public T Value { get; private set; }

        /// <summary>
        /// Converts from Setable to T.
        /// </summary>
        /// <param name="p_setable"></param>
        /// <returns></returns>
        public static implicit operator T(Setable<T> p_setable)
        {
            return p_setable.Value;
        }

        /// <summary>
        /// Converts from T to Setable.
        /// </summary>
        /// <param name="p_tee"></param>
        /// <returns></returns>
        public static implicit operator Setable<T>(T p_tee)
        {
            return new Setable<T>
            {
                IsSet = true
              , Value = p_tee
            };
        }
    }
  • 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-06-04T11:59:18+00:00Added an answer on June 4, 2026 at 11:59 am

    I would at least consider creating a new type for the parameter:

    public void DoSomething(DoSomethingOptions options)
    

    … where DoSomethingOptions could look like this:

    public class DoSomethingOptions
    {
        private Class1 class1;
        public bool HasClass1 { get; private set; }
    
        public Class1 Class1 
        {
            get { return class1; }
            set
            {
                class1 = value;
                HasClass1 = true;
            }
        }
    
        ... for other properties ...
    }
    

    Then you can call it with:

    DoSomething(new DoSomethingOptions { Class1 = null, Class2 = new Class2() });
    

    You don’t end up with an exponential set of overloads, and you can still call it reasonably compactly.

    This is similar to the approach that Process takes with ProcessStartInfo.

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

Sidebar

Related Questions

I've been stuck trying to arrive at a best solution for this for a
I have this problem and I don't know what is the best solution for
Looking for best solution for this problem ( Almost all the social networking sites
Edited: SOLUTION FOUND. This is strange and not the best solution, but I just
I am looking best solution in PHP for a search form which has multiple
What's the best solution for authoring HTML5 in Emacs? Is there a mode that
I have structure for storing callback function like this: template<class T> struct CommandGlobal :
I am currently trying to figure out the best way to create a .xls
this question is a extension of one made in Best Solution Architecture , basically
I can't come up with a solution for this problem. I have list of

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.