不理解类和用法
不理解类和用法
我有一个VS2010项目,该项目有许多类(项目中的子项目) 我正在尝试在类中使用方法,就像在本地 winforms 项目中一样 IE
using System.Diagnostics;
namespace DataBaseAccess
{
public class Class1
{
string[] lines = System.IO.File.ReadAllLines("names.txt");
public string startgenerationofnames()
{
foreach (string value in lines)
{
Debug.WriteLine(lines);
//call next class with the current value
}
}
}
}
我想使用 winforms 中的这个类,我已经创建了这个类。
DataBaseAccess.Class1 makenames = new DataBaseAccess.Class1();
并希望像没有课程时那样以正常方式使用它 IE
DataBaseAccess.Class1.startgenerationofnames();
我不期望返回值,也不期望从类中得到任何东西,除了运行 Debug.WriteLine(lines); 我显然无法理解这个基本任务,并且已经搜索了好几天了。 感谢您的帮助
Not understanding classes and usage
I have a VS2010 project, This project has a number of classes (sub projects in the Project)
I am trying to use a method in the class as I would in a local winforms project
IE
using System.Diagnostics;
namespace DataBaseAccess
{
public class Class1
{
string[] lines = System.IO.File.ReadAllLines("names.txt");
public string startgenerationofnames()
{
foreach (string value in lines)
{
Debug.WriteLine(lines);
//call next class with the current value
}
}
}
}
I would like to use this class from winforms, I have created the class.
DataBaseAccess.Class1 makenames = new DataBaseAccess.Class1();
and would like to use it like I would in a normal way as I would if there was no class
IE
DataBaseAccess.Class1.startgenerationofnames();
I am not expecting return values, nor am I expecting anything from the class, other then to run the Debug.WriteLine(lines);
I am obviosly failing to understand this basic task and have been searching for it for days already.
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将方法和类转换为静态以调用该方法,而无需该类的实例。
现在你可以像这样调用它:
You can convert your method and class to be static to call the method without needing an instance of the class.
Now you can invoke it like this:
将您的类创建为静态类和静态方法。然后您可以调用该方法而无需实例化该类。
Create your class as static class and static method. Then you can call the method without instantiating the class.
有两件事... 1 您的 start Generationofnames 需要返回一个字符串...或者您可以将其更改为 void 返回类型。
2 将读取所有行的加载移动到函数本身中...作为对象构造的一部分,您不想做类似的事情...因为它会给您一个不太明显的错误。
试试这个...
There are 2 things... 1 your startgenerationofnames needs to return a string... or you could change it to a void return type.
2 move the loading of the read all lines into the function itself... you don't want to do anything like that as part of the object construction... because it will give you an error that is less obvious to what is wrong.
Try this...
您不需要将类设为静态,只需将方法设为静态即可。但是,您当前无法执行此操作,因为您的方法使用类的实例成员:
string[]lines ...
方法的标准静态定义包括
static
关键字:public static string start Generationofnames()
我认为您不希望返回(?),并且您需要将这些行移到方法中(或使它们在类中静态):
如果您不希望要实例化类,您可以将类本身设为静态:
public static class Class1
这会强制类内的所有成员都是静态的,并阻止人们通过以下方式实例化它:
Class1 c = new Class1();
该类与您期望的静态方法一样:
Class1.StartGenerationOfNames();
You don't need to make the class static, just the method. However, you cannot do this currently as your method uses instance members of the class:
string[] lines ...
A standard static definition of a method includes the
static
keyword:public static string startgenerationofnames()
I think you want no return (?), and you need to move the lines into the method (or make them static in the class):
If you don't want the class to be instantiated at all, you can make the class itself static:
public static class Class1
This then forces any members inside the class to be static and stops people from being able to instantiate it via:
Class1 c = new Class1();
Usage of the class is as you'd expect with static methods:
Class1.StartGenerationOfNames();
您可以定义 public string start Generationofnames() 来使用它而不实例化 Class1,但在这种情况下会失败,因为您使用(在
start Generationofnames 内部))非静态变量。
你可以使用:
You could define
public string startgenerationofnames()
to use it without instantiating Class1, but in this case this fails because you're using (insidestartgenerationofnames
) variables that are not static.You could use: