从另一个类在WinForm类中传递的Access对象 [英] Access Object that was passed in a WinForm class from another class

查看:47
本文介绍了从另一个类在WinForm类中传递的Access对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个类(用户仪表板) )根据传递给它的内容显示用户信息。我可以成功获得firstaname和lastname
以及用户的其他信息。但现在的问题是,我有一个在另一个类上的按钮,我想获得对成功传递给用户仪表板的对象的访问权限。所以,这里是我可以成功传递用户对象
并使用它的代码

 public partial class UserDashboard:Form 
{
公共用户用户;
private dbModel db;
public UserDashboard(用户用户)
{
InitializeComponent();
this.user = user;
db = new dbModel();
LoadUser(用户);
}
public void LoadUser(用户p)
{
颜色c = lbluserDisplayName.ForeColor = GetLabelForeColor(p);
if(c == null)
lbluserDisplayName.Text = user.lastname.ToUpper()+" " + user.firstname;
else if(c == Color.Red)
lbluserDisplayName.Text = user.lastname.ToUpper()+" " + user.firstname;
else
lbluserDisplayName.Text = user.lastname.ToUpper()+" " + user.firstname;
lblUserAddress.Text = user.street_address;
lblUserEmail.Text =" E:" + user.email;
lblUserHome.Text =" T:" + user.mobile;
lblUserTelephone.Text =" H:" + user.phone;

}
}

我试图在此处传递的同一个类上返回该对象,以便我可以在另一个类上使用它

公共用户测试(用户用户)
{
返回this.user = user;
}

这是按钮所在的另一个类,我需要根据传递给userDashboard的用户获取访问权限。但是,当我
测试对象时,它没有数据。

公共部分类StartPage:Form 
{
public StartPage()
{
InitializeComponent();

}
用户u;
private void button1_Click(object sender,EventArgs e)
{
user u = udash.Testing(u);
if(u == null)
{
MessageBox.Show(" Empty data");
} else
MessageBox.Show(u.firstname);

}

请问,根据传递到第一个类的内容,我无权访问此对象?感谢任何帮助。






解决方案

您遇到问题:


public 用户 测试 用户用户

{

   
返回 用户
= 用户 ;

}


这将获取您在参数"user"中传递的任何值,然后将其复制到this.user中,然后返回已复制的值。因此,它总是返回您传递的同一个用户。


然后,你这样称呼它:


...测试(你)


其中u未初始化,因此值为null。


因此,您取null,用null覆盖this.user然后返回null。显然它没有达到预期的结果。


解决方案是简单地返回this.user:


public 用户 测试

{

   
返回 用户;

}


将其称为


用户u = udash 测试 () ;



I have a class on a form (user dashboard) which displays user information based on what was passed to it. I could successfully get the firstaname and lastname and other info of the user. But the issue now is that, I have a button which is on another class, I want to gain access to the object that was successfully passed to the user dashboard. So, far here is the code which I could successfully passed the user object and use it

public partial class UserDashboard : Form
{
        public user user;
        private dbModel db;
        public UserDashboard(user user)
        {
            InitializeComponent();
            this.user = user;
            db = new dbModel();
            LoadUser(user);
        }
        public void LoadUser(user p)
        {
            Color c = lbluserDisplayName.ForeColor = GetLabelForeColor(p);
            if (c == null)
                lbluserDisplayName.Text = user.lastname.ToUpper() + " " + user.firstname;
            else if (c == Color.Red)
                lbluserDisplayName.Text = user.lastname.ToUpper() + " " + user.firstname;
            else
                lbluserDisplayName.Text = user.lastname.ToUpper() + " " + user.firstname;
            lblUserAddress.Text = user.street_address;
            lblUserEmail.Text = "E: " + user.email;
            lblUserHome.Text = "T: " + user.mobile;
            lblUserTelephone.Text = "H: " + user.phone;

        }
}

I tried to return the object on this same class that was passed here so that I could use it on another class

public user Testing(user user)
 {
    return this.user = user;
 }

Here is the other class where the button is located at which I need to gain access to based on the user that was passed to the userDashboard. But when I test the object, it has no data in it.

public partial class StartPage : Form
{
    public StartPage()
    {
        InitializeComponent();

    }
user u;
private void button1_Click(object sender, EventArgs e)
{
   user u = udash.Testing(u);
   if (u == null)
   {
      MessageBox.Show("Empty data");
    }else
     MessageBox.Show(u.firstname);

 }

Please, what are my not getting right to get access to this object based on what was passed unto the first class?? Appreciate any assistance.


解决方案

You have a problem here:

public user Testing(user user)
{
   
return this.user = user;
}

This takes whatever value you pass in the argument "user", then copies it into the this.user, and then returns the value that was copied. Therefore, it always returns the same user that you passed.

Then, you call it like this:

...Testing(u)

where u is not initialized and therefore the value is null.

So you take null, overwrite this.user with null and then return null. Obviously it doesn´t achieve the expected result.

The solution is to simply return this.user:

public user Testing()
{
   
return this.user;
}

Call it as

user u = udash.Testing();


这篇关于从另一个类在WinForm类中传递的Access对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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