Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#类位于文件夹中,需要调用另一个文件夹中的类_C#_Class_Reference_Xna_Directory - Fatal编程技术网

C#类位于文件夹中,需要调用另一个文件夹中的类

C#类位于文件夹中,需要调用另一个文件夹中的类,c#,class,reference,xna,directory,C#,Class,Reference,Xna,Directory,我有一个名为StartUp的文件夹,它保存了开始的类,并在程序中首先加载。我想访问另一个名为total的文件夹,它包含将在整个游戏中使用的类。我想从总体上获取一个名为鼠标的类,并将其转移到启动中的任何其他类上 编辑:2013年7月12日10:00 这是文件和文件夹,例如,Mouses.cs有一个public int thing=1我希望它移动到TitleScreen.cs,我该如何允许TitleScreen.cs查看/编辑该int 我可以访问Game1.cs中的所有类,方法是调用那里的文件夹,然

我有一个名为
StartUp
的文件夹,它保存了开始的类,并在程序中首先加载。我想访问另一个名为
total
的文件夹,它包含将在整个游戏中使用的类。我想从
总体上
获取一个名为
鼠标
的类,并将其转移到
启动
中的任何其他类上

编辑:2013年7月12日10:00

这是文件和文件夹,例如,
Mouses.cs
有一个
public int thing=1
我希望它移动到
TitleScreen.cs
,我该如何允许
TitleScreen.cs
查看/编辑该int

我可以访问
Game1.cs
中的所有类,方法是调用那里的文件夹,然后调用类(例如
total.Mouses.ect=1;
),反之亦然

总体.Mouses.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Lover__Regret
{
    class Mouses
    {
        public int thing = 1;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Love__Regret.StartUp
{
    class TitleScreen
    {
        public static void Update(GameTime gameTime)
        {
        //Want to call thing here
        }
    }
}
StartUp.TitleScreen.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Lover__Regret
{
    class Mouses
    {
        public int thing = 1;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Love__Regret.StartUp
{
    class TitleScreen
    {
        public static void Update(GameTime gameTime)
        {
        //Want to call thing here
        }
    }
}

嗯,
鼠标中的
thing
字段不是静态字段。这意味着您需要在
TitleScreen
中有一个对
mouse
的实例引用,才能访问它

您说过在
Game1.cs
中已经有了实例的引用。因此,我建议做以下几点:

  • TitleScreen
    中的
    Update
    方法不应是静态的。您需要更新特定的
    标题屏幕
    实例。因此,删除static关键字
  • TitleScreen
    创建一个构造函数,该构造函数接受
    mouse
    的一个实例,并创建一个字段来存储该实例:

    私人鼠标(mouseHandler),

    公共标题屏幕(Mouses mouseHandler) { _mouseHandler=mouseHandler; }

  • 然后,在
    Update
    方法中调用如下内容:

    Console.WriteLine(_mouseHandler.thing);//或者对鼠标中的字段执行任何操作

  • 现在,当您在
    Game1.cs
    中构建
    TitleScreen
    时,通过传递一个
    鼠标
    实例来创建它:

    //创建鼠标实例(例如:mouses mouses=new mouses())
    标题屏幕=新标题屏幕(鼠标)


  • 大多数编程不是通过静态类/方法完成的(这将是过程性编程)。相反,您需要创建类的实例

    你说的“上一节课,把它转过来”是什么意思?一些代码可能有助于说明这个想法。@CodeCaster我将更新帖子。感谢您的帮助,我希望绕过主类(
    Game1.cs
    ),而不必使用它。由于我知道如何引用类,但希望使用最简单的代码,这意味着我必须对每个类都做同样的操作,比如ie.
    学分
    选项
    标题屏幕
    以及任何其他需要
    鼠标
    类的类。跳过主类可以节省大约20行代码,但我还是尝试了最少的代码。我已经解释了为什么我无法访问另一个文件夹
    总体上
    ,程序中有一个bug。我不知道它是什么,但创建一个新项目,创建两个文件夹,并在每个文件夹中添加一个类,我只需执行以下操作即可访问另一个类:
    private-all.Mouses