Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building Custom Int Struct (DateTime Struct to)

Status
Not open for further replies.

mbde

Programmer
Mar 14, 2005
55
US
I would like to build a custom Int struct that inheriets from Int, so that I can pass everything through but override the toString method.

What I have done in the pass...

I use customer business objects to store several ints and datetimes,

When loading these objects if the database returns null I would set the int = int.MinValue

On the GUI side when assigning the int to a textbox I would write a line like

textbox1.Text = (myObj.CustomerCount == int.MinValue) ? "" : myObj.CustomerCount.ToString();


It is the line above that I would like to but in a customer Int Struct so the users can just type

textbox1.Text = myObj.CustomerCount.ToString();

and it will do the type check in the overrided method.

I know you can not inherit from Int (or Int32) so this is where I am stuck. I would appreciate any help or steering in a better direction

I have done this in the past for a "unboundless" array where I inherit from ICollection

and then have
Code:
public string this[int index]
		{
			get
			{
				if(index > _arrayList.Count-1)
				{
					return null;
				}
				else
				{
					return _arrayList[index].ToString() == "" ? null : _arrayList[index].ToString();
				}
			}
		}
and
Code:
public static DateTime ConvertToDate(string field)
		{
			if (field != null && field.Length == 8) 
				return DateTime.Parse(field.Substring(0, 4) + "/" + field.ToString().Substring(4, 2) + "/" + field.Substring(6, 2));
			else
			{
				return new DateTime();
			}

		}

Thanks in advance.
 
You could always create another read-only property on your class called CustomerCountDisplayValue like this:

Code:
public string CustomerCountDisplayValue 
        {
            get
            {
                if (myObj.CustomerCount == int.MinValue) 
                    return "";
                else
                    return myObj.CustomerCount.ToString();
            }
        }
 
Thanks Aptitude

What I did was just bit the bullet and created a new class
with a int private variable and then just exposed it by creating a property InnerValue and override the ToString()

This causes the init and assignment to be re-written as such

NewInt customerCount = NewInt();
and
customerCount.InnerValue =5;

but the benefits pay off. It also allows me to init all new variables to MinValue instead of 0.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top