C# - Access another class methods

落爺英雄遲暮 提交于 2019-12-06 15:32:40

In my idea, it should read data (posX and posY in this case) by itself, since it is inside the same Map class. Is there a clean way to do this?

No. You will have to pass data back and forth between the DataHandler and FileHandler in your Map class, they do not automagically get informed of each other's presence.

It would really help if you could explain a bit what everything is supposed to do, as your class names seem to be a bit too generic.

If you strongly against changing structure (but it is recommended way) I suggest you refactor DataHandler and FileHandler - implement it as Singleton, so in any line of your code you can access it as via DataHandler.Instance.GetX() and FileHandler.Instance.SomeMethod().

Basic implementation of singleton is:

class DataHandler
{
    private DataHandler() {}
    public GetX() { ... }

    private static DataHandler instance = null;

    public static Instance
    {
        get
        {
            if (instance == null)
            {
                return (instance = new DataHandler());
            }
        }
    }
}

UPD: My sample is not thread-safe.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!