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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:32:20+00:00 2026-06-01T18:32:20+00:00

How can I make class, that can be cast to DateTime. But I need

  • 0

How can I make class, that can be cast to DateTime. But I need to cast my class, when it packed. For example:

object date1 = new MyDateTime();
DateTime date2 = (DateTime)date1;

I need directly this working example.

I know how to do it, but my way will work without packing. I’m not sure is there way to do it.

Please, help.

PS. I need cast directly object to DateTime. So, MyDateTime have to be packed before. Explicit works well, but it doesn’t help if you have packed object. And it have to cast just using ordinary casting like

(DateTime) (object) MyDateTime
  • 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-01T18:32:21+00:00Added an answer on June 1, 2026 at 6:32 pm

    What you appear to be after is inheritance, being able to “store” a derived class instance in a variable of the base type like so:

    Stream s = new FileStream();
    

    The fact that it is a FileStream under the hood is not lost just because you are pointing to it with the Stream goggles on.

    DateTime is a struct, and struct inheritance is not supported – so this is not possible.

    An alternative is the explicit keyword for user-defined conversions (syntactically looking like casts). This allows you to at least interchange between your class and DateTime with more sugar.

    This could look like:

    class MyDateTime
    {
        private DateTime _inner;
    
        public static explicit operator DateTime(MyDateTime mdt)
        {
            return mdt._inner;
        }
    }
    

    You can do the same with the counterpart implicit keyword:

    public static implicit operator DateTime(MyDateTime mdt)
    {
        return mdt._inner;
    }
    

    That then lets you do the “casting” implicitly:

    DateTime date = new MyDateTime();
    

    Another alternative is to wrap DateTime with your own adapter class that internally uses a DateTime and then inherit from this class to create MyDateTime. Then instead of using DateTime in your code base, you use this adapter class.

    I’ve seen similar things with SmartDateTime style classes where the DateTime has a better understanding of nulls and if it was set.

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

Sidebar

Related Questions

Given an instantiated JavaScript object/class can I make an Ajax call from that class
I know from Algebra class that with ABC and 123 we can make 216
I want to make a generic class that accepts only serializable classes, can it
I'm trying to make a Parallel Helper class ( ParallelRunner ) that can be
How do I cast an instance of an object and actually make it that
Can we make a class copy constructor virtual in C++? How to use?
How can I specify the position of a JOptionPane. Can anyone make a class
To make a class immutable what I can do is: 1)Make class final 2)do
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
How can make a link within a facebox window that redirects it to another

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.