473,385 Members | 1,355 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,385 software developers and data experts.

typesafe ints --- How????

Hi

I would like (or a least think I would like to) define a wrapper class
for an int so as to make it type safe. I know this means converting a
value type to a reference type - its not that performance sensitive.

How do I write a class that wraps the integer nicely and is something
other than Object?. For example, I have written:

public class DBSchemaID
{
private int schemaID=0;
public N2KDBSchemaID(int schemaIDIn)
{
schemaID=schemaIDIn;
}

public int SchemaID
{
get{return schemaID;}
set{schemaID=value;}
}
}

So now how can I make it act as an int in the when used as an array
entry? For example:

DBSchemaID schemaID=new DBSchemaID(4);
schemaObj=SchemaArray[schemaID];

Is some sort of implict conversion to int (like ToString()) possible.
Also can one override the operators == and >= etc.

Can this be done? Am I crazy for wanting to do it? How else could you
make sure that the parameters of a method call are type safe. In other
words, if a method is GetSchemaDBName(int schemaID) any int could be
handed in. If the method is GetSchemaDBName(DBSchemaID schemaID) then
I am being a bit stricter and safer. Is it reasonable to want to do
this?

Just being allowed to derive from Int32 would do everything necessary.

What the best way of doing this sort of thing? Any advice appreciated
- I am new to this.

Cheers
OldNewbie
Nov 17 '05 #1
10 1258
the simplest and type safe i cud like to suggest in this case is something
like the following

public enum DBSchemaType {
NT2K,
XP
}

public class DBSchema
{
//...
public GetSchema(DBSchemaType schemaType) {
//enum is type safe cuz otherwise, the code
//wont even compile
}
//...
}

Nov 17 '05 #2
On Mon, 7 Nov 2005 04:06:10 -0800, Ashura
<As****@discussions.microsoft.com> wrote:

Hi Ashure

I'm not sure this would do it. In my case the int I wish to wrap
really is an int. It will eventually be an array index and could have
a value of 100, 10,000 or a million.

