Click here to Skip to main content
15,895,667 members
Home / Discussions / C#
   

C#

 
QuestionDummy question, when i close the GUI from the "X" from upper left corner, how to call another function ? Pin
588311-Nov-10 4:11
588311-Nov-10 4:11 
AnswerRe: Dummy question, when i close the GUI from the "X" from upper left corner, how to call another function ? PinPopular
Lutosław11-Nov-10 4:17
Lutosław11-Nov-10 4:17 
GeneralRe: Dummy question, when i close the GUI from the "X" from upper left corner, how to call another function ? [modified] Pin
Jeff Connelly15-Nov-10 11:07
Jeff Connelly15-Nov-10 11:07 
QuestionCopy SQL Table with ADO Pin
musefan11-Nov-10 3:45
musefan11-Nov-10 3:45 
AnswerRe: Copy SQL Table with ADO Pin
musefan11-Nov-10 4:56
musefan11-Nov-10 4:56 
AnswerRe: Copy SQL Table with ADO Pin
Mycroft Holmes11-Nov-10 12:02
professionalMycroft Holmes11-Nov-10 12:02 
AnswerRe: Copy SQL Table with ADO Pin
Henry Minute11-Nov-10 13:13
Henry Minute11-Nov-10 13:13 
QuestionCoding style -- instantiating a nested class Pin
Lutosław11-Nov-10 2:33
Lutosław11-Nov-10 2:33 
Hi!

According to the Nested Type Usage Guidelines[^], creating an instance of a nested class by an outside code is generally bad.

I'd like to implement a factory+template+command pattern, in which creating an instance of a command would be forbidden. The factory has instances of all templates. Templates are responsible to validate user's input and, if it's valid, create a Command object. Command class executes itself and has a private constructor. Fortunately, a nested class can access a private constructor of it's parent.

I cannot use a static method, because the factory has to be extensible (it has an AddTemplate(CommandTemplate) method which allows adding new templates).

Here is the code:
class LookCommand : Command
{
    public class Template : CommandTemplate
    {
        private const string NAME = "look";

        public override Command TryCreate(RpgOntologyController ontc, CommandParser input)
        {
            return MatchesCommand(ontc, NAME, input) 
                ? new LookCommand(ontc) 
                : null;
        }

        protected string CommandName
        {
            get { return NAME; }
        }
    }
    private LookCommand(RpgOntologyController controller) : base(controller)
    {
    }

    public override CommandEffect Execute()
    {
            
        StringBuilder description = new StringBuilder();
        description.AppendLine(new SceneOntologyController(Controller).CurrentSceneDescription);
        description.AppendLine();
        return new CommandEffect
                {
                    Output = description.ToString()
                };
    }
    void AppendNPC(StringBuilder build)
    {
        foreach (string npc in new NPCOntologyController(Controller).NearbyNPCs)
        {
            build.AppendLine(npc);
        }
    }
}

In the factory:
public Command Create(string input)
{
    // (...)

    Command cmd = null;
    var commandParser = new CommandParser(input);
    if (_templates.Any(template => (cmd = template.TryCreate(_controller, commandParser)) != null))
    {
        return cmd;
    }

    throw new ArgumentException("Invalid command.");
}


Can it be an exception of a rule from the Nested Type Usage Guidelines[^]?

Thanks in advance,
Greetings - Jacek

AnswerRe: Coding style -- instantiating a nested class Pin
Dave Kreskowiak11-Nov-10 4:31
mveDave Kreskowiak11-Nov-10 4:31 
GeneralRe: Coding style -- instantiating a nested class Pin
Lutosław11-Nov-10 9:53
Lutosław11-Nov-10 9:53 
QuestionFTP download and bacgroundworker Pin
peropata11-Nov-10 2:18
peropata11-Nov-10 2:18 
AnswerRe: FTP download and bacgroundworker Pin
Herman<T>.Instance11-Nov-10 2:24
Herman<T>.Instance11-Nov-10 2:24 
AnswerRe: FTP download and bacgroundworker Pin
musefan11-Nov-10 2:33
musefan11-Nov-10 2:33 
AnswerRe: FTP download and bacgroundworker Pin
Luc Pattyn11-Nov-10 2:50
sitebuilderLuc Pattyn11-Nov-10 2:50 
GeneralRe: FTP download and bacgroundworker Pin
peropata11-Nov-10 3:29
peropata11-Nov-10 3:29 
GeneralRe: FTP download and bacgroundworker Pin
Luc Pattyn11-Nov-10 3:43
sitebuilderLuc Pattyn11-Nov-10 3:43 
GeneralRe: FTP download and bacgroundworker Pin
peropata11-Nov-10 4:58
peropata11-Nov-10 4:58 
Questioncopy Datagridview value to another datagridview Pin
annie_bel11-Nov-10 1:55
annie_bel11-Nov-10 1:55 
GeneralRe: copy Datagridview value to another datagridview Pin
musefan11-Nov-10 2:12
musefan11-Nov-10 2:12 
GeneralRe: copy Datagridview value to another datagridview Pin
annie_bel11-Nov-10 2:19
annie_bel11-Nov-10 2:19 
GeneralRe: copy Datagridview value to another datagridview [modified] Pin
musefan11-Nov-10 2:25
musefan11-Nov-10 2:25 
GeneralRe: copy Datagridview value to another datagridview Pin
annie_bel11-Nov-10 2:35
annie_bel11-Nov-10 2:35 
AnswerRe: copy Datagridview value to another datagridview Pin
musefan11-Nov-10 2:57
musefan11-Nov-10 2:57 
AnswerRe: copy Datagridview value to another datagridview Pin
Mycroft Holmes11-Nov-10 12:10
professionalMycroft Holmes11-Nov-10 12:10 
Questionhow to detect the type of device plugged into Pin
emmmatty110-Nov-10 23:47
emmmatty110-Nov-10 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.