English 中文(简体)
积极的参考问题
原标题:String reference issue
  • 时间:2012-01-14 15:59:27
  •  标签:
  • c#
  • string

在把我游戏放在第一号的游戏变成一个问题的同时,将形成一种方法,以加以扼杀,并将它与另一个扼杀相比较。

if (Player.GetCurrentRoom() == Level.Rooms[2,0]
    && string.Equals(TextMethods.ExtractArgument(input), "fortune"))
{
  Level.Rooms[2, 0].AddExit("north");
}

它说,投入没有得到承认,而我把投入定义为另一类。

public static string ExtractInput(string line)
{
  string input = TextMethods.ExtractArgument(line);
  return input;
}

编辑:此处为摘取法:

public static string ExtractArgument(string line)
{
  int index = line.IndexOf(   );
  if (index == -1)
    return "";
  else
    return line.Substring(index + 1, line.Length - index - 1);
}

任何关于这种情况发生的原因的想法?

问题回答

在你的第二部法典中,你正在显示一种地方变量,即“输入<>>/代码”。 这种变数在宣布的方法之外看不到。

如果你想像你一样工作,你必须宣布第二类财产为:

public class Class2
{
    public static string Input { get; set; }

    public static string ExtractInput(string line)
    {
        return TextMethods.ExtractArgument(line);
    }
}

然后在第一部法典中使用:

if (Player.GetCurrentRoom() == Level.Rooms[2,0]
        && string.Equals(TextMethods.ExtractArgument(Class2.Input), "fortune"))
{
    Level.Rooms[2, 0].AddExit("north");
}

如果您有一种地方变量,即input,则你可以直接使用另一种方法。 在包含当地变量的方法完成后,当地变量不再存在!

也许你们会因诸如贾瓦伦文之类的语言而感到困惑,在这些语言中,你可以以一种方法宣布一个全球性的变数,然后在任何地方加以使用。 这并非在这里发生的事情。

如果您希望通过<代码>ExtractInput()开展工作,你可以将其储存在现行方法中的当地变量中:

string line = …;

string input = TextMethods.ExtractInput(line);

if (Player.GetCurrentRoom() == Level.Rooms[2,0]
    && TextMethods.ExtractArgument(input) == "fortune")
{
  Level.Rooms[2, 0].AddExit("north");
}

如果你希望通过<代码>ExtractInput()进行更长时间的工作(而不仅仅是一种方法),你可以将其存放在外地。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签