关于演员错误的困惑在以前工作.. [英] Confused about a cast error was working before..

查看:96
本文介绍了关于演员错误的困惑在以前工作..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我不知道为什么我会收到施法错误。我可以作为管理员登录没有问题。我使用继承与客户类继承用户的属性。管理员只是普通用户。客户只有一些额外的属性,但在早些时候之前我能够运行我的程序没有错误现在突然我得到这个错误。我很困惑它出来了我认为可能是编译器错误,所以我清理/重建仍然是一个错误... ..

  //  登录屏幕以及我的捕获条件中发生错误的位置*** private void BtnLoginUser_Click(object sender,RoutedEventArgs e){ 
尝试 {
loginUser = SQLuserAccess.UserLogin(txtUsername.Text,txtPassword.Password.ToString());
if (txtUsername.Text == loginUser.Username&& txtPassword.Password == loginUser.Password){
if (loginUser.IsAdmin == true ){
Window showAdminSrc = new AdminWindow(loginUser);
showAdminSrc.Show();
关闭();
} else if (loginUser.IsAdmin == false ){
Customer CastUser = new Customer();
CastUser =(Customer)loginUser;
Window nonAdmin = new CustomerScreen(CastUser); // ((Customer)loginUser);
nonAdmin.Show();
关闭();
} else
lblInvalidText.Content = 管理员不是真或假!;
}
else {
lblInvalidText.Visibility = Visibility.Visible;
}
} catch (Exception ex){
ex.Message.ToString();
throw ex;
}
}
} // CUSTOMER SCREEN LOADING CONSTRUCTOR NOTHING SPECIAL .. public CustomerScreen(用户用户){
customerUser =(客户)用户;
InitializeComponent();
// 在Src / * future上显示用户'用户名' - 数据绑定。
TxtBlockName.Text = Hello: + Environment.NewLine + customerUser.Username;
}





我的尝试:



我尝试过几种不同的方法。

我试着像在管理部分那样使用它,因为它可以在那里工作。

这是有效的更早,但现在只有很少的变化,我得到这个错误。我没有改变本节中的任何内容以获取此错误。

解决方案

我们看不到 loginUser user 这是该代码中唯一的显式转换;在你投射之前,我们也看不到 SQLuserAccess.UserLogin 返回什么类型,或 user 中的内容。



所以在调试器中运行你的代码,看看变量包含的确切内容。



猜测,你说客户来自用户,但我怀疑UserLogin返回基本用户类的成员,而不是客户 - 你不能向上转发任何东西。

  public   class 用户{} 
public class 客户:用户{}
...
客户c;
用户u = 客户(); // 很好,客户来自用户。
c =(客户)u; // 也没关系,你包含一个客户。
u = 用户();
c =(客户)u; // 错误,因为您无法向上转播用户。


Hello all,
I’m not sure why I’m getting a cast error. I can login as an Admin with no problem. I am using inheritance with customer class inheriting properties from user. A admin is just a normal user. Customer only has a few extra properties but before a lil earlier I was able to run my program no errors now all a sudden im getting this error. I’m so confused it came out of no where I thought maybe it’s a compiler error so I cleaned/rebuilt and still a error…..

//LOGIN SCREEN AND WHERE THE ERROR OCCURS IN MY CATCH CLAUSE *** 			private void BtnLoginUser_Click(object sender, RoutedEventArgs e)    {
                try     {
                    loginUser = SQLuserAccess.UserLogin(txtUsername.Text, txtPassword.Password.ToString());
                    if (txtUsername.Text == loginUser.Username && txtPassword.Password == loginUser.Password)   {
                        if (loginUser.IsAdmin == true){
                            Window showAdminSrc = new AdminWindow(loginUser);
                            showAdminSrc.Show();
                            Close();
                        }else if (loginUser.IsAdmin == false)   {
                            Customer CastUser = new Customer();
                                CastUser = (Customer)loginUser;
          Window nonAdmin = new CustomerScreen(CastUser);//((Customer)loginUser);
                            nonAdmin.Show();
                            Close();
                        }  else
                            lblInvalidText.Content = "Admin is not True or False!";
                    }
                    else  {
                        lblInvalidText.Visibility = Visibility.Visible;
                    }
                } catch(Exception ex)     {
                    ex.Message.ToString();
                    throw ex;
                }
            }
        }	//CUSTOMER SCREEN LOADING CONSTRUCTOR NOTHING SPECIAL..       		 public CustomerScreen(User user) {
            customerUser = (Customer)user;
            InitializeComponent();
            //Display Users 'username' on Src / *future - Data Binding.
            TxtBlockName.Text = "Hello:" + Environment.NewLine + customerUser.Username;
        }



What I have tried:

I have tried casting a few different ways.
I tried having it as I do in the Admin section because it works there.
This was working earlier but now with very few changes, I get this error. I DID NOT CHANGE ANYTHING IN THIS SECTION for getting this error.

解决方案

WE can't see the definitions of loginUser and user which are the only explicit casts in that code; we also can't see what type SQLuserAccess.UserLogin returns, or what is in user before you cast that.

So run your code in the debugger, and look at exactly what the variables contain.

At a guess, you say that Customer is derived from User but I'd suspect that UserLogin returns a member of the base User class, not a Customer - and you can't "up-cast" anything.

public class User {}
public class Customer : User {}
...
Customer c;
User u = new Customer ();  // Fine, Customer derived from User.
c = (Customer) u;          // Also fine, u contains a Customer.
u = new User();
c = (Customer) u;          // Error because you can't upcast the User.


这篇关于关于演员错误的困惑在以前工作..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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