C# 当前上下文错误
调用方法C# 当前上下文错误,c#,compiler-errors,C#,Compiler Errors,调用方法Globals.Global.instantialeblowerobj()时遇到问题公开frmMain()。我收到错误“当前上下文中不存在名称'Globals.Global.instantialeblowerobj'。我将所有类和方法都设置为public,我无法解决这个问题 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System
Globals.Global.instantialeblowerobj()时遇到问题代码>公开frmMain()
。我收到错误“当前上下文中不存在名称'Globals.Global.instantialeblowerobj'。我将所有类和方法都设置为public,我无法解决这个问题
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 WetVacClient;
using Globals;
using Globals.Global;
namespace Globals
{
public class Global
{
public Blower[] _Blower = new Blower[4];
public void InstantiateBlowerObj()
{
for (int i = 1; i < 4; i++)
_Blower[i] = new Blower(i);
}
}
}
namespace WetVacClient
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
Globals.Global.InstantiateBlowerObj();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用WetVacClient;
使用Globals;
使用Globals.Global;
名称空间全局变量
{
公共类全球
{
公共鼓风机[]_鼓风机=新鼓风机[4];
public void实例化eblowerobj()
{
对于(int i=1;i<4;i++)
_鼓风机[i]=新鼓风机(i);
}
}
}
命名空间客户端
{
公共部分类名称:表单
{
公共财政收入()
{
初始化组件();
Globals.Global.instantialeblowerobj();
}
}
}
使其成为静态的
您正试图在静态上下文中访问非静态成员
public static void InstantiateBlowerObj()
{
for (int i = 1; i < 4; i++)
_Blower[i] = new Blower(i);
}
publicstaticvoid实例化eblowerobj()
{
对于(int i=1;i<4;i++)
_鼓风机[i]=新鼓风机(i);
}
您需要将方法和\u属性都设置为静态
public static Blower[] _Blower = new Blower[4];
public static void InstantiateBlowerObj()
{
for (int i = 1; i < 4; i++)
_Blower[i] = new Blower(i);
}
Globals.Global g=new Globals.Global();
g.InstantiateBlowerObj();