哪个是VB.NET的最佳代码 [英] Which is the best code VB.NET

查看:78
本文介绍了哪个是VB.NET的最佳代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

If role = "Administrator" Then
   MultiLevelMenu.Visible = True
Else
   MultiLevelMenu.Visible = False
End If






or

MultiLevelMenu.Visible = role = "Administrator"





我尝试过:



它只是可读性吗?

推荐答案

使用第一个:第二个是令人困惑的阅读,因为它暗示它应该读作两个赋值:

Use the first: the second is confusing to read as it implies that it should read as two assignments:
role = "Administrator"
MultiLevelMenu.Visible = role

(这是C#比VB更好的语言的原因之一:赋值运算符=与等式测试运算符==不相同) />


如果您觉得必须写第二个表格,那么至少使用括号使其更清晰:

(This is one of the reasons C# is a better language than VB: the assignment operator "=" is not the same as the equality test operator "==")

If you feel you must write the second form, then at least use brackets to make it clearer:

MultiLevelMenu.Visible = (role = "Administrator")

但仅仅因为你可以做某事,并不意味着你应该...

But just because you can do something, doesn't mean you should...


我不是来自VB.Net背景但如果我可以将它与C#联系起来,要么你可以尝试第一个,即,

I am not from VB.Net background but if I can relate this to C#, either you can try the first one i.e,
If role = "Administrator" Then
   MultiLevelMenu.Visible = True
Else
   MultiLevelMenu.Visible = False
End If



或者您可以使用三元运算符


Or you can use Ternary Operator

'MultiLevelMenu.Visible = (role == "Administrator"? true : false) //in c#
MultiLevelMenu.Visible = If(role = "Administrator", true, false)





从性能的角度来看,两者都应该相同,因为这些语句将转换为MSIL,并且在那时应该相同。 br />


希望,它有帮助:)



From performance point of view both should be same as these statements will be translated to MSIL and should same at that time.

Hope, it helps :)


两个代码都是一样的,它们也编译成相同的代码。

初学者通常喜欢第一种形式,因为它们更容易阅读。

第二种形式通常是实验用户的首选,因为它更紧凑,更易于阅读。

我更喜欢

Both codes are doing the same and they compiled to same code too.
First form is usually preferred by beginners because they find it easier to read.
Second form is usually preferred by experimented users because it is more compact and as easy to read.
I prefer
MultiLevelMenu.Visible = ( role = "Administrator" )



,因为某些语言允许级联分配d seconf form转换为:


because some language allow cascade assignment and seconf form translate to:

role = "Administrator"
MultiLevelMenu.Visible = role


这篇关于哪个是VB.NET的最佳代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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