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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:04:26+00:00 2026-06-08T10:04:26+00:00

The constructor for my class has three variables passed to it: public MyClass(int Id,

  • 0

The constructor for my class has three variables passed to it:

public MyClass(int Id, String Name, DateTime StartDate)

But, StartDate might be passed into the variable as a String or as a DateTime object.

Should I create two different constructors one specifying DateTime for StartDate and one as String? Or, should I make the type Dynamic and determine what it is at run time and handle it then? I ask because my class has five dates and if I were to write constructors for every different combination, that would be way too much code.

  • 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-08T10:04:28+00:00Added an answer on June 8, 2026 at 10:04 am

    Define an overload of the constructor that calls your default constructor:

    class MyClass
    {
        public MyClass(int id, string name, DateTime startDate)
        {
        }
    
        public MyClass(int id, string name, string startDate)
            : this(id, name, DateTime.Parse(startDate))
        {
        }
    }
    

    I would only create a constructor that accepts all as DateTime objects and an overload that accepts all as strings. I would ignore any other combination. In fact, I would only accept DateTime.

    Accepting a dynamic type would result in a poor interface.

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

Sidebar

Related Questions

junit.framework.AssertionFailedError: Class com.android.deviceintelligence.test.Testshutdown has no public constructor TestCase(String name) or TestCase() at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) at
public class Module { // required instance variables private String codeModule, titleModule; private int
String class has a constructor: new String(byte[] bytes, Charset charset) and a method: byte[]
I have a class file called MarkerCustom. MarkerCustom has a constructor that takes three
If a class has a private constructor then it can't be instantiated. So, if
I recently created a class which has a constructor taking 3 enumerations as arguments.
I have a constructor (for an auto generated class) that has 255 paremeters. Using
I have a class that has several member classes as attributes. The constructor of
I have a service class which has overloaded constructors. One constructor has 5 parameters
boost::shared_ptr has an unusual constructor template<class Y> shared_ptr(shared_ptr<Y> const & r, T * p);

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.