An object reference is required for the non-static field, method, or property using global variable

StackOverflow https://stackoverflow.com/questions/19431081

  •  01-07-2022
  •  | 
  •  

Question

Hello Iam working in a global int in which a variable will be used later. I've made the global variable like this:

    class Foo
    {
       public static int stream = Bass.BASS_StreamCreateFile(path1.Text, 0, 0, BASSFlag.BASS_DEFAULT);
    }

which will be later called like this:

Foo.stream

and it can also contain more then 1 stream for example stream20,30,etc...

The problem here is that it returns me this error:

"An object reference is required for the non-static field, method, or property" where I call the text in path1.Text

How do I fix this?

Was it helpful?

Solution

Your better off doing something like this:

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.

Passing the path as a parameter.

EDIT:

Based on your comment, does the following code work for you?

public class Foo
{
    public static int GetStream(string path)
    {
        return 1;
    }
}

int foo = Foo.GetStream(path1.Text);

You should get 1 returned. If that does work then you have a problem with Bass.BASS_StreamCreateFile(). Otherwise, can you please post all of your code so that we can see how you are using the class Foo in your code?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top