C# 使用全局变量的非静态字段、方法或属性需要对象引用
您好,我在一个全局整数中工作,稍后将在其中使用一个变量。我将全局变量设置为:C# 使用全局变量的非静态字段、方法或属性需要对象引用,c#,bass,C#,Bass,您好,我在一个全局整数中工作,稍后将在其中使用一个变量。我将全局变量设置为: class Foo { public static int stream = Bass.BASS_StreamCreateFile(path1.Text, 0, 0, BASSFlag.BASS_DEFAULT); } Foo.stream public class Foo { public static int GetStream(string path) {
class Foo
{
public static int stream = Bass.BASS_StreamCreateFile(path1.Text, 0, 0, BASSFlag.BASS_DEFAULT);
}
Foo.stream
public class Foo
{
public static int GetStream(string path)
{
return Bass.BASS_StreamCreateFile(path, 0, 0, BASSFlag.BASS_DEFAULT);
}
}
int foo = Foo.GetStream(path1.Text); // Or whatever you want to call the method.
以后会这样称呼它:
class Foo
{
public static int stream = Bass.BASS_StreamCreateFile(path1.Text, 0, 0, BASSFlag.BASS_DEFAULT);
}
Foo.stream
public class Foo
{
public static int GetStream(string path)
{
return Bass.BASS_StreamCreateFile(path, 0, 0, BASSFlag.BASS_DEFAULT);
}
}
int foo = Foo.GetStream(path1.Text); // Or whatever you want to call the method.
它还可以包含多个流,例如流20、30等
这里的问题是,它会返回以下错误:
“非静态字段、方法或属性需要对象引用”,其中我调用path1.text中的文本
我该如何解决这个问题?您最好这样做:
class Foo
{
public static int stream = Bass.BASS_StreamCreateFile(path1.Text, 0, 0, BASSFlag.BASS_DEFAULT);
}
Foo.stream
public class Foo
{
public static int GetStream(string path)
{
return Bass.BASS_StreamCreateFile(path, 0, 0, BASSFlag.BASS_DEFAULT);
}
}
int foo = Foo.GetStream(path1.Text); // Or whatever you want to call the method.
将路径作为参数传递
编辑:
根据您的评论,以下代码是否适用于您
public class Foo
{
public static int GetStream(string path)
{
return 1;
}
}
int foo = Foo.GetStream(path1.Text);
您应该返回1
。如果这样做有效,那么您的Bass.Bass\u StreamCreateFile()
就会出现问题。否则,您能否发布所有代码,以便我们了解您如何在代码中使用类Foo
?路径1.Text
来自何处?在哪里申报?您必须将path1声明为静态
,以解决您的问题,例如公共静态Foo path1`,但是这种设计已经很难闻。大概path1
是一个实例变量。您希望在这里与Foo
的哪个实例相关?path1.Text
是在我设置变量(path1.Text,0,0,bassfag.BASS\u默认值)
的地方被去格的,在那里我使用正在使用的文件的路径。是的,但是如果“路径”是在文本中呢?例如,我会使用path1.Text作为路径。它将返回相同的错误,并将我留在最初开始的位置。仍然会收到相同的错误非静态字段、方法或属性需要对象引用,加上无效字符。我做错了什么?我看到你删除了无效字符,但错误仍然存在。我用的正是你写的<代码>非静态字段、方法或属性需要对象引用
是否缺少其他内容?