I may be mistaken, (I'm new to C#) and I may have misunderstood you
but I don't think I can define a value in the enum for every possible
int.

Cheers
ON
the simplest and type safe i cud like to suggest in this case is something
like the following

public enum DBSchemaType {
NT2K,
XP
}

public class DBSchema
{
//...
public GetSchema(DBSchemaType schemaType) {
//enum is type safe cuz otherwise, the code
//wont even compile
}
//...
}


Nov 17 '05 #3
enum data type is an integer by itself

say
public enum DBSchemaType {
NT2K = 0,
XP = 1
}

and if ya do the follwoing

DBSchemaType schemaType = DBSchemaType.NT2K;
int typeInt = Convert.ToInt32(schemaType);
//NOW typeInt = 0
Nov 17 '05 #4
Hi,

"CSharpNewbie" <No****@NoSpam.com> wrote in message
news:bv********************************@4ax.com...
Hi

I would like (or a least think I would like to) define a wrapper class
for an int so as to make it type safe.
What you mena with "type safe" ?


DBSchemaID schemaID=new DBSchemaID(4);
schemaObj=SchemaArray[schemaID];
What you gain with that?
Is some sort of implict conversion to int (like ToString()) possible.

ToString() is not implicit , in fact is not even explicit nor a conversion
, ToString() is a method !!!

To answer your question, yes, there is, look into the C# help the the
implicit keyword.
I think you need to rethink what you want first.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #5
On Mon, 7 Nov 2005 09:18:14 -0500, "Ignacio Machin \( .NET/ C# MVP \)"
<ignacio.machin AT dot.state.fl.us> wrote:
Hi,

"CSharpNewbie" <No****@NoSpam.com> wrote in message
news:bv********************************@4ax.com.. .
Hi

I would like (or a least think I would like to) define a wrapper class
for an int so as to make it type safe.


What you mena with "type safe" ?


DBSchemaID schemaID=new DBSchemaID(4);
schemaObj=SchemaArray[schemaID];


What you gain with that?


Hi Ignacio

Well, I'm pretty new as this so to be honest I am not totally sure
what I want.

The problem is that I have arrays of object types lets say they are
ObjX [] XArray = new ObjX[1000];
ObjY [] YArray = new ObjY[10000];
ObjZ [] ZArray = new ObjZ[100000];

These arrays are sparsely populated. There may or may not be an object
in any specific position. The position in the array essentially forms
a quick access "ID" for the object. This ID is used a lot. I pass this
ID into various member functions which perform operations on the
arrays (and the objects contained in the arrays). Since I pass this ID
around I was thinking that I should probably make it an "Object". That
way it would represent a ObjX ID or an ObjY ID. An ObjXID (essentially
an index into the XArray) passed in to a function DoSomething(ObjYID
foo) would generate a compile (or runtime) error rather than just
doing something spurious. Once inside the function they would act just
as an int did. The ability to inherit from int would do this nicely:
i.e.

public class ObjXID : int32 {} // but you can't do this

Thats what I mean by type safe. If I'm flinging various array indexes
around I thought it might be nice to make sure that they actually were
what they were supposed to be instead of a generic int.

I'm inexperienced - so this may be an inappropriate use of the
language. All advice cheerfully received.

Cheers
Newbie

Nov 17 '05 #6
Hi,
These arrays are sparsely populated. There may or may not be an object
in any specific position. The position in the array essentially forms
a quick access "ID" for the object. This ID is used a lot. I pass this
ID into various member functions which perform operations on the
arrays (and the objects contained in the arrays).

Why not use a Hashtable ?

Or even create your own strong typed collection

Having an array sparsely populated is not a good idea
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #7
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:OM**************@TK2MSFTNGP15.phx.gbl...
Why not use a Hashtable ?

I second Ignacio's choice.

A sparce array of objects would need to be large enough to keep references
(pointers?) to each of the objects, whether they existed or not. This could
potentially suck up lots of memory (not to mention difficult to resize).

Scott
Nov 17 '05 #8
I had a similar thought to your own, and here is how I resolved the
problem.

Wherever possible, pass object references around, not IDs. Inside your
code, you don't want to be "flinging spurious array indices around" for
precisely the reasons you mentioned: because the client code needs to
know how to use those array indices to get the relevant object. I
decided that it was far better to pass an object reference around. That
way client code has the actual object, not just a name for it.

I then built a bit of structure to help support this sort of thing.

First, I created an interface called IKeyed:

public interface IKeyed
{
public string PrimaryKey { get; }
}

The primary key doesn't need to be a string; it could be an int
instead. Now every business object implements IKeyed, and knows its own
key. Then I built a class, SelfKeyedCollection, that accepted IKeyed
objects. Since each object knows its own key, the SelfKeyedCollection
just needs the object in order to add it:

public void Add(IKeyed obj) ...

Inside the SelfKeyedCollection I used a Hashtable to store the items.

Finally, each business object declared a static method:

public static string PrimaryKey(...)

that allows outside callers to construct a primary key given qualities
of the object. (Your version might return int, not string). So now, I
can pass around business objects (which know their own keys),
collections of business objects (which can be strongly typed), and
clients can find a business object in a collection by constructing a
primary key:

BObj myBusinessObject = keyedCollection[BObj.PrimaryKey(company,
stockCode)];

In the end, passing about the objects themselves turned out to be much
better than passing around IDs.

Nov 17 '05 #9
Hi Bruce

Thanks very much for this post. V interesting - I will probably
implement something like this. Much appreciated you sharing it with
me.

Cheers
Newbie

On 8 Nov 2005 09:41:54 -0800, "Bruce Wood" <br*******@canada.com>
wrote:
I had a similar thought to your own, and here is how I resolved the
problem.

Wherever possible, pass object references around, not IDs. Inside your
code, you don't want to be "flinging spurious array indices around" for
precisely the reasons you mentioned: because the client code needs to
know how to use those array indices to get the relevant object. I
decided that it was far better to pass an object reference around. That
way client code has the actual object, not just a name for it.

I then built a bit of structure to help support this sort of thing.

First, I created an interface called IKeyed:

public interface IKeyed
{
public string PrimaryKey { get; }
}

The primary key doesn't need to be a string; it could be an int
instead. Now every business object implements IKeyed, and knows its own
key. Then I built a class, SelfKeyedCollection, that accepted IKeyed
objects. Since each object knows its own key, the SelfKeyedCollection
just needs the object in order to add it:

public void Add(IKeyed obj) ...

Inside the SelfKeyedCollection I used a Hashtable to store the items.

Finally, each business object declared a static method:

public static string PrimaryKey(...)

that allows outside callers to construct a primary key given qualities
of the object. (Your version might return int, not string). So now, I
can pass around business objects (which know their own keys),
collections of business objects (which can be strongly typed), and
clients can find a business object in a collection by constructing a
primary key:

BObj myBusinessObject = keyedCollection[BObj.PrimaryKey(company,
stockCode)];

In the end, passing about the objects themselves turned out to be much
better than passing around IDs.


Nov 17 '05 #10
Sorry... one correction.

Each business object declares a static method:

public static string BuildPrimaryKey(...)

so you would say

BObj myBusinessObject = keyedCollection[BObj.BuildPrimaryKey(company,
stockCode)];

not "PrimaryKey", since the business object already implements the
IKeyed method and so has an instance property named "PrimaryKey". The
compiler won't let you have an instance property and a static method
with the same name, so your static method for building primary keys
from scratch needs a different name.

Sorry for the typo.

Nov 17 '05 #11

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

Similar topics

7
by: Jus! | last post by:
Hi. I am reading bits(1's & 0's) from a file and i wa wondering what is the most efficient method of converting these strings to individual int's? eg. File contains: 110001 010011 etc......
3
by: uclamathguy | last post by:
I am working on connected component analysis, but that is irrelevant. I have a mapping containing ints as the keys and sets of ints as the "values." Given an integer, I need to iterate through...
3
by: Chris N. Hinds | last post by:
I have a question regarding accessing long long ints in unions. I have constructed a union with a double, two ints in a structure, and a long long int. When the double is loaded with a...
4
by: Hal Styli | last post by:
/*hello could someone please help me with the errors i get related to pointers to arrays of ints. I want to declare something like int *d = { 7, 11, 18, 54, 64, 55, 37, 38, }; rather than...
2
by: adam | last post by:
Having spent nearly 2 years in win forms land the inevitable request came for me to "do some web pages". So being new to this bit of .net and having had a look around I can't see where the best...
3
by: Matthias S. | last post by:
Hi there, I have a typesafe collection in my application called Countries, which is derived from the ReadOnlyCollectionBase. As you might guess, the item-value is of type Country. Each Country...
4
by: Davo | last post by:
I wish to setup a resource that holds CONSTANT values. The resource file is call LogCat.resx (Logger Categories) and contains Debug, Info, Error etc... This resource is located in a resource...
10
by: bg_ie | last post by:
Hi, I have a function which compares two unsigned ints and returns their difference in order to establish which is the greater of the two. int Compare(unsigned int time_left, unsigned int...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.