C# 在另一个.cs文件中引用的静态
我忽略了一些我认为很简单的事情。我有一张带复选框的表格。我需要知道是否在不同的cs文件/类中选中了该复选框,以了解是否要设置列标题选项1或选项2。C# 在另一个.cs文件中引用的静态,c#,C#,我忽略了一些我认为很简单的事情。我有一张带复选框的表格。我需要知道是否在不同的cs文件/类中选中了该复选框,以了解是否要设置列标题选项1或选项2。 Form1(公共部分类)代码: 在Export1类中,我有一个私有的void CreateCell1,它接收要导出的数据(从datatable创建excel文件)。我无法工作的代码部分是: if (Form1.Checked.Equals("true")) { newRow["Option1"] = date2; } else { n
Form1(公共部分类)代码: 在Export1类中,我有一个私有的void CreateCell1,它接收要导出的数据(从datatable创建excel文件)。我无法工作的代码部分是:
if (Form1.Checked.Equals("true"))
{
newRow["Option1"] = date2;
}
else
{
newRow["Option2"] = date2;
}
我正在获取-错误1非静态字段、方法或属性“Matrix1.Form1.Checked.get”需要对象引用
我忽略了什么 无论如何,您需要访问要检查的
Form1
的特定实例
有几种方法可以做到这一点:
public class SomeOtherClass
{
// One option is to have a public property that can be set
public Form1 FormInstance { get; set; }
// Another option is to have it set in a constructor
public SomeOtherClass(Form1 form1)
{
this.FormInstance = form1;
}
// A third option would be to pass it directly to the method
public void AMethodThatChecksForm1(Form1 form1)
{
if (form1 != null && form1.Checked)
{
// Do something if the checkbox is checked
}
}
// This method uses the local instance of the Form1
// that was either set directly or from the constructor
public void AMethodThatChecksForm1()
{
AMethodThatChecksForm1(this.FormInstance);
}
}
该类需要由实例form1
使用以下方法之一实例化:
// Pass the instance through the constructor
var someOtherClass = new SomeOtherClass(this);
// Or set the value of a property to this instance
someOtherClass.FormInstance = this;
// Or pass this instance to a method of the class
someOtherClass.AMethodThatChecksForm1(this);
无论如何,您需要访问要检查的
Form1
的特定实例
有几种方法可以做到这一点:
public class SomeOtherClass
{
// One option is to have a public property that can be set
public Form1 FormInstance { get; set; }
// Another option is to have it set in a constructor
public SomeOtherClass(Form1 form1)
{
this.FormInstance = form1;
}
// A third option would be to pass it directly to the method
public void AMethodThatChecksForm1(Form1 form1)
{
if (form1 != null && form1.Checked)
{
// Do something if the checkbox is checked
}
}
// This method uses the local instance of the Form1
// that was either set directly or from the constructor
public void AMethodThatChecksForm1()
{
AMethodThatChecksForm1(this.FormInstance);
}
}
该类需要由实例form1
使用以下方法之一实例化:
// Pass the instance through the constructor
var someOtherClass = new SomeOtherClass(this);
// Or set the value of a property to this instance
someOtherClass.FormInstance = this;
// Or pass this instance to a method of the class
someOtherClass.AMethodThatChecksForm1(this);
这里的问题正是编译器告诉你的。要访问属性,需要对象引用 请允许我解释一下 在C#中,默认情况下,类成员(字段、方法、属性等)是实例成员。这意味着它们与它们所属的类的实例相关联。这将启用以下行为:
public class Dog
{
public int Age { get; set; }
}
public class Program
{
public static void Main()
{
var dog1 = new Dog { Age: 3 };
var dog2 = new Dog { Age: 5 };
}
}
public class Foo
{
public static string bar = "bar";
}
public class Program
{
public static void Main()
{
Console.WriteLine(Foo.bar);
}
}
public class Form2 : Form
{
private Form1 parent;
public Form2(Form1 parent)
{
this.parent = parent;
InitializeComponent();
}
}
Dog
的两个实例都具有属性Age
,但是该值与Dog
的实例相关联,这意味着每个实例的值可能不同
在C#中,与许多其他语言一样,有一些东西被称为类的成员。当类成员声明为静态时,则该成员不再绑定到它所属的类的实例。这意味着我可以执行以下操作:
public class Dog
{
public int Age { get; set; }
}
public class Program
{
public static void Main()
{
var dog1 = new Dog { Age: 3 };
var dog2 = new Dog { Age: 5 };
}
}
public class Foo
{
public static string bar = "bar";
}
public class Program
{
public static void Main()
{
Console.WriteLine(Foo.bar);
}
}
public class Form2 : Form
{
private Form1 parent;
public Form2(Form1 parent)
{
this.parent = parent;
InitializeComponent();
}
}
Foo
类的bar
字段声明为static
。这意味着它对于Foo
的所有实例都是相同的。事实上,我们甚至不必初始化Foo
的新实例来访问它
您在这里面临的问题是,虽然Form1
不是一个静态类,Checked
不是一个静态属性,但您将其视为静态属性。为了使您尝试执行的操作正常工作,您需要创建Form1
的实例,并访问该实例的Checked
属性
根据您的程序的结构,有很多方法可以做到这一点。如果在您试图访问选中的的范围内创建了Form1
,那么这将非常简单。如果Form1
产生了新的作用域,那么通常的做法是在构造函数中传递对它的引用
例如,如果Form1
创建了一个新的Form2
,那么我们将执行以下操作:
public class Dog
{
public int Age { get; set; }
}
public class Program
{
public static void Main()
{
var dog1 = new Dog { Age: 3 };
var dog2 = new Dog { Age: 5 };
}
}
public class Foo
{
public static string bar = "bar";
}
public class Program
{
public static void Main()
{
Console.WriteLine(Foo.bar);
}
}
public class Form2 : Form
{
private Form1 parent;
public Form2(Form1 parent)
{
this.parent = parent;
InitializeComponent();
}
}
然后您可以访问整个Form2
中的parent
。当然,根据程序的结构,具体的实现将有所不同。然而,一般模式是相同的。将对Form1的引用从创建它的作用域传递到新类,然后从那里访问它。好吧,这里的问题正是编译器告诉您的。要访问属性,需要对象引用
请允许我解释一下
在C#中,默认情况下,类成员(字段、方法、属性等)是实例成员。这意味着它们与它们所属的类的实例相关联。这将启用以下行为:
public class Dog
{
public int Age { get; set; }
}
public class Program
{
public static void Main()
{
var dog1 = new Dog { Age: 3 };
var dog2 = new Dog { Age: 5 };
}
}
public class Foo
{
public static string bar = "bar";
}
public class Program
{
public static void Main()
{
Console.WriteLine(Foo.bar);
}
}
public class Form2 : Form
{
private Form1 parent;
public Form2(Form1 parent)
{
this.parent = parent;
InitializeComponent();
}
}
Dog
的两个实例都具有属性Age
,但是该值与Dog
的实例相关联,这意味着每个实例的值可能不同
在C#中,与许多其他语言一样,有一些东西被称为类的成员。当类成员声明为静态时,则该成员不再绑定到它所属的类的实例。这意味着我可以执行以下操作:
public class Dog
{
public int Age { get; set; }
}
public class Program
{
public static void Main()
{
var dog1 = new Dog { Age: 3 };
var dog2 = new Dog { Age: 5 };
}
}
public class Foo
{
public static string bar = "bar";
}
public class Program
{
public static void Main()
{
Console.WriteLine(Foo.bar);
}
}
public class Form2 : Form
{
private Form1 parent;
public Form2(Form1 parent)
{
this.parent = parent;
InitializeComponent();
}
}
Foo
类的bar
字段声明为static
。这意味着它对于Foo
的所有实例都是相同的。事实上,我们甚至不必初始化Foo
的新实例来访问它
您在这里面临的问题是,虽然Form1
不是一个静态类,Checked
不是一个静态属性,但您将其视为静态属性。为了使您尝试执行的操作正常工作,您需要创建Form1
的实例,并访问该实例的Checked
属性
根据您的程序的结构,有很多方法可以做到这一点。如果在您试图访问选中的的范围内创建了Form1
,那么这将非常简单。如果Form1
产生了新的作用域,那么通常的做法是在构造函数中传递对它的引用
例如,如果Form1
创建了一个新的Form2
,那么我们将执行以下操作:
public class Dog
{
public int Age { get; set; }
}
public class Program
{
public static void Main()
{
var dog1 = new Dog { Age: 3 };
var dog2 = new Dog { Age: 5 };
}
}
public class Foo
{
public static string bar = "bar";
}
public class Program
{
public static void Main()
{
Console.WriteLine(Foo.bar);
}
}
public class Form2 : Form
{
private Form1 parent;
public Form2(Form1 parent)
{
this.parent = parent;
InitializeComponent();
}
}
然后您可以访问整个Form2
中的parent
。当然,根据程序的结构,具体的实现将有所不同。然而,一般模式是相同的。传递引用t