473,394 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

[Q]Custom data types in C#

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

Mar 22 '07 #1
5 5958
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
Mar 22 '07 #2
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.
Mar 22 '07 #3
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<intMembers

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

#endregion

#region IComparable<intMembers

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
}

<sh******@yahoo.com.auha scritto nel messaggio
news:11*********************@o5g2000hsb.googlegrou ps.com...
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.


Mar 22 '07 #4
sh******@yahoo.com.au wrote:
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;
}

}

--
Göran Andersson
_____
http://www.guffa.com
Mar 22 '07 #5
sh******@yahoo.com.au wrote:
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
Mar 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Blake T. Garretson | last post by:
I'm having some issues with decimal.Decimal objects playing nice with custom data types. I have my own matrix and rational classes which implement __add__ and __radd__. They know what to do with...
3
by: Gabe Moothart | last post by:
I would like to create slightly modified 'custom' data types. For instance, a 'Degree' type, which behaves exactly like a double, except that it has an extra property 'ToRadians'. Or, a 'phone'...
7
by: Luc Tremblay | last post by:
Given the typical following code: void Listener::HandleEvent(const Event& event) { // handling code } In a "clean" fashion, how is it possible to add custom data (to be subsequently...
0
by: Rossen Tzonev | last post by:
Hi all, I am using VC.NET 2003 Professional and I want to create COM simple object. Everything is OK but now I want to use my own defined type ( enumeration in my case ) as type for COM object...
3
by: Matt D | last post by:
In my web service project I've imported an assembly (that I also have the source to) that contains structs that are serializable. I've created some web service methods that return and take as...
6
by: Gaz | last post by:
Hi guys. I've been lookig for this in the numpy pdf manual, in this group and on google, but i could not get an answer... Is there a way to create a custom data type (eg: Name: string(30), Age:...
2
by: Ratnakar .N | last post by:
hello, please define any custom datatypes in .net and provide one example regards ratnakar
0
by: prasadbodas2000 | last post by:
Hi, I need a sample code in ASP or VB wherein ADO command object is used to invoke oracle stored procedure. The stored procedure (SP) should have oracle specific custom data types (CDT) as...
1
by: jehugaleahsa | last post by:
Hello: I am experiencing performance related issues when my custom data structures work with value types. I use generics to prevent boxing wherever I can. For instance, I use IEqualityComparer,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.