如何在C#中引用类 [英] How to Reference a Class in C#

查看:436
本文介绍了如何在C#中引用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我对C#比较新。我实际上是在VB6之后停止编码。我想知道是否有人可以帮助我。我不能为我的生活弄清楚如何调用类中的方法或函数。



所以下面我的共享代码我希望有人可能指出我正朝着正确的方向前进。

On按钮点击Id喜欢调用我写的以下代码的函数。

有点像使用.bas并调用函数VB中的Library.FunctionName。无论如何感谢你的帮助!



Hi All,

I am relatively new to C#. I actually stopped coding just after VB6. I was wondering if someone could assist me. I cannot for the life of me figure out how to call methods or functions that are in a class.

So Ill share code below I have in hopes someone could maybe point me in the right direction.
On Button click Id like to call the function for the following code I have written.
Kind of like using a .bas and calling the function Library.FunctionName in VB. Anyways thanks for your help!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceProcess;
using System.IO;
using System.Management;

namespace TunerReconstruct
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ServiceController myService = new ServiceController();
                myService.ServiceName = "Service1";
                string svcStatus = myService.Status.ToString();
                myService.Stop();
                MessageBox.Show("Service1 Service is now stopping.", "Windows Service Controller", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

          // This will catch any unexpected errors while stopping the windows service.
            catch (Exception ex)
            {
                string strMessage = ex.Message;
                MessageBox.Show("An error occured stopping the Service1 Service" + strMessage, "Service1 Repair Controller", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }
    }
}





我有一个我的项目中的类名为Library,

Id喜欢按钮1的代码单击类中



当用户单击按钮1时,Id就像它调用类函数所以我可以添加多个函数,所以我可以看到哪些部分可能会出错。



I have a Class in my project named Library,
Id like the code for Button 1 Click in the Class

When the user clicks button one, Id like it call the Class Function so I can add multiple functions with try so I can see what sections may error out.

推荐答案

有两种方法可以访问类方法(假设访问说明符允许你,但我们假设所有方法都是 public ,这意味着任何类都可以访问它们。



第一个是静态访问:

There are two ways to access class methods (assuming that the access specifiers permit you to, but let's assume that all the methods are public which means that any class can access them).

The first is a static access:
int result = Library.Multiply( 2, 5 );



这需要静态定义:


This requires a static definition:

public class Library
   {
   private int myValue = 666;
   public static int Multiply(int a, int b) { return a * b; }
   }

这样做的缺点是Multiply方法只能访问Library类的 static 成员:它无法访问类实例成员。因此,Multiply无法访问myValue。



另一种是使用库的实例:

The disadvantage of this is that the Multiply method can only access static members of the Library class: it cannot access class instance members. Thus, Multiply cannot access myValue.

The other is to use an instance of the Library:

Library lib = new Library();
int i = lib.Multiply( 2, 10 );



这需要一个实例定义:


This requires an instance definition:

public class Library
   {
   private int myValue = 666;
   public int Multiply(int a, int b) { return a * b; }
   }

在这个版本中,Multiply可以在需要时访问myValue,因为它可以访问Library类的所有实例成员。

In this version, Multiply can access myValue if it needs to, because it has access to all the instance members of the Library class.


这篇关于如何在C#中引用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