In using Dictionary type..

  • Thread starter Thread starter dngchn
  • Start date Start date
D

dngchn

Hello,
I'm newbie.

generally, to use dictionary type in .net.
Dictionary<string, int> anyDic;

in above, If I want to define the key and value's meaning, How can I do that?
in my short thinking,
Dictionary<string name, int studentIdNumber> anyDic;

but the above arise compile error?
Is any idea about this? not just comment.

Thanks.
 
generally, to use dictionary type in .net.
Dictionary<string, int> anyDic;

in above, If I want to define the key and value's meaning,

Meaning to whom?
How can I do that?
in my short thinking,
Dictionary<string name, int studentIdNumber> anyDic;

but the above arise compile error?

Yes, it would.
Is any idea about this? not just comment.

There's nothing in the language that gives you that degree of
descriptiveness. If there were, it would be highly inconsistent with the
use of types everywhere else in the language (or other similar languages
for that matter).

To some extent, you can address the issue through variable naming, which
is the standard way to present usage information in code (i.e. _not_
writing that information as part of the type itself). I personally like
the Hungarian naming convention (the true original one, not the
basterdized "Systems Hungarian" most people are familiar with though
Microsoft's Win32 API), and it addresses this question by defining a "map"
naming format: "mp<from><to>" where "<from>" and "<to>" are the type tags
used for the map.

Of course, for that to apply, you need to be using Hungarian elsewhere and
have appropriate type tags for the types used in the dictionary. But,
supposing you did, and supposing you were using the tag "name" for the
names and "sid" for the student ID number, then the variable name would be
"mpnamesid", or possibly "mpnamesid<Qualifier>" where "<Qualifier>" is
some capitalized word that further describes the mapping (useful mainly if
you have multiple such dictionaries). (*)

Even in absence of Hungarian though, you can a lot better than "anyDic"
for a variable name. So do so. You should be naming your variables in
ways that usefully convey the purpose and usage of the variable, and if
you do that, you wouldn't need to describe the key or value in the type of
the variable declaration.

Pete


(*) I actually struggle with this aspect of the Hungarian convention,
because of the awkwardness of the construction. In the olden days, this
kind of data mapping didn't come up very often, and when it did it was
usually an indexed array rather than a hash-based structure. But
hash-based structures are much more feasible now that the hardware is so
fast and memory so plentiful, and of course you have built-in dictionary
structures in pretty much every mainstream programming environment now.

When one was using this aspect of naming infrequently, the fact that it's
awkward wasn't such a big deal. But IMHO it does get a bit out of hand
when you have a lot of dictionaries running around in the code. On the
one hand, at least if you're using VS's Intellisense, it's not really as
much extra typing as it might seem. But the variables names are still
lengthy and I've never really been happy with the way that the tags run
into each other without any easy-to-read delimiter.

There's a lot about Hungarian I like and I find it extremely valuable as a
naming convention. But it does have some warts and IMHO this is one of
them (the "mp" tag). I've yet to decide on an alternative I like better,
but in the meantime I admit that I often wind up describing the dictionary
in only a pseudo-Hungarian way, using the tag "dict" and then providing
some qualifiers that more-completely describe the usage of the object.

Anyway, that's a long way of saying that if you don't find this use-case
sufficiently compelling enough a reason to switch your convention over to
Hungarian, I wouldn't be surprised. But if you're already using
Hungarian, it's something to consider. At the very least, whether you're
using Hungarian or not, take the underlying motivation to heart and just
use some variable naming convention to incorporate the information you're
trying to present.
 
Thank for your long and kind reply.

I don't know whether I understand what you said rightly.
So, Would you show me the example you said that using tag 'dict' and 'map'.

and, I just want to be changed and supported in C# just like
Dictionary<string name, int studentNumber> anyDic;
Is it simple and efficient as like C++ function declare?
"void anyFunction(string name, int studentNumber);"

Even if dictionary type using is changed as like I said,
I think any other vagueness or inconvenience are not arised.
 
Thank for your long and kind reply.

I don't know whether I understand what you said rightly.
So, Would you show me the example you said that using tag 'dict' and
'map'.

If you want to understand Hungarian, here are some good links to start
with:
http://en.wikipedia.org/wiki/Hungarian_notation (duh)
http://www.byteshift.de/msg/hungarian-notation-doug-klunder (detailed
description of the convention)
http://www.parc.com/about/history/publications/bw-ps-gz/csl76-7.ps.gz
(Charles Simonyi's original paper)
and, I just want to be changed and supported in C# just like
Dictionary<string name, int studentNumber> anyDic;

Sorry, not going to happen.
Is it simple and efficient as like C++ function declare?
"void anyFunction(string name, int studentNumber);"

That's _completely_ different, and C# does in fact provide that syntax (as
do a number of other languages with the concept of functions).

If you want to compare the behavior of the C# generic type
Dictionary<TKey, TValue> where "string" and "int" are used for the type
parameters "TKey" and "TValue", respectively, to something in C++, you
should be looking at templates, not function calls. And they have the
same basic behavior as generics do in this respect (even if in other ways
they are different).
Even if dictionary type using is changed as like I said,
I think any other vagueness or inconvenience are not arised.

"Arised"? Are you using some kind of automated translation software?
Your post is very difficult to understand; half the sentences don't even
make sense

Pete
 
dngchn said:
Hello,
I'm newbie.

generally, to use dictionary type in .net.
Dictionary<string, int> anyDic;

in above, If I want to define the key and value's meaning, How can I do that?
in my short thinking,
Dictionary<string name, int studentIdNumber> anyDic;

but the above arise compile error?
Is any idea about this? not just comment.

Thanks.

Possibly what you want to try is the following, create a class called
StudentRoster that inherits Dictionary<string, int>. In the class
definition, include the routine "public new void Add(string name, int id)".
This way when you use an instance of StudentRoster with the Add method,
intellisense shows "string name, int studentIdNumber" for the help.

public class StudentRoster : Dictionary<string, int>
{
public new void Add(string name, int studentIdNumber)
{
base.Add(name, studentIdNumber);
}
}

This approach then allows for StudentRoster specific functions to be added
to the class.

Mike
 
Back
Top