[Q]Custom data types in C#

S

shofu_au

Hi Readers,

I am Pascal programmer who is converting a program to C#.

I would like know how to define the following data types in C#.

Q1. A data type that can have a range 1..13

Q2. A data typ that is 40 bits - aka a five byte array.

I wish to be able to declare variables with these data types line int,
bool etc.

Thanks

Mark
 
C

Claes Wedin

Q1:
As I understand it, can't you just create a class derived from integer and
set the conditions you vant?

Q2: Same procedure but different datatype

//Claes
 
S

shofu_au

Claes,

Thanks for the pointer. Tried the following

public class myint : int
{
public const ushort MaxValue = 16;
public const ushort MinValue = 0;
}

But I am still missing something as I get an error about cannot derive
from sealed type int.

Mark.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Claes,

Thanks for the pointer. Tried the following

public class myint : int
{
public const ushort MaxValue = 16;
public const ushort MinValue = 0;
}

But I am still missing something as I get an error about cannot derive
from sealed type int.

Mark.

You can't inherit from the simple data types. You have to encapsulate it
inside the structure.

public struct LimitedInt {

private int _value;

public const int MinValue = 0;
public const int MaxValue = 16;

public LimitedInt(int value) {
if (value < this.MinValue) {
throw new ArgumentException("Error message...");
}
if (value > this.MaxValue) {
throw new ArgumentException("Error message...");
}
_value = value;
}

public static implicit operator LimitedInd(int value) {
return new LimitedInt(value);
}

public static implicit operator int(LimitedInt limited) {
return limited._value;
}

}
 
L

Laura T.

You can't derive from from sealed classes or, in this case, from value types
(structs).
System.Int32 is a struct.

We use this kind of types in out apps, hope it helps:

struct RangeInt0to13 : IComparable, IFormattable, IConvertible,
IComparable<int>, IEquatable<int>
{
private int thisValue;
public const int MinValue = 0;
public const int MaxValue = 13;

private RangeInt0to13(int Value)
{
if (Value >= MinValue && Value <= MaxValue)
thisValue = Value;
else
throw new ArgumentOutOfRangeException("This type accepts
values from "+MinValue+" to " + MaxValue);
}

#region ObjectOverrides
public override int GetHashCode()
{
return thisValue.GetHashCode();
}

public override bool Equals(object obj)
{
return thisValue.Equals(obj);
}
#endregion

#region Helpers
public static RangeInt0to13 Parse(string s)
{
return new RangeInt0to13(int.Parse(s));
}

public static RangeInt0to13 Parse(string s, IFormatProvider
provider)
{
return new RangeInt0to13(int.Parse(s,provider));
}

public static RangeInt0to13 Parse(string s, NumberStyles style)
{
return new RangeInt0to13(int.Parse(s,style));
}

public static RangeInt0to13 Parse(string s, NumberStyles style,
IFormatProvider provider)
{
return new RangeInt0to13(int.Parse(s, style,provider));
}

public static bool TryParse(string s, out RangeInt0to13 result)
{
int tempInt;
bool retVal=int.TryParse(s, out tempInt);
result = default(int);

if (retVal == true)
{
try { result = new RangeInt0to13(tempInt); }
catch (ArgumentOutOfRangeException) { retVal = false; }
}

return retVal;
}

public static bool TryParse(string s, NumberStyles style,
IFormatProvider provider, out RangeInt0to13 result)
{
int tempInt;
result = default(int);
bool retVal = int.TryParse(s, style, provider, out tempInt);

if (retVal == true)
{
try { result = new RangeInt0to13(tempInt); }
catch (ArgumentOutOfRangeException) { retVal = false; }
}

return retVal;
}

#endregion

#region Conversions

public static implicit operator RangeInt0to13(int NewValue)
{
return new RangeInt0to13(NewValue);
}

public static implicit operator int(RangeInt0to13 NewValue)
{
return
NewValue.ToInt32(System.Globalization.CultureInfo.CurrentCulture.NumberFormat);
}

public override string ToString()
{
return thisValue.ToString();
}
#endregion


#region IEquatable<int> Members

public bool Equals(int other)
{
return thisValue.Equals(other);
}

#endregion

#region IComparable<int> Members

public int CompareTo(int other)
{
return thisValue.CompareTo(other);
}

#endregion

#region IConvertible Members

public TypeCode GetTypeCode()
{
return thisValue.GetTypeCode();
}

public bool ToBoolean(IFormatProvider provider)
{
return Convert.ToBoolean(thisValue, provider);
}

public byte ToByte(IFormatProvider provider)
{
return Convert.ToByte(thisValue, provider);
}

public char ToChar(IFormatProvider provider)
{
return Convert.ToChar(thisValue, provider);
}

public DateTime ToDateTime(IFormatProvider provider)
{
return Convert.ToDateTime(thisValue, provider);
}

public decimal ToDecimal(IFormatProvider provider)
{
return Convert.ToDecimal(thisValue, provider);
}

public double ToDouble(IFormatProvider provider)
{
return Convert.ToDouble(thisValue, provider);
}

public short ToInt16(IFormatProvider provider)
{
return Convert.ToInt16(thisValue, provider);
}

public int ToInt32(IFormatProvider provider)
{
return Convert.ToInt32(thisValue, provider);
}

public long ToInt64(IFormatProvider provider)
{
return Convert.ToInt64(thisValue, provider);
}

public sbyte ToSByte(IFormatProvider provider)
{
return Convert.ToSByte(thisValue, provider);
}

public float ToSingle(IFormatProvider provider)
{
return Convert.ToSingle(thisValue, provider);
}

public string ToString(IFormatProvider provider)
{
return thisValue.ToString(provider);
}

public object ToType(Type conversionType, IFormatProvider provider)
{
return Convert.ChangeType(thisValue, conversionType, provider);
}

public ushort ToUInt16(IFormatProvider provider)
{
return Convert.ToUInt16(thisValue, provider);
}

public uint ToUInt32(IFormatProvider provider)
{
return Convert.ToUInt32(thisValue, provider);
}

public ulong ToUInt64(IFormatProvider provider)
{
return Convert.ToUInt64(provider);
}

#endregion

#region IFormattable Members

public string ToString(string format, IFormatProvider
formatProvider)
{
return thisValue.ToString(format, formatProvider);
}

#endregion

#region IComparable Members

public int CompareTo(object obj)
{
return thisValue.CompareTo(obj);
}

#endregion
}
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

I am Pascal programmer who is converting a program to C#.

I would like know how to define the following data types in C#.

Q1. A data type that can have a range 1..13

Q2. A data typ that is 40 bits - aka a five byte array.

I wish to be able to declare variables with these data types line int,
bool etc.

C# is in the C++ & Java tradition - not in the Pascal & Ada
tradition.

You can not create types like that in C#.

You can do what people do - use a sufficient data type
(byte and long in your examples) and enforce the
restrictions in your own code.

Or you can wrap the same data types in a class and
specify various methods and operators on that class
to provide the needed functionality. This is probably
the most OOP'sk way of doing it, but a bit cumbersome
and performance could suffer.

I would go for the first approach.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top