1

I have this code (the problem lines have comments above them):

private async void btn_loginAdmin_Click(object sender, RoutedEventArgs e)
        {
            // 'txt_adminId' does not exist in the current context.
            if (txt_adminId.Text = "root")
            {
                // 'txt_adminPw' does not exist in the current context.
                if (txt_adminPw.Password = "password")
                {
                    var msg_login = new MessageDialog("Logged in!");
                    await msg_login.ShowAsync();
                }
                else
                {
                    var msg_login = new MessageDialog("Wrong password!");
                    await msg_login.ShowAsync();
                }
            }
            else
            {
                var msg_login = new MessageDialog("Wrong combo!");
                await msg_login.ShowAsync();
            }
        }

I have always had that problem with C#. I don't know what it means. But I'm sure that in this file's .xaml, those 2 text boxes exist.

  • This is a Windows Store C# program.

EDIT:

Here's the output:

1>C:\Database\GH3_WSE\GH3_WSE\login_admin.xaml.cs(119,17,119,42): error CS0029: Cannot implicitly convert type 'string' to 'bool'
1>C:\Database\GH3_WSE\GH3_WSE\login_admin.xaml.cs(121,21,121,54): error CS0029: Cannot implicitly convert type 'string' to 'bool'
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Wix
  • 11
  • 2

2 Answers2

0

Check the x:Class on xaml page has the exact name of your .cs class.

It should be like:

x:Class = "ProjectName.C#ClassName"
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
0

I guess you forgot twice the = operator on the following lines:

if (txt_adminId.Text = "root")

should be

if (txt_adminId.Text == "root")

and also on this line

if (txt_adminPw.Password = "password")

So, you might want to write this:

private async void btn_loginAdmin_Click(object sender, RoutedEventArgs e)
        {
            // 'txt_adminId' does not exist in the current context.

            if (txt_adminId.Text == "root")
            {
                // 'txt_adminPw' does not exist in the current context.
                if (txt_adminPw.Password == "password")
                {
                    var msg_login = new MessageDialog("Logged in!");
                    await msg_login.ShowAsync();
                }
                else
                {
                    var msg_login = new MessageDialog("Wrong password!");
                    await msg_login.ShowAsync();
                }
            }
            else
            {
                var msg_login = new MessageDialog("Wrong combo!");
                await msg_login.ShowAsync();
            }
        }

Also, I would recommend to use String.Equals instead of ==, because you would like to compare values rather than reference. Note that String.Equals compares proper values while == also takes into consideration their references.

See: Why would you use String.Equals over ==?

Community
  • 1
  • 1
Shmwel
  • 1,697
  • 5
  • 26
  • 43