不理解类和用法

发布于 2024-12-11 07:59:59 字数 924 浏览 0 评论 0原文

不理解类和用法

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

待天淡蓝洁白时 2024-12-18 07:59:59

您可以将方法和类转换为静态以调用该方法,而无需该类的实例。

using System.Diagnostics;

namespace DataBaseAccess
{
    public static class Class1
    {
        private static string[] lines = System.IO.File.ReadAllLines("names.txt");

        public static void startgenerationofnames()
        {
            foreach (string value in lines)
            {
                Debug.WriteLine(lines);
                //call next class with the current value
            }
        }
    }
}

现在你可以像这样调用它:

DataBaseAccess.Class1.startgenerationofnames();

You can convert your method and class to be static to call the method without needing an instance of the class.

using System.Diagnostics;

namespace DataBaseAccess
{
    public static class Class1
    {
        private static string[] lines = System.IO.File.ReadAllLines("names.txt");

        public static void startgenerationofnames()
        {
            foreach (string value in lines)
            {
                Debug.WriteLine(lines);
                //call next class with the current value
            }
        }
    }
}

Now you can invoke it like this:

DataBaseAccess.Class1.startgenerationofnames();

将您的类创建为静态类和静态方法。然后您可以调用该方法而无需实例化该类。

Create your class as static class and static method. Then you can call the method without instantiating the class.

×眷恋的温暖 2024-12-18 07:59:59

有两件事... 1 您的 start Generationofnames 需要返回一个字符串...或者您可以将其更改为 void 返回类型。

2 将读取所有行的加载移动到函数本身中...作为对象构造的一部分,您不想做类似的事情...因为它会给您一个不太明显的错误。

试试这个...

 public static class Class1
                {

           public static void startgenerationofnames()
          {
               string[] lines = System.IO.File.ReadAllLines("names.txt");
               foreach (string value in lines)
              {
                Debug.WriteLine(lines);
                //call next class with the current value
              }
        }
    }

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...

 public static class Class1
                {

           public static void startgenerationofnames()
          {
               string[] lines = System.IO.File.ReadAllLines("names.txt");
               foreach (string value in lines)
              {
                Debug.WriteLine(lines);
                //call next class with the current value
              }
        }
    }
迷雾森÷林ヴ 2024-12-18 07:59:59

您不需要将类设为静态,只需将方法设为静态即可。但是,您当前无法执行此操作,因为您的方法使用类的实例成员:

string[]lines ...

方法的标准静态定义包括 static 关键字:

public static string start Generationofnames()

我认为您不希望返回(?),并且您需要将这些行移到方法中(或使它们在类中静态):

public static void StartGenerationOfNames()
{
    string[] lines = System.IO.File.ReadAllLines("names.txt");

    foreach (string line in lines)
    {
        Debug.WriteLine(line);
    }
}

如果您不希望要实例化类,您可以将类本身设为静态:

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):

public static void StartGenerationOfNames()
{
    string[] lines = System.IO.File.ReadAllLines("names.txt");

    foreach (string line in lines)
    {
        Debug.WriteLine(line);
    }
}

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();

送君千里 2024-12-18 07:59:59

您可以定义 public string start Generationofnames() 来使用它而不实例化 Class1,但在这种情况下会失败,因为您使用(在 start Generationofnames 内部))非静态变量。
你可以使用:

public class Class1
{
    static string[] lines = System.IO.File.ReadAllLines("names.txt");
    public static string startgenerationofnames()
    {
        foreach (string value in lines)
        {
             Debug.WriteLine(line); // not lines
             //call next class with the current value
        }
        return ""; // or what you need
     }
}

You could define public string startgenerationofnames() to use it without instantiating Class1, but in this case this fails because you're using (inside startgenerationofnames) variables that are not static.
You could use:

public class Class1
{
    static string[] lines = System.IO.File.ReadAllLines("names.txt");
    public static string startgenerationofnames()
    {
        foreach (string value in lines)
        {
             Debug.WriteLine(line); // not lines
             //call next class with the current value
        }
        return ""; // or what you need
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文