关于C#变量访问 [英] about c# variable accessing

查看:70
本文介绍了关于C#变量访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用属性将一种形式的变量访问另一种形式的变量.
我已经修改了这个问题............
我有一个登录表单,然后为员工创建了一个登录表单.
然后,我编写了两个属性以从登录表单到登录表单访问用户名和ID.

how can i access a variable of one form to another using property.
I have modified this question............
i have a login form and then i created one sign in form for employee.
then i wrote two properties to access the username and id from the login form to the sign in form.

public string getUname
       {
           get
           {
               return this.uname;
           }
           set
           {
               this.uname = value;
           }
       }
       #endregion

       #region // getUserid
       public string getUserid
       {
           get
           {
               return this.Userid;
           }
           set
           {
               this.Userid = value;
           }
       }


确实有效.
登录页面上的以下代码为属性分配值

登录登录=新SignIn();
signin.getUname =用户名;
signin.getUserid = id;

但是随后我在其中添加了MDI表单,这意味着我以正确的登录信息打开了MDI,然后从MDI中打开了登录表单并将这些变量值传递给数据库以进行检查.
MDI表单上的代码

私有void MDI_EmployeeAttendance_Load(对象发送者,EventArgs e)
{
登录frmlogin = new Login();
登录登录=新SignIn();

uname = signin.getUname;
uid = signin.getUserid;

uname = frmlogin.username;
uid = frmlogin.id;
}



但是现在我在登录页面上写的属性无法从登录页面获取值.所以调试后可能没有找到什么问题.当我将登录表单中的值分配给属性时,它会分配给它,但是当登录表单打开时,属性变量为null.因此,请帮助我...


actually it worked.
following code on login page to assign values to property

SignIn signin = new SignIn();
signin.getUname = username;
signin.getUserid = id;

but then i added a MDI form within them means i opened MDI with correct login and then from MDI i am opening a sign in form and passing those variable values to database to check.
code on mdi form

private void MDI_EmployeeAttendance_Load(object sender, EventArgs e)
{
Login frmlogin = new Login();
SignIn signin = new SignIn();

uname = signin.getUname;
uid = signin.getUserid;
OR
uname=frmlogin.username;
uid=frmlogin.id;
}



but now the properties i have been written on sign in page are not getting the values from the login page. so what might be the problem i dont found after debugging. when i assign values from login form to property it assigns to it but when sign in form opens the property variables are null. so please help me...

推荐答案

您可以将该属性设置为公共属性,并通过表单实例对其进行访问.如果需要使用其他方法来执行此操作,则可以使表单成为成员变量,以便可以使用它.

如有疑问,也可以发布代码,以便我们告诉您如何解决.
You make the property public and access it via the form instance. If you need to do this in other methods, you make the form a member variable so you can get to it.

You also post code when you have a question so we can tell you how to fix it.


这是有关表单协作的常见问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA


这篇关于关于C#变量访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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