Re: My First C# (warning - long post)



On Feb 6, 2:45 am, LX-i <lxi0...@xxxxxxxxxxxx> wrote:

snippetty snip


Ah... I shied away from that because I thought, "I don't need a
key-value pair - just the value." Would it still be acceptably
efficient to do the Add()s off the array defined in the constructor? I
could convert it to hard-coded Add()s, that would be easy enough.

Yes, given that the contents are only loaded once, it might be worth
doing purely from the 'readability point of view.

One thing though.... I would have expected an 'addAll(Object[]) '
method like the Java collections have, so to save you from having to
code it.

From a Design point of view, this population loop is a Responsability
that does not below with the KeyWordDictionary and so would be best
moved into its own class, with a choice of designs being:

1) Using Inheritence -
class ExtendedDictionary : Dictionary
{
public void addAll(....)
{
foreach (entry)
add( entry, entry);
}
}


2 Use Delegation

class DictionaryFactory
{

public Dicitionary createFrom(Object[] elements)
{
Dicitionary dictionary = new Dictionary();

for (int i = 0; i < keywords.Length; i++)
{
dictionaryAdd(keywords[i], keywords[i]);
}

}
}
}


Also, regarding the above - would there be anything wrong with making
isKeyword() a static method as well? Then, you wouldn't actually have
to create an object - you could just say something like

if (FastCobolKeywordDictionary.isKeyword(words[i])) {
// do something really cool

}


Wrong... such a subjective term....

So, as I see it (YMMV) at the very highest level of our design this is
a Procedural approach and we are aiming for an OO one.

With the procedural approach, I can't use polymorphism to determine
which keyworddictionary to use, whereas I can with the OO approach.

Procedural example...

public void makeAnimalSound(Animal animal)
{
if(animal.getType() == Dog)
{
DogNoiseGenerator.bark();
}
else if (animal.,getType() == Cat)
{
CatNoiseGenerator.meeow();
}
}

OO Example....(where the noisegenerators are used in a polymorphic
way)

public void makeAnimalSound(Animal animal)
{
AnimalNoiseGenerator noiseGenerator = dictionary.get
(animal.getType() );
noiseGenerator.makeNoise();
}



I did that today with the NormalizeSpace method - I created a class
called CSCSFuncs (as plain Funcs was already taken), and made
NormalizeSpace() a class method.


Personally I always favour Instance Methods over (static) Class
Methods because of this 'subsitutability' benefit. There is nothing to
be gained from worrying about the memory or cpu usage from creating
one of this objects vs calling a static method.


Andrew

.



Relevant Pages

  • Re: Generics and static members
    ... public void MethodB() ... If this is the case, then it's not possible for the compiler to fulfill your intent, because it needs to know when it compiles the _generic_ class how to get at the method, and at that point the type parameter hasn't actually been resolved yet. ... the compiler could generate some complex code to use reflection to determine at run-time the correct static method to call. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: OO Design question about batch job
    ... > refactor the code now - using the best current estimate of the ... > public void dumpListAscii() ... > public class AsciiLister extends Lister ... > {protected java.lang.String entry ...
    (comp.lang.java.programmer)
  • Re: Hashtable
    ... > delete entry ... > public void textBox() ... > display = Display.getDisplay; ...
    (comp.lang.java.programmer)
  • Re: Hashtable
    ... delete entry ... private TextField userNumber = new ... public void textBox() ...
    (comp.lang.java.programmer)
  • Re: New C# programmer questions
    ... static void Main ... an object to leave a static method for a non-static method? ... public void MyOtherMethod() ... // do the last of the things, time to exit program...how to exit? ...
    (microsoft.public.dotnet.languages.csharp)