Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a custom Object, called fileSize and it holds two variables. In the same way a string's value can be String.Empty(). I would like to do the same with my object, to assign fileSize.Empty to a variable, but not sure how. The code below shows the custom object class.
C#
public class fileSize
{
    int minSize;
    int maxSize;

    public  fileSize(int MinSize, int MaxSize)
    {
        minSize=MinSize;
        maxSize=MaxSize;
    }
}



Any help's appreciated.
Posted

It's interesting to think about the "edge case" where the user creates an instance of the 'fileSize' object, supplying one valid int, and the other argument null: what's the right thing to do in that case ?

Should you throw an error in the constructor in that case ?

Here's some ideas:
C#
public class fileSize
{
    int? minSize;
    int? maxSize;

    public bool IsEmpty { get; set; }

    // 'empty' constructor
    public fileSize()
    {
        minSize = null;
        maxSize = null;
        IsEmpty = true;
    }

    // potentially 'non-empty' constructor
    public fileSize(int? MinSize, int? MaxSize)
    {
        minSize = MinSize;
        maxSize = MaxSize;

        // is this the logic you really want here ?
        IsEmpty = minSize == null || maxSize == null;
    }
}
Tests:
fileSize f1 = new fileSize();
Console.WriteLine(f1.IsEmpty);

fileSize f2 = new fileSize(null,null);
Console.WriteLine(f2.IsEmpty);

fileSize f3 = new fileSize(100, 200);
Console.WriteLine(f3.IsEmpty);

fileSize f4 = new fileSize(100, null);
Console.WriteLine(f4.IsEmpty);
 
Share this answer
 
create nullables int? minSize and int? maxSize

a private bool empty
a public bool Empty
C#
public bool Empty {
get { return minSize == null && maxSize == null; } 
}
 
Share this answer
 
v2
Add something like that ::
C#
<code>
public static fileSize Empty{
get { return new fileSize(0,0); }
}
</code>


:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900