当前上下文错误
Current Context error
我在调用 public frmMain()
中的方法 Globals.Global.InstantiateBlowerObj();
时遇到问题。我收到错误 "The name 'Globals.Global.InstantiateBlowerObj' does not exist in the current context." 我拥有所有 类 和 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();
}
}
}
成功static
.
您正在尝试访问静态上下文中的非静态成员。
public static void InstantiateBlowerObj()
{
for (int i = 1; i < 4; i++)
_Blower[i] = new Blower(i);
}
您需要将方法和 _Blower 设为静态属性静态
public static Blower[] _Blower = new Blower[4];
public static void InstantiateBlowerObj()
{
for (int i = 1; i < 4; i++)
_Blower[i] = new Blower(i);
}
否则创建一个 Global 实例并调用它的实例方法(但我认为这不是您想要的)。
Globals.Global g=new Globals.Global();
g.InstantiateBlowerObj();
我在调用 public frmMain()
中的方法 Globals.Global.InstantiateBlowerObj();
时遇到问题。我收到错误 "The name 'Globals.Global.InstantiateBlowerObj' does not exist in the current context." 我拥有所有 类 和 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();
}
}
}
成功static
.
您正在尝试访问静态上下文中的非静态成员。
public static void InstantiateBlowerObj()
{
for (int i = 1; i < 4; i++)
_Blower[i] = new Blower(i);
}
您需要将方法和 _Blower 设为静态属性静态
public static Blower[] _Blower = new Blower[4];
public static void InstantiateBlowerObj()
{
for (int i = 1; i < 4; i++)
_Blower[i] = new Blower(i);
}
否则创建一个 Global 实例并调用它的实例方法(但我认为这不是您想要的)。
Globals.Global g=new Globals.Global();
g.InstantiateBlowerObj();