Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从windows窗体访问公共静态类_C#_Visual Studio 2010_Image Processing_Static Methods_Windows Forms Designer - Fatal编程技术网

C# 如何从windows窗体访问公共静态类

C# 如何从windows窗体访问公共静态类,c#,visual-studio-2010,image-processing,static-methods,windows-forms-designer,C#,Visual Studio 2010,Image Processing,Static Methods,Windows Forms Designer,我希望我的问题能得到相关的回答,因为我是个新手 例如,我有两个名为Class1.cs和Form1.cs的编码。基本上,Class1.cs是进行图像过滤处理的程序,而在Form1中,是允许它从文件加载图像的程序 单击按钮处理后,我是否可以从Form1访问Class1 Class1.cs Form1.cs 我想知道是否有必要在“program.cs”上添加或编辑一些程序。 我希望我的问题能得到回答。非常感谢您抽出时间。您可以做以下两件事之一: 1.您可以使用静态变量访问Class1中的数据: nam

我希望我的问题能得到相关的回答,因为我是个新手

例如,我有两个名为
Class1.cs
Form1.cs
的编码。基本上,Class1.cs是进行图像过滤处理的程序,而在
Form1
中,是允许它从文件加载图像的程序

单击按钮处理后,我是否可以从
Form1
访问
Class1

Class1.cs

Form1.cs

我想知道是否有必要在“program.cs”上添加或编辑一些程序。
我希望我的问题能得到回答。非常感谢您抽出时间。

您可以做以下两件事之一:

1.您可以使用静态变量访问
Class1
中的数据:

namespace MyProgram
{
    class Class1
    {
        public static int Class1Variable;
    }
}

namespace MyProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void LoadImageButton_Click(object sender, EventArgs e)
        {
            //Load image logic
        }

        private void ResultButton_Click(object sender, EventArgs e)
        {
            Class1.Class1Variable = 1;
        }
    }
}
2.您可以在Form1中创建Class1的实例:

namespace MyProgram
{
    class Class1
    {
        public int Class1Variable;

        public Class1()
        {
        }
    }
}

namespace MyProgram
{
    public partial class Form1 : Form
    {
        public Class1 c1;

        public Form1()
        {
            InitializeComponent();
            c1 = new Class1();
        }

        private void LoadImageButton_Click(object sender, EventArgs e)
        {
            //Load image logic
        }

        private void ResultButton_Click(object sender, EventArgs e)
        {
            c1.Class1Variable = 1;
        }
    }
}

让我为静态类添加一个属性和一个方法,以使其易于理解。让
class1程序
如下所示:

public static class Class1Program
{  
    public static int MyProperty{get; set;} // is a static Property
    public static void DoSomething() // is a static method for performing the action
    {
        //my program for image filtering is starting at here
    }

}
现在,您可以访问
表单1
中的方法和属性,如下所示:

 private void ResultButton_Click(object sender, EventArgs e)
 {
    Class1.Class1Program.MyProperty = 20; // assign 20 to MyProperty
    int myint = Class1.Class1Program.MyProperty; // access value from MyProperty
    Class1.Class1Program.DoSomething();// calling the method to perform the action
 }

对文本格式感到抱歉。我不知道为什么代码块没有按应有的格式格式化。如果我以后有时间,我会尝试修改格式。谢谢。很好的解释。我会记下来的。谢谢你,先生,我刚才试过了,效果很好。但现在我遇到了问题,因为我有太多的方法需要在主静态方法下声明。
public static class Class1Program
{  
    public static int MyProperty{get; set;} // is a static Property
    public static void DoSomething() // is a static method for performing the action
    {
        //my program for image filtering is starting at here
    }

}
 private void ResultButton_Click(object sender, EventArgs e)
 {
    Class1.Class1Program.MyProperty = 20; // assign 20 to MyProperty
    int myint = Class1.Class1Program.MyProperty; // access value from MyProperty
    Class1.Class1Program.DoSomething();// calling the method to perform the action
 }