C#的新手,我目前在布尔值上有一个Try Parse错误。 [英] New to C#, I'm currently having a Try Parse error on a Boolean value.

查看:98
本文介绍了C#的新手,我目前在布尔值上有一个Try Parse错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手。

我的任务是创建这个应用程序,我觉得我做得很好。做专有信息我不能提供太多细节,但我确实需要一些方向。



我正在使用带有C#的Silverlight 5来构建业务应用程序。这是交易:



我有一个基于LINQ查询从我的数据库中引入的值,该查询填充文本块以显示值,以便我可以看到它。到目前为止一切正常,我更改了DB中的值,文本块中的值发生了变化。 DB中的值是一个转换为我的文本块中的文本的位(True,False)...很棒......有效!



当我尝试拿这个值并将其转换为Bool但是,它每次都失败了。我所有的搜索和研究都产生了一些似乎对其他人有用的好主意。我很确定我正确使用它,显然不是。



这是我的主要问题所在的代码。



I''m new to C#.
I''ve been tasked to create this application and I felt like I was doing well. Do to proprietary information I cannot give too many details but I do need some direction.

I''m using Silverlight 5 with C# to build a business application. Here''s the deal:

I have a value brought in from my DB based on a LINQ query that populates a text block to display the value so I can see it. To this point it all works, I change the value in the DB the value changes in the text block. The value in the DB is a bit which translates to text in my text block (True,False)...great...works!

When I attempt to take that value and convert it to Bool however, it fails...every time. All my searching and research has resulted in great ideas that seem to work for others. I was pretty sure I was using it correctly, obviously not however.

Here''s the code where my primary issue is located.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    flag.Text = bool.TryParse(hiddenSts.Text, out allowed).ToString();
            
    bool isAllowed;
            
    if(!bool.TryParse(hiddenSts.Text, out isAllowed))
    {
        String Name = WebContext.Current.User.DisplayName.ToString().Substring(8);
        String Auth = WebContext.Current.User.IsAuthenticated.ToString();

        userNotAllowed accessDenied = new userNotAllowed();
        accessDenied.userIdentity.Text = Name;
        accessDenied.authentication.Text = string.Concat(Auth, " | Win_Auth");
        accessDenied.Show();
    }
}





导航到此页面后,我的代码获取存储在我的文本块中的值(hiddenSts ),根据我在DB中的位值,它是真还是假,并尝试对文本块中的字符串进行布尔转换。因此,当值为false时,我会弹出一个子窗口,并在页面之间重定向...当我对任何值进行硬编码时,所有工作都正常。 TryParse是唯一没有按照我的想法行事的人。



-flag是一个文本框,可以看到TryParse的返回(总是假的)

-hiddenSts是一个文本块,用于根据查询查看数据库中的值



任何方向都会非常感激。



Upon navigating to this page my code takes the value stored in my text block (hiddenSts), which is True or False depending on my bit value in the DB, and attempts the Boolean conversion on the string in the text block. So when the value is false I have a child window pop up and a redirect between pages...all working when I hard code any values. TryParse is the only one not acting as I thought it would.

-flag is a text box to see the return of the TryParse (always False)
-hiddenSts is a text block to see the value from the DB based on a query

Any direction would be greatly appreciated.

推荐答案

您需要了解 bool.TryParse 可以返回false,这不是错误(它是什么?),但表示输入字符串包含意外的内容或为空。预期的大写字符串为True或False。



-SA
You need to understand that bool.TryParse can return false, which is not an "error" (what is it?), but an indication that input string contains something unexpected or is null. Expected are the capitalized strings "True" or "False".

—SA


To检查发生了什么,使用bool.Parse而不是bool.TryParse。如果抛出FormatException,则您提供的值不等于bool.TrueString或bool.FalseString(测试不区分大小写)。



如果ArgumentNullException为抛出,你的价值为空。



否则它应该有效。
To check what''s going on, use bool.Parse instead of bool.TryParse. If a FormatException is thrown, the value you are supplying is not equal to bool.TrueString or bool.FalseString (the test is case-insensitive).

If ArgumentNullException is thrown, your value is null.

Otherwise it should work.


这篇关于C#的新手,我目前在布尔值上有一个Try Parse错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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