Member '≪Member Name≫' Cannot Be Accessed With an Instance Reference

Member ' member name ' cannot be accessed with an instance reference

In C#, unlike VB.NET and Java, you can't access static members with instance syntax. You should do:

MyClass.MyItem.Property1

to refer to that property or remove the static modifier from Property1 (which is what you probably want to do). For a conceptual idea about what static is, see my other answer.

Member cannot be accessed with an instance reference qualify it with a type name instead and I DO use type name

In C#, instance methods can only be called on an instance, whereas static methods can only be called on a class/struct itself.

Why can't you chain Info onto SimpleLogger.Instance()?

Because SimpleLogger.Instance(...) returns an instance of SimpleLogger, and you are trying to call a static method on the returned value. The returned value is an instance of SimpleLogger, so you can't call a static method on it.

By making Info non-static, you enable it to be called on an instance. Therefore, you can call it on the return value of Instance().

One reason for your confusion might be that you don't see the instance of SimpleLogger in your chain of methods, so to better illustrate the idea of chaining methods, this:

SimpleLogger.Instance("path").Info("info");

is equivalent to:

SimpleLogger logger = impleLogger.Instance("path");
logger.Info("info");

See the instance of SimpleLogger now?

members cannot be accessed with an instance reference

I've found it by made IocContainer.ShowWeatherDetailViewModel and IocContainer.ShowWeatherViewModel an instance property.

Thanks to @JonSkeet and @ChrisShain.

cannot be accessed with an instance reference; qualify it with a type name instead

Your testThread is a static method, so it's available via type name. So, instead of using isntance threadTest, use ThreadTest type.

// public static void testThread()
testingThread = new Thread(new ThreadStart(ThreadTest.testThread));

Or change method declaration (remove static):

// public void testThread()
testingThread = new Thread(new ThreadStart(threadTest.testThread));

Also you should pass method to delegate ThreadTest.testThread (parentheses removed) instead of passing result of method invokation ThreadTest.testThread().

Member ' method ' cannot be accessed with an instance reference error in Singleton Unity

That static method is in the class, not in an instance. So change your code in InventoryUI to this:

public class PlayerInventoryUI : MonoBehaviour
{
//Inventory Instance
static PlayerInventory inventory;

void Start()
{
inventory = PlayerInventory.getInstance();
//works because static methods are called from the class
}
}

I use Singletons with Unity a lot, have shared my pattern in a Quora answer: https://qr.ae/TSqaWt

Oh and if your Inventory is not in some GameObject Script component, it does not need to inherit from MonoBehaviour. Is better to use just a plain C# class if you don't need to use MonoBehaviour things like having Unity call Update on it.

If you wan't to have it as a script component, which can be nice to have it show in the inspector etc, then you should not 'new' it yourself but let Unity create it and assign the instance to the static reference when that happens. Is good to do that in Awake() so the singleton is already there when other scripts do Start()

Instance member cannot be used on type?

it should be;

public mutating func setPrivateLogger(imp: ((_: String) -> Void)?) {
logger = imp
}

explanation:
struct is a value type. Therefore, when you change it, you actually get a new value just like Int, String or another value type.
Because it is a value type, you cannot change it in its own instance.
"By default, the properties of a value type cannot be modified from within its instance methods" (Swift documentation)
To be able to do this, you need "mutating" behavior.

  • for more info:
    https://docs.swift.org/swift-book/LanguageGuide/Methods.html

if you want to set "logger" only in Struct, you can define it as "private(set) var logger" or if you want to access it only in Struct, you can define it as "private var logger"

struct Console {
static let formatter = DateFormatter()
private(set) var logger: ((_: String) -> Void)?

public static func log(_ level: Slog_ClientLog.LogLevel,_ tag: String,_ message: String) {
// other code
}

public mutating func setPrivateLogger(imp: ((_: String) -> Void)?) {
logger = imp
}
}


Related Topics



Leave a reply



Submit