Click here to Skip to main content
15,897,519 members
Home / Discussions / C#
   

C#

 
AnswerRe: Change a version of pdf Pin
Dave Kreskowiak9-Mar-09 4:43
mveDave Kreskowiak9-Mar-09 4:43 
GeneralRe: Change a version of pdf Pin
DaveyM699-Mar-09 5:13
professionalDaveyM699-Mar-09 5:13 
GeneralRe: Change a version of pdf Pin
Dave Kreskowiak9-Mar-09 7:53
mveDave Kreskowiak9-Mar-09 7:53 
JokeRe: Change a version of pdf Pin
DaveyM699-Mar-09 10:17
professionalDaveyM699-Mar-09 10:17 
Questionc# Pin
Amin.mosbat9-Mar-09 0:53
Amin.mosbat9-Mar-09 0:53 
AnswerRe: c# Pin
dan!sh 9-Mar-09 0:59
professional dan!sh 9-Mar-09 0:59 
AnswerRe: c# Pin
Paw Jershauge9-Mar-09 1:39
Paw Jershauge9-Mar-09 1:39 
QuestionCreating you own Nullable<t></t> Pin
Paw Jershauge9-Mar-09 0:25
Paw Jershauge9-Mar-09 0:25 
Im not sure if this can be done.

But i want to create my on Nullable<t> struct.
my problem is that i cant assign null to my struct as it is an non-nullable type, how can i overcome this ?

Heres my attemt:

[Serializable, StructLayout(LayoutKind.Sequential)]
public struct 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 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;
    }
}


This is all fine but when i then try to use it i get an non-nullable type exception:

Example:
private Null<int> index = null;

the null; assignment returns the following error:
Error 4 Cannot convert null to 'Null<int>' because it is a non-nullable value type

Can this be done,if so HELP please, or am i on an wrong track here ?

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

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 
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 

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.