试图使用getMethod访问数据和在另一个类中使用它Trying to use a getMethod to access data and use it in another class

- 此内容更新于:2015-01-06
主题:

原文:

I am trying to use a getMethod to access data from form1.cs and use it in another class. Here I am getting errors that I do not understand.

Error :

"Invalid token '=' in class, struct, or interface member declaration" and
"Invalid token '(' in class, struct, or interface member declaration".

The following is code:

Method inside form1.cs :

// select_emp_id_number is an INT

    public int get_select_emp_id_number()
    {
        return select_emp_id_number;
    }

Code in Absence class : I am trying to get the Data from form1.cs

    private int a = 0;
    Form1 test = new Form1();
    a = test.get_select_emp_id_number();

I didn't see anything wrong here. Can anyone see where I did wrong & help me

JacquesB的回复:你不能有一个任务(第三行)直接在类的内部。它必须在一个方法或构造函数。

(原文:You cannot have an assignment (the third line) directly inside a class. It has to be inside a method or constructor.)

i486的回复:显示更多的代码之前和之后私人int = 0。现在尚不清楚这是在课堂上,法等。

(原文:Show more code before and after private int a = 0. Now it is not clear whether this is in class, method, etc.)

Dirk的回复:你可能不要# 39;想要实例化一个新的Form1但使用这# 39;已经显示给用户。

(原文:And you probably don't want to instantiate a new Form1 but use the one that's already shown to the user.)

Cash Money的回复:这一块私人面前使用系统= 0;使用System.Collections。泛型,使用系统。Linq,使用系统。使用System.Threading文本;。使用System.Data任务;。SqlServerCe;名称空间EmployeePerformanceSystem {类没有{

(原文:This chunk lies before private in a = 0 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlServerCe; namespace EmployeePerformanceSystem { class Absence {)

解决方案:
而不是, 试一试: int = test.get_select_emp_id_number(); 这样做的原因是,你不能在函数声明私有变量。相反,他们用于定义其他类的变量可用性解决方案。
原文:

Rather than,

private int a = 0;

try:

int a = test.get_select_emp_id_number();

The reason for this, is that you cannot declare private variables in a function. Instead they are used to define the variables availability to other classes in the solution.

Cash Money的回复:我曾尝试过这种方法,我收到一个不同的错误当这样做——“一个字段初始值设定项不能引用非静态字段,方法,或者财产& # 39;EPS.Absence。测试# 39;“-我不清楚如何解决这个问题。

(原文:I have previously tried this approach, I receive a different error when doing this - "A field initializer cannot reference the non-static field, method, or property 'EPS.Absence.test'" - I am not clear on how to fix this.)

Vash的回复:@CashMoney你必须把这条线在你的类的构造函数或主要方法不同,你使用它。

(原文:@CashMoney you have to put this line in the constructor of your class or main method depending where you use it.)

解决方案:
原文:

I assume that your calling code is inside a function. You may not declare a variable as private inside a function.

change

private int a = 0;

to

int a = 0;

or simply

int a = test.get_select_emp_id_number();

Or if you have your code inside the class and not in a function, move the part

a = test.get_select_emp_id_number();

inside a function or the constructor

Edit:

According to your comments, maybe this example can help you. You can pass a reference to Form1 (or better an interface that Form1 implements) to your Absence class:

public class Absence
{
    private Form1 form1;

    public Absence(Form1 form1)
    {
        this.form1 = form1;
    }

    private void MyFunction()
    {
        int a = form1.get_select_emp_id_number();
    }
}
Cash Money的回复:我曾尝试过这种方法,我收到一个不同的错误当这样做——“一个字段初始值设定项不能引用非静态字段,方法,或者财产& # 39;EPS.Absence。测试# 39;“-我不清楚如何解决这个问题。

(原文:I have previously tried this approach, I receive a different error when doing this - "A field initializer cannot reference the non-static field, method, or property 'EPS.Absence.test'" - I am not clear on how to fix this.)

Flat Eric的回复:是否需要一个字段或者你只是需要它作为一个变量在函数吗?

(原文:Does a have to be a field or do you just need it as a variable inside the function?)

Cash Money的回复:我不担心我如何获取数据,只要它可以在没有类,我需要使用SQL的数据的目的

(原文:Im not concerned how I get the data, as long as it is available in the absence class, I need to use the data for SQL purposes)

Flat Eric的回复:没有另一种形式或只是一些类?

(原文:Is Absence another form or just some class?)

Cash Money的回复:只是一个正常的类

(原文:its just a normal class)