三元运算符中的问题 [英] problem in ternary operators

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

问题描述

您好,我正在开发一个应用程序,当该应用程序存在默认密码时就接受新密码

Hi,i''m buliding an application which accepts new password when their exists a default password

savedPassword = (Application.UserAppDataRegistry.GetValue("newpassword")) ? Application.UserAppDataRegistry.GetValue("newpassword").ToString() : "defaultPassword";


但是我遇到一个错误,它说不能将objet转换为bool ..我认为它们在三元运算符中出了点问题.
并且如果我在expr1处添加null,即


but i''m getting an error it says cannnot convert objet to bool..i think theirs something wrong in ternary operator.
and if I add null at expr1 ie

savedPassword = (Application.UserAppDataRegistry.GetValue("newpassword")!=null) ? Application.UserAppDataRegistry.GetValue("newpassword").ToString() : "defaultPassword";


不会显示任何错误,但是它不接受默认密码,并且在可以更改之前不会打开主窗体.
thanx:confused:


than no error is shown but it doesn''t accepts default password and main form doesn''t open before it can be changed.
thanx:confused:

推荐答案

嗨Pravat shah,

检查以下值是否为``null''或``Empty''

Hi Pravat shah,

Check this following value is comes ''null'' or ''Empty''

Application.UserAppDataRegistry.GetValue("newpassword")



如果为空意味着只需更改条件



if Empty means just change your condition

savedPassword = (Application.UserAppDataRegistry.GetValue("newpassword").ToString()!=string.Empty) ? Application.UserAppDataRegistry.GetValue("newpassword").ToString() : "defaultPassword";



干杯:)



Cheers :)


Application.UserAppDataRegistry.GetValue("newpassword")!= null)

您可能需要将其与string.empty进行比较.
Application.UserAppDataRegistry.GetValue("newpassword")!=null)

You might need to compare this with string.empty.




只是为了对上面的答案做进一步的说明...

出现错误的原因是因为Application.UserAppDataRegistry.GetValue返回一个对象,而不是bool.

更好的方法是尝试以下方法:

Hi,

just to make further notes on the answer above...

The reason you are getting the error is because Application.UserAppDataRegistry.GetValue returns an object, not a bool.

A better way to implement it would be to try the following:

<code>
string savedPassword;
string password = Application.UserAppDataRegistry.GetValue("newpassword").ToString();
savedPassword = string.IsNullOrEmpty(password) ? "defaultPassword" : password;
</code>



戴夫



Dave


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

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