typesafe ints ---如何???? [英] typesafe ints --- How????

查看:55
本文介绍了typesafe ints ---如何????的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想(或者至少我想我想)为一个int定义一个包装类

以使其类型化安全。我知道这意味着将一个

值类型转换为引用类型 - 它对性能不敏感。


如何编写一个很好地包装整数的类除了Object之外还有什么东西?
?例如,我写了:


公共类DBSchemaID

{

private int schemaID = 0;

public N2KDBSchemaID(int schemaIDIn)

{

schemaID = schemaIDIn;

}


public int SchemaID

{

get {return schemaID;}

set {schemaID = value;}

}

}


现在我怎样才能让它在用作数组时充当int?

条目?例如:


DBSchemaID schemaID = new DBSchemaID(4);

schemaObj = SchemaArray [schemaID];


是否可以将某种形式的转换转换为int(如ToString())。

也可以覆盖运算符==和> =等等。


可以这样做吗?想要这样做我疯了吗?你怎么可能

确保方法调用的参数是类型安全的。在其他

字样中,如果方法是GetSchemaDBName(int schemaID),则任何int都可以是

交给。如果方法是GetSchemaDBName(DBSchemaID schemaID)那么

我更加严格和安全。想要这样做是否合理

这个?


只要被允许从Int32派生就可以做所有必要的事情。


做这种事的最好办法是什么?任何建议赞赏

- 我是新来的。


干杯

OldNewbie

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

推荐答案

最简单和类型安全的我喜欢在这种情况下建议是什么

喜欢以下


public enum DBSchemaType {

NT2K,

XP

}


公共课DBSchema

{

// ...

public GetSchema(DBSchemaType schemaType){

// enum是类型安全的cuz否则,代码

//甚至不会编译

}

// ...

}

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
}
//...
}


2005年11月7日星期一04:06:10 -0800,Ashura

< as **** @ discussion.microsoft。 COM>写道:


您好Ashure


我不确定会这样做。在我的情况下,我希望包装的int

真的是一个int。它最终将是一个数组索引,并且可能有$ 100 $ 10,000或者100万美元的价值。


我可能会弄错,(我是新的到C#)我可能误解了你

但是我不认为我可以为枚举中的每一个定义一个值

int。


干杯

ON
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
最简单和类型安全我喜欢在这种情况下建议像
以下

公共枚举DBSchemaType {
NT2K,
XP
}
公共课DBSchema
{
/ / ...
公共GetSchema(DBSchemaType schemaType){
// enum是类型安全的cuz否则,代码
//甚至不编译
}
// ......

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
}
//...
}






枚举数据类型本身就是一个整数



公共枚举DBSchemaType {

NT2K = 0,

XP = 1

}


如果是的话执行以下操作


DBSchemaType schemaType = DBSchemaType.NT2K;

int typeInt = Convert.ToInt32(schemaType);

//现在typeInt = 0
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


这篇关于typesafe ints ---如何????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