如何将常见的C#代码添加到Visual C#2008 Express项目/解决方案中?

时间:2022-10-30 14:02:59

(I still feel like a complete newbie in MS Visual environments... so please bear with!)

(我仍然觉得在MS视觉环境中是一个完整的新手...所以请忍受!)

I'm using Microsoft Visual C# 2008 Express Edition.

我正在使用Microsoft Visual C#2008 Express Edition。

I have a project and in that project are two different forms. The .cs file for each form starts out:

我有一个项目,在那个项目中有两种不同的形式。每个表单的.cs文件开头:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyNameSpace
{
    public partial class MyFormName : Form
    {
...

(...and the second is "MyFormName2" but no differences besides that)

(......第二个是“MyFormName2”,但除此之外没有区别)

I want to write a function that I know both forms are going to need to access. I right-clicked on my project, selected "Add", selected "New Item" then selected "Code File" and named my file "Common.cs" and it gave me a completely blank file that's in my project.

我想写一个我知道两种形式都需要访问的函数。我右键单击我的项目,选择“添加”,选择“新项目”,然后选择“代码文件”并命名我的文件“Common.cs”,它给了我一个完全空白的文件,在我的项目中。

How do I set this up...? I thought I should do the following...

我该怎么设置......?我以为我应该做以下......

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyNameSpace
{
}

...but then when I try to add a function like:

...但是当我尝试添加如下函数时:

public void mytestfunc() { } within that namespace I get the following error:

public void mytestfunc(){}在该命名空间内我收到以下错误:

"Expected class, delegate, enum, interface, or struct"

“预期的类,委托,枚举,接口或结构”

How do I set things up so I can have "mytestfunc" be available to both MyFormName and MyFormName2?

如何设置,以便MyFormName和MyFormName2可以使用“mytestfunc”?

Thanks!

-Adeena

UPDATE: Understand (now) that everything must be in a class, but then I don't understand how to really use it. Does that mean I have to create an object? This common function happens to just be some math...

更新:理解(现在)一切都必须在一个类中,但后来我不明白如何真正使用它。这是否意味着我必须创建一个对象?这个常见的功能恰好是一些数学...

so now if I have this:

所以,如果我有这个:

namespace MyNameSpace
{
    public class MyCommonClass
    {
        public void testFunc()
        {
            MessageBox.Show("Hee hee!");
            return;
        }
    }
}

...how do I call testFunc from my Form? Must I do the following:

...如何从我的表单中调用testFunc?我必须做以下事情:

MyCommonClass temp = new MyCommonClass;
temp.testFunc();

or is there another way to call testFunc?

还是有另一种方法来调用testFunc?

4 个解决方案

#1


If you do something like:

如果您执行以下操作:

namespace MyNameSpace
{
    public class myclass
    {
        public myMethod()
        {
            // Code
        }
    }
}

You will be able to instantiate and access it. If you change it to:

您将能够实例化并访问它。如果您将其更改为:

namespace MyNameSpace
{
    public class myclass
    {
        public static myMethod()
        {
            // Code
        }
    }
}

You will be able to call myClass.myMethod without instantiating a new myClass.

您可以在不实例化新myClass的情况下调用myClass.myMethod。

#2


The short answer is that everything needs to be inside a class; I'd suggest you sit down with a basic tutorial to help you get to grips with the basics...

简短的回答是,一切都需要在课堂内;我建议你坐下来阅读一本基础教程,以帮助你掌握基础知识......

#3


Code need to be inside classes.

代码需要在类内部。

It would look something like this:

它看起来像这样:

using System;

namespace MyNameSpace
{
   public class CommonHelper
   {
       public string FormatMyData(object obj)
       {
            //do something
            return String.Empty;
       }
   }
}

#4


If the function you call is not related to the forms, make it static

如果您调用的函数与表单无关,请将其设置为静态

namespace myns
{
    public static class myhelper
    {
        public static void DoSomething()
        {
        }
    }
}

and call the method using myhelper.DoSomething();

并使用myhelper.DoSomething()调用该方法;

If the function you want to call is somehow form-related, e.g. common functionality across multiple forms, derive a class from Form (does not need a visual form) and make it base class of the visual forms:

如果您要调用的函数以某种方式与表单相关,例如跨多个表单的通用功能,从Form派生一个类(不需要可视化表单)并使其成为可视表单的基类:

namespace myns
{
    public class MyFormBase : Form
    {
        protected void DoSomethingWithTheForm()
        {
        }
    }
}

and in your form's .cs:

并以您的形式.cs:

namespace myns
{
    public partial class MyFormName : MyFormBase
    {
    }
}

#1


If you do something like:

如果您执行以下操作:

namespace MyNameSpace
{
    public class myclass
    {
        public myMethod()
        {
            // Code
        }
    }
}

You will be able to instantiate and access it. If you change it to:

您将能够实例化并访问它。如果您将其更改为:

namespace MyNameSpace
{
    public class myclass
    {
        public static myMethod()
        {
            // Code
        }
    }
}

You will be able to call myClass.myMethod without instantiating a new myClass.

您可以在不实例化新myClass的情况下调用myClass.myMethod。

#2


The short answer is that everything needs to be inside a class; I'd suggest you sit down with a basic tutorial to help you get to grips with the basics...

简短的回答是,一切都需要在课堂内;我建议你坐下来阅读一本基础教程,以帮助你掌握基础知识......

#3


Code need to be inside classes.

代码需要在类内部。

It would look something like this:

它看起来像这样:

using System;

namespace MyNameSpace
{
   public class CommonHelper
   {
       public string FormatMyData(object obj)
       {
            //do something
            return String.Empty;
       }
   }
}

#4


If the function you call is not related to the forms, make it static

如果您调用的函数与表单无关,请将其设置为静态

namespace myns
{
    public static class myhelper
    {
        public static void DoSomething()
        {
        }
    }
}

and call the method using myhelper.DoSomething();

并使用myhelper.DoSomething()调用该方法;

If the function you want to call is somehow form-related, e.g. common functionality across multiple forms, derive a class from Form (does not need a visual form) and make it base class of the visual forms:

如果您要调用的函数以某种方式与表单相关,例如跨多个表单的通用功能,从Form派生一个类(不需要可视化表单)并使其成为可视表单的基类:

namespace myns
{
    public class MyFormBase : Form
    {
        protected void DoSomethingWithTheForm()
        {
        }
    }
}

and in your form's .cs:

并以您的形式.cs:

namespace myns
{
    public partial class MyFormName : MyFormBase
    {
    }
}