简体   繁体   English

字符串变量= a textbox.text

[英]String variable to = a textbox.text

Everywhere I have researched this should work and yet it does not... I am basically trying to get my variable stores in one class to work on my main form. 我研究过的每个地方都应该工作,但它不会...我基本上试图让我的变量商店在一个类中工作在我的主表单上。 I keep getting an error code of 我一直收到错误代码

Cannot implicitly convert type string to Form1.Employee 无法将类型string隐式转换为Form1.Employee

My form code 我的表格代码

Employee FirstName = new Employee();
if (Directions.Text == "Please Enter Employee First Name")
{
    FirstName = Info.Text;
    Directions.Text = "Please Enter Employee Last Name";
}

In my second class I have 在我的第二堂课中

public class Employee
{
    public string FirstName;
}
Employee employee = new Employee();
if (Directions.Text == "Please Enter Employee First Name")
{
    employee.FirstName = Info.Text;
    Directions.Text = "Please Enter Employee Last Name";
}

In your first code, FirstName is an instance of an Employee class. 在您的第一个代码中, FirstNameEmployee类的实例。 In your 在你的

FirstName = Info.Text;

line, you try to assign .Text (which is string ) property to your class instance which is not possible. 你尝试将.Textstring )属性赋给你的类实例,这是不可能的。 That's why compiler says; 这就是编译器说的原因;

Cannot implicitly convert type 'string' to Employee 无法将类型'string'隐式转换为Employee

You can assign it's FirstName field of your instance to your Info.Text value. 您可以将实例的FirstName字段分配给Info.Text值。 Like; 喜欢;

FirstName.FirstName = Info.Text;

Of course would be better if you put a better name on your Employee instance. 如果你在Employee实例上放一个更好的名字,当然会更好。

Problem is in following line since FirstName is an instance of Employee. 问题出现在以下行中,因为FirstName是Employee的一个实例。

FirstName = Info.Text;

It should be 它应该是

FirstName.FirstName = Info.Text;

FirstName before dot(.) is the instance of Employee Class and dot(。)之前的FirstName是Employee Class和的实例
FirstName after dot(.) is the field of Employee class dot(。)之后的FirstName是Employee类的字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM