简体   繁体   English

C# 添加到列表问题

[英]C# Adding to list issue

Hey guys, I have an application which consists of a baseclass (MacroCommand), three subclasses of MacroCommand (Resize, Rotate, Colour), and another class which does not inherit anything (Image) and then another class (Macro) which holds a list of MacroCommands嘿伙计们,我有一个应用程序,它由一个基类(MacroCommand)、三个 MacroCommand 子类(调整大小、旋转、颜色)和另一个不继承任何东西的 class(图像)和另一个包含列表的 class(宏)组成宏命令

class Macro
{
    List<MacroCommand> macroCommandList = new List<MacroCommand>();
}

Basically, when I create my MacroCommand objects in the Form class, I need to then add them to the MacroCommand list, however, I keep getting errors when I try to do this:基本上,当我在表格 class 中创建我的 MacroCommand 对象时,我需要将它们添加到 MacroCommand 列表中,但是,当我尝试这样做时,我不断收到错误:

macroCommandList.Add(colourObject);
macroCommandList.Add(rotateFlipObject);
macroCommandList.Add(resizeObject);

(this is in the Form class) (这是在 Form 类中)

Any help would be appreciated, thanks guys.任何帮助将不胜感激,谢谢大家。

EDIT: Errors:编辑:错误:

Error 2 The name 'colourObject' does not exist in the current context C:\Users\Ari\Desktop\Assignment 3\Assignment 3\Assignment 3\Form1.cs 134 42 Assignment 3 Error 3 The name 'macroCommandList' does not exist in the current context C:\Users\Ari\Desktop\Assignment 3\Assignment 3\Assignment 3\Form1.cs 135 21 Assignment 3错误 2 当前上下文中不存在名称 'colourObject' C:\Users\Ari\Desktop\Assignment 3\Assignment 3\Assignment 3\Form1.cs 134 42 分配 3 错误 3 名称 'macroCommandList' 不存在于当前上下文 C:\Users\Ari\Desktop\Assignment 3\Assignment 3\Assignment 3\Form1.cs 135 21 Assignment 3

It's just these errors but for each different line只是这些错误,但对于每个不同的行

This should work:这应该有效:

class Macro {
    private List<MacroCommand> macroCommandList = new List<MacroCommand>();

    public void AddMacroCommand(MacroCommand mc)
    {
        macroCommandList.Add(mc);
    }
}

According to the error, the objects you're trying to add don't exist.根据错误,您尝试添加的对象不存在。 Where are those objects declared?这些对象在哪里声明? What is their scope?他们的 scope 是什么? The list is private by default, so you need to supply those objects as arguments to the method which is adding them to the list.该列表默认为私有,因此您需要将这些对象作为 arguments 提供给将它们添加到列表的方法。

If you have a List<BaseClass> you can only add to it elements that can be converted to the BaseClass (ie BaseClass 'es and classes derived from BaseClass ).如果你有一个List<BaseClass>你只能添加可以转换为BaseClass的元素(即BaseClass和从BaseClass派生的类)。

If you want to add elements to a list (say, macroCommandList ) you need to make sure the list is accesible to the calling code.如果要将元素添加到列表(例如macroCommandList ),则需要确保调用代码可以访问该列表。 This means either making it public (not a very good idea) or adding a public method to its containig class (ie Macro ) that does the adding.这意味着要么将其公开(不是一个好主意),要么向其包含的 class (即Macro )添加一个公共方法来进行添加。 The method is public so it is accessible to callers outside the class and it also has access to the private variable macroCommandList .该方法是公共的,因此 class 之外的调用者可以访问它,并且它还可以访问私有变量macroCommandList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM