Discussion:
new class from dictionary?
(too old to reply)
SpreadTooThin
2016-07-17 04:01:38 UTC
Permalink
I have a class that has a dictionary in it.

public class myClass
{
Dictionary<String, AnObject> MyDict;
public myClass()
{
MyDict = new Dictionary<string, AnObject> ();
}
public void Add(AnObject o)
{
seriesQueue.Add(o.UID, o);
}
}

Should this 'type' of dictionary be a class?
What is the syntax to do so?
Arne Vajhøj
2016-07-17 12:48:02 UTC
Permalink
Post by SpreadTooThin
I have a class that has a dictionary in it.
public class myClass
{
Dictionary<String, AnObject> MyDict;
public myClass()
{
MyDict = new Dictionary<string, AnObject> ();
}
public void Add(AnObject o)
{
seriesQueue.Add(o.UID, o);
}
}
Should this 'type' of dictionary be a class?
Probably not.

The property name UID hint at some sort of numbering scheme
and values that would not be suitable as property names.
Post by SpreadTooThin
What is the syntax to do so?
You would need to make a property for each value of UID.

Arne
SpreadTooThin
2016-07-17 16:24:17 UTC
Permalink
public class MyClass : System.Collections.CollectionBase

Something to that effect?
Arne Vajhøj
2016-07-17 18:59:13 UTC
Permalink
Post by SpreadTooThin
public class MyClass : System.Collections.CollectionBase
Something to that effect?
No.

Instead of having a Dictionary with keys "A", "B" and "C"
then have a class with properties A, B and C.

Arne
Anton Shepelev
2016-07-18 11:31:32 UTC
Permalink
Post by SpreadTooThin
I have a class that has a dictionary in it.
public class myClass
{ Dictionary<String, AnObject> MyDict;
public myClass()
{
MyDict = new Dictionary<string, AnObject> ();
}
public void Add(AnObject o)
{
seriesQueue.Add(o.UID, o);
}
}
Should this 'type' of dictionary be a class?
What is the syntax to do so?
Due to the paucity of information I cannot tell
whether Arne's suggestion will work you or not, but
you indeed can declare your dictionary as a class:

class MyDict: Dictionary< string, AnObject >

It may help if you use this class a lot and pass it
around in a parameter.

The incusion of the UID field in both the dictionary
and the class betrays a redundancy which may be un-
desirable. I also suggest that you follow the
CamelCase naming for acronyms too and rename UID to
Uid.
--
() ascii ribbon campaign - against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]
Arne Vajhøj
2016-07-19 01:42:57 UTC
Permalink
Post by Anton Shepelev
Post by SpreadTooThin
I have a class that has a dictionary in it.
public class myClass
{ Dictionary<String, AnObject> MyDict;
public myClass()
{
MyDict = new Dictionary<string, AnObject> ();
}
public void Add(AnObject o)
{
seriesQueue.Add(o.UID, o);
}
}
Should this 'type' of dictionary be a class?
What is the syntax to do so?
Due to the paucity of information I cannot tell
whether Arne's suggestion will work you or not,
I am just mentioning it as a possibility. I am
not suggesting it. The use of UID as key sort of
indicate that it is a bad idea.
Post by Anton Shepelev
but
class MyDict: Dictionary< string, AnObject >
True.

But in many cases:

public class MyDict: IDictionary< string, AnObject >
{
private Dictionary< string, AnObject > real;

would provide more flexibility.

Arne

Continue reading on narkive:
Loading...