1

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

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:

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

(this is in the Form class)

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

It's just these errors but for each different line

4
  • 3
    Do you feel like sharing the actual error with someone? Commented Jun 5, 2011 at 12:19
  • I think you will need to post the complete code in order to get proper help. Commented Jun 5, 2011 at 12:19
  • What are the types of colourObject, rotateFlipObject, and resizeObject? Commented Jun 5, 2011 at 12:20
  • You can only do macroCommandList.Add inside the Macro class, since you're accessing a private member. Commented Jun 5, 2011 at 12:23

3 Answers 3

1

This should work:

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

    public void AddMacroCommand(MacroCommand mc)
    {
        macroCommandList.Add(mc);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

According to the error, the objects you're trying to add don't exist. Where are those objects declared? What is their 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.

Comments

0

If you have a List<BaseClass> you can only add to it elements that can be converted to the BaseClass (i.e. BaseClass'es and classes derived from 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. This means either making it public (not a very good idea) or adding a public method to its containig class (i.e. Macro) that does the adding. The method is public so it is accessible to callers outside the class and it also has access to the private variable macroCommandList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.