这简直太愚蠢了。 [英] This is just plain stupid.

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

问题描述

我可以这样说:


int commandTimeout =

Convert.ToInt32(ConfigurationSettings.AppSettings [" commandTimeout"]);


但是我不能这样说:

int commandTimeout =

(int)ConfigurationSettings.AppSettings [" commandTimeout"];


或许我的VB背景造成脑损伤,我可以这样做:


Dim i As Integer

i =" 123" < - 我现在是数字123


有没有更干净的方式做这样的事情而没有很长的

表达式是代码的两倍宽窗口?

-

Peace&快乐的计算,


Mike Labosh,MCSD


当你杀了一个男人时,你就是凶手。

杀死很多人,你就是征服者。

杀死所有人,你就是上帝了。 - Dave Mustane

I can say this:

int commandTimeout =
Convert.ToInt32(ConfigurationSettings.AppSettings["commandTimeout"]);

but I can''t say this:
int commandTimeout =
(int)ConfigurationSettings.AppSettings["commandTimeout"];

Or maybe my VB background has caused brain damage, where I can do this:

Dim i As Integer
i = "123" <-- i is now numeric 123

Is there any cleaner way of doing stuff like this without great big long
expressions twice as wide as the code window?
--
Peace & happy computing,

Mike Labosh, MCSD

"When you kill a man, you''re a murderer.
Kill many, and you''re a conqueror.
Kill them all and you''re a god." -- Dave Mustane

推荐答案

Mike,


你是对的,这是你的VB背景造成脑损伤。

当你在VB中这样做时:

Dim i As Integer

i =" 123"


VB将为你执行字符串类型转换为数字。


在C#中,当你这样做时:


int commandTimeout =(int)

ConfigurationSettings.AppSettings [" commandTimeout"];


您正在尝试执行演员表从一个字符串到一个int,这不能完成
。您需要转换。


您始终可以将设置存储在字符串变量中,并在下一行转换为

。无论哪种方式,你必须告诉C#该做什么。是的,VB将会给你带来这样的优点,但最终,这个魔术会带来什么样的优势。它会为你做b $ b对你来说会在一天之内咬你。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam .guard.caspershouse.com


" Mike Labosh" <毫升***** @ hotmail.com>在消息中写道

news:O
Mike,

You are right, it is your VB background that has caused brain damage.
When you do this in VB:

Dim i As Integer
i = "123"

VB will perform a conversion of the string type into a number for you.

In C#, when you do this:

int commandTimeout = (int)
ConfigurationSettings.AppSettings["commandTimeout"];

You are trying to perform a cast from a string to an int, which can''t be
done. You need a conversion.

You can always store the setting in a string variable, and convert it on
the next line. Either way, you have to tell C# what to do. Yes, VB will
give you little advantages like this, but ultimately, that "magic" that it
does for you is going to bite you in the ass one day.

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

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:O


************** @ tk2msftngp13.phx.gbl ...
**************@tk2msftngp13.phx.gbl...
我可以这样说:

int commandTimeout =
Convert.ToInt32(ConfigurationSettings.AppSettings [" commandTimeout"]);

但是我不能这样说:
int commandTimeout =
(int)ConfigurationSettings.AppSettings [" commandTimeout"];

或许我的VB背景造成脑损伤,在哪里我可以这样做:

Dim i As Integer
i =" 123" < - 我现在是数字123

有没有更清晰的方式做这样的事情而没有比代码窗口宽两倍的长大的表达式?
-
Peace&快乐的计算,

Mike Labosh,MCSD

当你杀死一个男人时,你是一个凶手。
杀了很多人,你就是一个征服者。
杀死他们,你就是上帝。 - Dave Mustane
I can say this:

int commandTimeout =
Convert.ToInt32(ConfigurationSettings.AppSettings["commandTimeout"]);

but I can''t say this:
int commandTimeout =
(int)ConfigurationSettings.AppSettings["commandTimeout"];

Or maybe my VB background has caused brain damage, where I can do this:

Dim i As Integer
i = "123" <-- i is now numeric 123

Is there any cleaner way of doing stuff like this without great big long
expressions twice as wide as the code window?
--
Peace & happy computing,

Mike Labosh, MCSD

"When you kill a man, you''re a murderer.
Kill many, and you''re a conqueror.
Kill them all and you''re a god." -- Dave Mustane



" Mike Labosh" <毫升***** @ hotmail.com>在消息中写道

news:O
"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:O


这篇关于这简直太愚蠢了。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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