Click here to Skip to main content
15,891,136 members
Home / Discussions / C#
   

C#

 
QuestionCreating you own Nullable<t></t> Pin
Paw Jershauge9-Mar-09 0:25
Paw Jershauge9-Mar-09 0:25 
AnswerRe: Creating you own Nullable Pin
ABitSmart9-Mar-09 0:39
ABitSmart9-Mar-09 0:39 
GeneralRe: Creating you own Nullable Pin
Paw Jershauge9-Mar-09 1:19
Paw Jershauge9-Mar-09 1:19 
GeneralRe: Creating you own Nullable Pin
ABitSmart9-Mar-09 1:29
ABitSmart9-Mar-09 1:29 
AnswerRe: Creating you own Nullable [modified] Pin
harold aptroot9-Mar-09 0:51
harold aptroot9-Mar-09 0:51 
GeneralRe: Creating you own Nullable Pin
Paw Jershauge9-Mar-09 1:26
Paw Jershauge9-Mar-09 1:26 
GeneralRe: Creating you own Nullable Pin
harold aptroot9-Mar-09 1:32
harold aptroot9-Mar-09 1:32 
GeneralRe: Creating you own Nullable Pin
Paw Jershauge9-Mar-09 1:36
Paw Jershauge9-Mar-09 1:36 
Yeah your right... they have the advantage here Wink | ;)

Yes the casting on an object works bacause the object in its nature can be null.

So heres the code which works just fine (BUT ITS AN CLASS... grrrrrrrr, i wanted an struct. Wink | ;) )

[Serializable, StructLayout(LayoutKind.Sequential)]
public class Null<T> where T : struct
{
    private bool hasValue;
    private bool allowNull;
    internal T value;
    public Null(T value)
    {
        this.value = value;
        this.hasValue = true;
        this.allowNull = true;
    }
    public Null(T value,bool allownull)
    {
        this.value = value;
        this.hasValue = true;
        this.allowNull = allownull;
    }
    public Null(bool allownull)
    {
        this.hasValue = false;
        this.allowNull = allownull;
    }
    public Null()
    {
        this.hasValue = false;
        this.allowNull = false;
    }

    public bool HasValue
    {
        get
        {
            return this.hasValue;
        }
    }
    public bool AllowNull
    {
        get
        {
            return this.allowNull;
        }
    }
    public T Value
    {
        get
        {
            if (!this.HasValue)
            {
                throw new InvalidOperationException("No Value!");
            }
            return this.value;
        }
    }
    public T GetValueOrDefault()
    {
        return this.value;
    }

    public T GetValueOrDefault(T defaultValue)
    {
        if (!this.HasValue)
        {
            return defaultValue;
        }
        return this.value;
    }

    public override bool Equals(object other)
    {
        if (!this.HasValue)
        {
            return (other == null);
        }
        if (other == null)
        {
            return false;
        }
        return this.value.Equals(other);
    }

    public override int GetHashCode()
    {
        if (!this.HasValue)
        {
            return 0;
        }
        return this.value.GetHashCode();
    }

    public override string ToString()
    {
        if (!this.HasValue)
        {
            return "";
        }
        return this.value.ToString();
    }

    public static implicit operator Null<T>(T value)
    {
        return new Null<T>(value);
    }

    public static explicit operator T(Null<T> value)
    {
        return value.Value;
    }
}


With great code, comes great complexity, so keep it simple stupid...Shucks | :-\ Shucks | :-\

GeneralRe: Creating you own Nullable Pin
harold aptroot9-Mar-09 1:41
harold aptroot9-Mar-09 1:41 
GeneralRe: Creating you own Nullable Pin
Paw Jershauge9-Mar-09 1:50
Paw Jershauge9-Mar-09 1:50 
GeneralRe: Creating you own Nullable Pin
harold aptroot9-Mar-09 2:14
harold aptroot9-Mar-09 2:14 
GeneralRe: Creating you own Nullable Pin
Paw Jershauge9-Mar-09 2:55
Paw Jershauge9-Mar-09 2:55 
GeneralRe: Creating you own Nullable Pin
harold aptroot9-Mar-09 2:59
harold aptroot9-Mar-09 2:59 
GeneralRe: Creating you own Nullable Pin
Paw Jershauge9-Mar-09 3:38
Paw Jershauge9-Mar-09 3:38 
AnswerRe: Creating you own Nullable Pin
DaveyM699-Mar-09 2:22
professionalDaveyM699-Mar-09 2:22 
GeneralRe: Creating you own Nullable Pin
Paw Jershauge9-Mar-09 2:57
Paw Jershauge9-Mar-09 2:57 
QuestionPause, Resume Copy of file... How (C#)? Pin
NeCroFire9-Mar-09 0:08
NeCroFire9-Mar-09 0:08 
AnswerRe: Pause, Resume Copy of file... How (C#)? Pin
dan!sh 9-Mar-09 0:26
professional dan!sh 9-Mar-09 0:26 
GeneralRe: Pause, Resume Copy of file... How (C#)? Pin
NeCroFire9-Mar-09 3:35
NeCroFire9-Mar-09 3:35 
Questionlines disappear on image resize Pin
grizli9-Mar-09 0:07
grizli9-Mar-09 0:07 
AnswerRe: lines disappear on image resize Pin
Luc Pattyn9-Mar-09 2:31
sitebuilderLuc Pattyn9-Mar-09 2:31 
QuestionBitmaps on button Pin
saksp8-Mar-09 23:59
saksp8-Mar-09 23:59 
AnswerRe: Bitmaps on button Pin
dan!sh 9-Mar-09 0:02
professional dan!sh 9-Mar-09 0:02 
QuestionC# Coding Pin
Digubha8-Mar-09 23:34
Digubha8-Mar-09 23:34 
AnswerRe: C# Coding Pin
Christian Graus8-Mar-09 23:40
protectorChristian Graus8-Mar-09 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.