你怎么解决这个问题? [英] How do you get around this?

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

问题描述

抱歉这个愚蠢的问题,但我去过那里并做到了。只是

刚才我忘记了它。它发生在

你完全不同的想法。保释我在这里。


仅举一个例子:


static void Main(string [] args)

{

Console.WriteLine((新的Class1())。SomeMethod((Object)19));

}


public int SomeMethod(System.Object obj)

{

if(obj == null)

抛出新的System.Exception(" ;参数为null。);


if((int)obj> = 10)

return(int)obj;

}

错误:Class1.cs(13):''Exception.Class1.SomeMethod(object)'':并非所有

代码路径都返回价值


你怎么摆脱这个?

Sorry for the stupid question, but I''ve been there and done that. Just
that just now I have forgotten it for the moment. It happens when
you''re in a totally different rut of thought. Bail me out here.

Just to quote an example:

static void Main(string[] args)
{
Console.WriteLine((new Class1()).SomeMethod((Object)19));
}

public int SomeMethod(System.Object obj)
{
if (obj == null)
throw new System.Exception("The argument is null.");

if ((int)obj >= 10)
return (int)obj;
}
Error: Class1.cs(13): ''Exception.Class1.SomeMethod(object)'': not all
code paths return a value

How do you get out of this?

推荐答案

告诉它什么如果obj是< = 9,那么你想做什么...返回别的东西?

抛出一个ArgumentException?跳一个探戈? (后者不支持

1.1 ...或2.0 ...)


另外 - 也许是来自更复杂的代码示例的简化,

但是我不确定为什么obj需要在这种情况下成为一个对象,因为我们将
排除为null然后转换为int,这不会'''留下很多选择......怎么样

public int SomeMethod(int value){

返回值> = 10?值:0; //如果value isn''t> = 10,则返回0;可以

抛出一个ex而不是

}
Just tell it what you want to do if obj is <= 9... return something else?
throw an ArgumentException? dance a Tango? (the latter not supported by
1.1... or 2.0...)

Also - maybe it is from the simplification from a more complex code sample,
but I''m not sure why obj needs to be an object in this case, since we rule
out null and then cast to int, this doesn''t leave many options... how about

public int SomeMethod(int value) {
return value >= 10 ? value : 0; // returns 0 if value isn''t >= 10; could
throw an ex instead
}


是的!在发帖之后就这样做了。


问题实际上是:

public System.Data.IDataReader GetReader(string SQL)

{

System.Data.IDataReader ret = null;


if(this._connection.UnderlyingConnection == null)

{

System.Exception ex = new DatabaseTypeNotSupportedException();

_log.Write(new LogEntry(ex),true);

抛出ex;

}


if(this._connection.Database == Databases.MSAccess)

ret =(new OleDbCommand(SQL,

(OleDbConnection)this._connection.UnderlyingConnec tion))。ExecuteReader();

else if(this._connection.Database == Databases.SQLServer)

ret =(new SqlCommand(SQL,

(SqlConnection)this._connection.UnderlyingConnecti on))。ExecuteReader();


返回ret;

}

Yup! Did that just after making the post.

The problem under question actually was:
public System.Data.IDataReader GetReader(string SQL)
{
System.Data.IDataReader ret = null;

if (this._connection.UnderlyingConnection == null)
{
System.Exception ex = new DatabaseTypeNotSupportedException();
_log.Write(new LogEntry(ex), true);
throw ex;
}

if (this._connection.Database == Databases.MSAccess)
ret = (new OleDbCommand(SQL,
(OleDbConnection)this._connection.UnderlyingConnec tion)).ExecuteReader();
else if (this._connection.Database == Databases.SQLServer)
ret = (new SqlCommand(SQL,
(SqlConnection)this._connection.UnderlyingConnecti on)).ExecuteReader();

return ret;
}


嗨Walter,

public int SomeMethod(System.Object o bj)

{

// ...

if((int)obj> = 10)

return(int)obj;

}


如果(int)obj不大于10,它会返回什么?

-

HTH,


Kevin Spencer

Microsoft MVP

专业版Chicken Salad Alchemist


顺序,选择,迭代。

Water Cooler v2 < wt ***** @ yahoo.comwrote in message

news:11 ********************** @ m73g2000cwd。 googlegr oups.com ...
Hi Walter,

public int SomeMethod(System.Object obj)
{
//...
if ((int)obj >= 10)
return (int)obj;
}

What does it return if (int)obj is NOT greater than 10?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Sequence, Selection, Iteration.
"Water Cooler v2" <wt*****@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...

很抱歉这个愚蠢的问题,但我去过那里并做到了。只是

刚才我忘记了它。它发生在

你完全不同的想法。保释我在这里。


仅举一个例子:


static void Main(string [] args)

{

Console.WriteLine((新的Class1())。SomeMethod((Object)19));

}


public int SomeMethod(System.Object obj)

{

if(obj == null)

抛出新的System.Exception(" ;参数为null。);


if((int)obj> = 10)

return(int)obj;

}


错误:Class1.cs(13):''Exception.Class1.SomeMethod(object)'':不是全部

代码路径返回一个值


你怎么摆脱这个?
Sorry for the stupid question, but I''ve been there and done that. Just
that just now I have forgotten it for the moment. It happens when
you''re in a totally different rut of thought. Bail me out here.

Just to quote an example:

static void Main(string[] args)
{
Console.WriteLine((new Class1()).SomeMethod((Object)19));
}

public int SomeMethod(System.Object obj)
{
if (obj == null)
throw new System.Exception("The argument is null.");

if ((int)obj >= 10)
return (int)obj;
}
Error: Class1.cs(13): ''Exception.Class1.SomeMethod(object)'': not all
code paths return a value

How do you get out of this?



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

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