Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make class in C#, that can be cast to DateTime?

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
like image 621
Evgeny Ekim Avatar asked Apr 12 '12 10:04

Evgeny Ekim


People also ask

Can you declare a class in C?

C ClassesA class consists of an instance type and a class object: An instance type is a struct containing variable members called instance variables and function members called instance methods.

How class are created?

A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end. Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated.

What Is syntax to create class?

The basic syntax is: class MyClass { // class methods constructor() { ... } method1() { ... }

Can you create objects in C?

In terms of C programming, an object is implemented as a set of data members packed in a struct , and a set of related operations. With multiple instances, the data for an object are replicated for each occurrence of the object.


1 Answers

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.

like image 111
Adam Houldsworth Avatar answered Sep 28 '22 02:09

Adam Houldsworth