比较问题 [英] Comparison question

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

问题描述

我试图这样做:


public static void GetValueFromDbObject(object dbObjectValue,ref decimal

destination)

{

destination = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue:

(十进制)dbObjectValue;

}


我只想说dbObjectValue是否相等到一个

DBNull.Value或",make destination = decimal.MaxValue。


这给了我:


nullHandler.cs(24,18):错误CS0019:运算符''||''不能应用于

类型为''bool''和''string''的操作数br />

如何更改该语句以执行我要执行的操作?


我试图将传递的值(dbObjectValue)移动到目的地,

除非=空或空字符串(因为这可能来自空白文本框)。


谢谢,


Tom

I tried to do this:

public static void GetValueFromDbObject(object dbObjectValue, ref decimal
destination)
{
destination = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue :
(decimal)dbObjectValue;
}

Where I am just trying to say if dbObjectValue is either equal to a
DBNull.Value or "", make destination = decimal.MaxValue.

This gives me:

nullHandler.cs(24,18): error CS0019: Operator ''||'' cannot be applied to
operands of type ''bool'' and ''string''

How would I change that statement to do what I am trying to do?

I am trying to move the value passed (dbObjectValue) into destination,
unless = Null or empty string (as this may come from a blank textbox).

Thanks,

Tom

推荐答案



tshad写道:

tshad wrote:
我试图这样做:

public static void GetValueFromDbObject(object dbObjectValue,ref decimal
destination)
{
destina tion = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue:
(十进制)dbObjectValue;
}

我只想说dbObjectValue是否等于一个
DBNull.Value或 ",make destination = decimal.MaxValue。

这给了我:

nullHandler.cs(24,18):错误CS0019:运算符''||''不能应用于''bool''和''string'类型的操作数

如何更改该语句以执行我想要做的操作?
除非= Null或空字符串(因为这可能来自空白文本框)。

谢谢,

Tom
I tried to do this:

public static void GetValueFromDbObject(object dbObjectValue, ref decimal
destination)
{
destination = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue :
(decimal)dbObjectValue;
}

Where I am just trying to say if dbObjectValue is either equal to a
DBNull.Value or "", make destination = decimal.MaxValue.

This gives me:

nullHandler.cs(24,18): error CS0019: Operator ''||'' cannot be applied to
operands of type ''bool'' and ''string''

How would I change that statement to do what I am trying to do?

I am trying to move the value passed (dbObjectValue) into destination,
unless = Null or empty string (as this may come from a blank textbox).

Thanks,

Tom




问题在于评估顺序。 C#正在评估此部分

首先:


dbObjectValue == DBNull.Value


这导致一个布尔值表达式,不能与

字符串进行比较。要使?:运算符工作,您需要比较两个类似

类型。这应该有效:


destination =((dbObjectValue == DBNull.Value)||

(dbObjectValue == string.Empty))?

decimal.MaxValue:

(十进制)dbObjectValue;


HTH!

Mike



The problem is in the order of evaluation. C# is evaluating this part
first:

dbObjectValue == DBNull.Value

This results in a boolean expression, which can''t be compared to a
string. To make the ?: operator work you need to compare two like
types. This should work:

destination = ((dbObjectValue == DBNull.Value) ||
(dbObjectValue == string.Empty)) ?
decimal.MaxValue :
(decimal)dbObjectValue;

HTH!
Mike


tshad,


您想要执行以下操作:


public static void GetValueFromDbObject(object dbObjectValue,ref decimal

destination)

{

//如果对象为null,或db null,或者对象是字符串,它是

为空,然后

//返回最大值。

if(dbObjectValue == null || dbObjectValue == DBNull.Value)

{

//返回最大值。

返回decimal.MaxValue;

}


//检查字符串。

string str = object as string;


//如果string不为null,检查为空。

if(str!= null)

{

//修剪字符串。

str = str.Trim();


//如果字符串为空,则返回最大值。

if(str == string.Empty)

{

//返回最大值。

返回decimal.MaxValue;

}


//将对象设置为字符串。

dbObjectValue = str;

}


//返回对象,转换为小数。

返回Convert.ToDecimal(dbObjectValue);

}


这将检查dbObjectValue是否也为空,并且它也将修改字符串,如果它是一个字符串,它与

略有不同你有什么,但你可能想检查(你可以删除它,如果你有
特定含义的空白字符串和对象为空)。


最后,这可能是在一个声明中完成的,但

这是一个非常重要的逻辑,它更容易维护,如

这个。


希望很有帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" tshad" < TS ********** @ ftsolutions.com>在消息中写道

news:es ************** @ TK2MSFTNGP15.phx.gbl ...
tshad,

You want to do the following:

public static void GetValueFromDbObject(object dbObjectValue, ref decimal
destination)
{
// If the object is null, or db null, or the object is string, and it is
empty, then
// return the max value.
if (dbObjectValue == null || dbObjectValue == DBNull.Value)
{
// Return the max value.
return decimal.MaxValue;
}

// Check for a string.
string str = object as string;

// If the string is not null, check for empty.
if (str != null)
{
// Trim the string.
str = str.Trim();

// If the string is empty, return the max value.
if (str == string.Empty)
{
// Return the max value.
return decimal.MaxValue;
}

// Set the object to the string.
dbObjectValue = str;
}

// Return the object, converted to a decimal.
return Convert.ToDecimal(dbObjectValue);
}

This checks to see if dbObjectValue is null as well, and it will also
trim the string, if it is a string, which is a little bit different than
what you had, but you might want to check (you can remove it if you have
specific meaning for strings of whitespace and the object is null).

