没有超载的方法' X'需要1个参数

时间:2015-06-27 08:39:51

标签: c#

我现在面对C#中的一个问题,这让我很烦恼.. 无论如何这里是代码的一部分;

public class blocked
{
   public static void blockedOpcodes()
   {
    Dictionary<ushort, String> Opcodes
    = new Dictionary<ushort, String>();
            Opcodes.Add(0x2005, "Blocked: TD46 Tool");
    }
}

继续,我试图从字典中获取操作码列表;

 if (Project_name.blocked.blockedOpcodes(current.Opcode))
{
}

^但是那个显示错误&#39;没有重载方法&#39; blockedOpcodes&#39;需要1个参数。&#39;

EDIT1:我试图改变

   public static void blockedOpcodes()
   {

   public static void blockedOpcodes(ushort opcode)
   {

但另一个错误显示为&#39;无法隐式转换类型&#39; void&#39;到了&#39; bool&#39;

PS:我完全是初学者!我一周前才开始。

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定我明白你想要达到的目的是什么。 如果您正在尝试查找字典中是否有current.Ocpode,那么您可能希望更改代码:

public class blocked
{
   public static Dictionary<ushort, String> blockedOpcodes()
   {
    Dictionary<ushort, String> Opcodes
    = new Dictionary<ushort, String>();
            Opcodes.Add(0x2005, "Blocked: TD46 Tool");
    }
}

现在你有一个方法可以返回带有操作码的字典。

if (Project_name.blocked.blockedOpcodes().ContainsKey(current.Opcode))
{
    //current.Opcode is in the Dictionary
    //do what you wanted 
    string text = Project_name.blocked.blockedOpcodes()[current.Opcode];
    Console.WriteLine(text);
}

一些有用的链接:

<强> UPD

public static bool isOpcodeAllowed(ushort opcode) 
{ 
    //if opcode is 0x2005 and in Opcodes there is a key 0x2005 
    //then the next line will return true
    if (Opcodes.ContainsKey(opcode)) 
    { 
           string text = Opcodes[opcode];
           //there is a string "Blocked: TD46 Tool" in the text variable 
           Console.WriteLine(text); //what do you see here?
           Log1.LogMsg(text); 
           return false; 
     }
     return true; 
 }

<强> UPD2

这不是一个例子!这解释了为什么您的代码无法正常工作! 你觉得这里发生了什么:     Opcodes.ContainsValue(Opcodes.ContainsKey(操作码)));

首先,您将获得此表达式的值:

Opcodes.ContainsKey(opcode)

is true or false.

然后你做Opcodes.ContainsValue(true); 结果是错误的,因为你没有价值&#39; true&#39;在你的字典中,你有字符串

<强> upd4

如何使用字典:

Dictionary<ushort, string> dict = new Dictionary<ushort, string>();

现在你有一个词典,它是空的。

dict.Add(0x2005, "Blocked: TD46 Tool");

现在你有一条记录。记录有密钥和值。

价值是&#34;被阻止:TD46工具&#34;那是string

键是0x2005,而且是ushort

dict.ContaintsKey(0x2005) - 返回true。因为你的词典中有一个键0x2005。

dict.ContaintsKey(0x105) - 返回false。因为你的字典中没有密钥0x105。

dict[0x2005] - 返回&#34;已阻止:TD46工具&#34;因为这是一个值或带Key的记录。

如果你这样做:

string text = dict[0x2005];

你得到&#34;被阻止:TD46工具&#34;存储在text变量中。

现在您可以将text发送到日志,控制台。

如果按&#34;播放&#34;这里https://dotnetfiddle.net/XEhhst 你会看到代码有效。

相关问题