在C#中创建类和对象
问题描述:
第1次制作本课程
1st make This Class
{
public class Customer
{
public Customer()
{
}
private string m_Name;
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
private string m_CusCode;
public string CusDese
{
get { return m_CusCode; }
set { m_CusCode = value; }
}
}
}
和第二步此类
And 2nd Step this Class
public Customer GetCustomer(string pCusCode)
{
Customer c = new Customer();
c.Name = "asif";
return c;
}
和第3个MAke这个班级
And 3rd MAke This Class
private void button1_Click(object sender, EventArgs e)
{
Customer c2 = new Customer();
GetCustomer("18-020-00000005");
textBox1.Text = c2.Name;
}
private void Form1_Load(object sender, EventArgs e)
{
}
在第二步数据显示名称等所有并正确返回c<<
但是当按钮功能加载时在任何变量中都没有数据
我做了什么错误???
答
你好,
更改button1_click,如下所示。
Hello,
Change button1_click as shown below.
private void button1_Click(object sender, EventArgs e)
{
Customer c2 = Customer.GetCustomer("18-020-00000005");
textBox1.Text = c2.Name;
}
忘记添加以下内容
将GetCustomer方法设为静态
Forgot to add following
Make the GetCustomer mthod as static
public static Customer GetCustomer(string pCusCode)
{
Customer c = new Customer();
c.Name = "asif";
return c;
}
问候,
Regards,
嗨saimm,
我希望将返回对象分配给另一个对象可以解决问题。
请尝试以下代码。
Hi saimm,
I hope assigning returning object to another object would resolve the issue.
Try the below code.
Customer c2 = new Customer();
c2= GetCustomer("18-020-00000005");
但是为什么在地球上你将该字符串值传递给GetCustomer()函数;-)
问候,
RK
But why in the earth you are passing that string value to GetCustomer() function ;-)
Regards,
RK
嗨saimm,
1.错误:你不要将GetCustomer
的结果分配给c2
变量。顺便说一句。你不需要分配两次。所以这样做:
Hi saimm,
1. Error: You don''t assign the result ofGetCustomer
to yourc2
variable. Btw. you don''t need to assign it twice. So do it like this:
Customer c2 = GetCustomer("blablabla");
textbox1.Text = c2.Name;
2.错误:你对字符串参数 pCusCode 在 GetCustomer
方法中。因此,使用不同的参数调用GetCustomer没有任何效果....
2. Error: You do nothing with the string parameter pCusCode
in the GetCustomer
method. So there is no effect in calling GetCustomer with different parameters....