In the end, this could have probably been done in one statement, but
this is a non-trivial piece of logic, and it is more easily maintained like
this.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"tshad" <ts**********@ftsolutions.com> wrote in message
news:es**************@TK2MSFTNGP15.phx.gbl...
我试图这样做:

public static void GetValueFromDbObject(object dbObjectValue,ref decimal
destination)
{
destination = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue:
(十进制)dbObjectValue;
}

我只想说dbObjectValue是否等于一个
DBNull.Value或 ",make destination = decimal.MaxValue。

这给了我:

nullHandler.cs(24,18):错误CS0019:运算符''||''不能应用于''bool''和''string'类型的操作数

如何更改该语句以执行我想要做的操作?
除非= Null或空字符串(因为这可能来自空白文本框)。

谢谢,

Tom
I tried to do this:

public static void GetValueFromDbObject(object dbObjectValue, ref decimal
destination)
{
destination = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue :
(decimal)dbObjectValue;
}

Where I am just trying to say if dbObjectValue is either equal to a
DBNull.Value or "", make destination = decimal.MaxValue.

This gives me:

nullHandler.cs(24,18): error CS0019: Operator ''||'' cannot be applied to
operands of type ''bool'' and ''string''

How would I change that statement to do what I am trying to do?

I am trying to move the value passed (dbObjectValue) into destination,
unless = Null or empty string (as this may come from a blank textbox).

Thanks,

Tom





Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:uN ************** @ TK2MSFTNGP09.phx.gbl ...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uN**************@TK2MSFTNGP09.phx.gbl...
tshad,
您想要执行以下操作:

public static void GetValueFromDbObject(object dbObjectValue,ref decimal
destination)
//
//如果对象是null,或db null,或者对象是字符串,它是空的,然后
//返回最大值。
if(dbObjectValue == null || dbObjectValue == DBNull .Value)
//返回最大值。
返回decimal.MaxValue;
}
//检查字符串。
string str = object as string;

//如果字符串不为null,请检查是否为空。
if(str!= null)
{
//修剪字符串。
str = str.Trim();

//如果字符串为空,则返回最大值。
if(str == string。空)
{
//返回最大值。
返回decimal.MaxValue;
}
//将对象设置为字符串。
dbObjectValue = str;
}

//返回转换为十进制的对象。
返回Convert.ToDecimal(dbObjectValue);

这将检查dbObjectValue是否也为空,它也将

为什么要检查null和DBNull.Value?


if(dbObjectValue == null || dbObjectValue == DBNull.Value)


这里发生的事情是,这将是一个十进制传递

来自十进制变量,文本框或来自数据库的字段。


谢谢,


Tom最后,这可能是在一个声明中完成的,但是这样。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

" tshad" < TS ********** @ ftsolutions.com>在消息中写道
新闻:es ************** @ TK2MSFTNGP15.phx.gbl ...
tshad,

You want to do the following:

public static void GetValueFromDbObject(object dbObjectValue, ref decimal
destination)
{
// If the object is null, or db null, or the object is string, and it
is empty, then
// return the max value.
if (dbObjectValue == null || dbObjectValue == DBNull.Value)
{
// Return the max value.
return decimal.MaxValue;
}

// Check for a string.
string str = object as string;

// If the string is not null, check for empty.
if (str != null)
{
// Trim the string.
str = str.Trim();

// If the string is empty, return the max value.
if (str == string.Empty)
{
// Return the max value.
return decimal.MaxValue;
}

// Set the object to the string.
dbObjectValue = str;
}

// Return the object, converted to a decimal.
return Convert.ToDecimal(dbObjectValue);
}

This checks to see if dbObjectValue is null as well, and it will also
trim the string, if it is a string, which is a little bit different than
what you had, but you might want to check (you can remove it if you have
specific meaning for strings of whitespace and the object is null).

Why would you check for both null and DBNull.Value?

if (dbObjectValue == null || dbObjectValue == DBNull.Value)

What is happening here is that this would either be a decimal being passed
in either from a decimal variable, textbox or a field from a database.

Thanks,

Tom In the end, this could have probably been done in one statement, but
this is a non-trivial piece of logic, and it is more easily maintained
like this.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"tshad" <ts**********@ftsolutions.com> wrote in message
news:es**************@TK2MSFTNGP15.phx.gbl...
我试图这样做:

public static void GetValueFromDbObject(object dbObjectValue,ref
十进制目标)
{
destination = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue:
(十进制)dbObjectValue;
}

我只想说dbObjectValue是否等于一个
DBNull.Value或 ",make destination = decimal.MaxValue。

这给了我:

nullHandler.cs(24,18):错误CS0019:运算符''||''不能应用于''bool''和''string''类型的操作数

如何更改该语句以执行我想做的事情?
除非= Null或空字符串(因为这可能来自空白文本框)。

谢谢,

Tom
I tried to do this:

public static void GetValueFromDbObject(object dbObjectValue, ref
decimal destination)
{
destination = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue :
(decimal)dbObjectValue;
}

Where I am just trying to say if dbObjectValue is either equal to a
DBNull.Value or "", make destination = decimal.MaxValue.

This gives me:

nullHandler.cs(24,18): error CS0019: Operator ''||'' cannot be applied
to operands of type ''bool'' and ''string''

How would I change that statement to do what I am trying to do?

I am trying to move the value passed (dbObjectValue) into destination,
unless = Null or empty string (as this may come from a blank textbox).

Thanks,

Tom




这篇关于比较问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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