当前上下文中不存在名称“myClassInstance”

时间:2013-01-03 16:03:00

标签: c# dll compiler-errors include

环境:c#.net VS 2010

解决方案有以下两个项目:

  • 我添加了几种测试方法的dll。

  • 测试项目

测试项目中唯一的东西是包含以下代码的表单:(名称已更改以便于阅读)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using DLL_PROJECT; //Yes I remembered to include the dll project

namespace DLL_PROJECT_Test
{
public partial class frmTest : Form
{
    private Class_1 myClass_1; //this comes from the dll - no errors here
    private Class_2 myClass_2 = new Class_2(); // no errors here either

    public frmTest()
    {
        InitializeComponent();
        //TransparencyKey = BackColor;
        this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
        this.BackColor = System.Drawing.Color.FromArgb(0, System.Drawing.Color.Black);
        myDebouncer = new Debouncer(this);
        this.SetDragging(true); //THIS EXTENSION COMES FROM THE DLL AND WORKS FINE
        this.RoundCorners(40, 80); //AS DOES THIS ONE
        myClass_2 = new Class_2();
        myClass_2.HoldStartEvent += new Class_2EventHandler(myClass_2_HoldStartEvent);
        myClass_2.DragStartEvent += new Class_2EventHandler(myClass_2_DragStartEvent);
    }

    private void myClass_2_DragStartEvent(Class_2 sender)
    {
        myClass_2("DragStart") += 1; //THE ONLY ERROR IS HERE AS FOLLOWS
//ERROR: "The name 'myClass_2' does not exist in the current context"
//     - Yes, the DLL is included
//     - Yes, the project is .Net 4 (not client profile)
//     - Yes xxx WRONG xxx, this exact syntax has been tested before on an instance of
//       this class, it's just a default parameter.
// xxx should be [] instead of () for the indexer in c#.  #VB_Fails
    }

    void myClass_2_HoldStartEvent(Class_2 sender)
    {
        this.Close();
    }
}
}

1 个答案:

答案 0 :(得分:2)

此代码:

myClass_2("DragStart") += 1;

...正在使用myClass_2,就好像它是方法的名称或委托实例一样。

你真的想要使用索引器吗?那将是:

myClass_2["DragStart"] += 1;

"DragStart"在这里意味着什么?它实际上是一个属性名称吗?也许你想要:

myClass_2.DragStart += 1;

我非常怀疑“这个确切的语法之前已经在这个类的实例上进行了测试”。

不可否认,在这种情况下,错误消息没有多大意义。我认为实际上更可能是你的真实代码中有一个拼写错误 - 这是因为你改变了名字所以没有在这里传播的拼写错误。如果您可以在简短的完整的程序中重现这一点,那么它将使生活变得更加简单。