I'm using a dictionary called Dictionary<MyProperty, string> to use in all my source codes.
And I want to change the name into something shorter, which has the same effect,
because the Dictionary<MyProperty, string> is a bit too long.
I mean:
change
Dictionary<MyProperty, string> mydic = new Dictionary<MyProperty, string>();
into
MyDic mydic = new MyDic();
And I want to use it the same as Dictionary, like
mydic[myProperty] = "somestring";
How do I achieve this?
I tried
public class MyDic : Dictionary<MyProperty, string>
{
}
but it doesn't work, or should I add something?
you can use type aliases in C#. also consider using var instead of full name if it is possible
using MyDic = System.Collections.Generic.Dictionary<MyProperty, string>;
later instantiate a variable like
MyDic mydic = new MyDic(); // or var mydic = new MyDic()
you can read more here
you can also use inheritance to just rename a type like what you did before
public class MyDic : Dictionary<MyProperty, string> { }
Using inheritance to do aliasing has the problem of requiring you redefine the relevant constructors. Since it will quickly become unreasonable to do that everywhere, it's probably best to avoid it for consistency's sake.
Finally. Please don't do it. When people see this code
var MyDic = new Dictionary<MyProperty, string>;
they understand immediately that it is a dictionary of strings with MyProperty as key.
but when they see:
var MyDic = new MyDic();
they must go and find out what MyDic is! One programmer's saving on typing could very well be the next programmer's maintenance nightmare, writing readable code is a good idea.
It works for me
I just tested
What do you mean it does not work?
TestCast tc3 = new TestCast(5757);
MyDic myDic = new MyDic();
myDic.Add(tc3, "string");
myDic[tc3] = "string2";
public class MyDic : Dictionary<MyProperty, string>
{
}
Personally unless you are going override then just use
Dictionary<MyProperty, string> mydic = new Dictionary<MyProperty, string>();
So a few keystrokes. It is just one line and pretty self explanatory.
Related
I need to know how to access and initialize a series of Dictionaries containing other dictionaries.
For example, if I have
class Conv{
Dictionary<int, Dictionary<int, Dictionary<int, List<double>>>> valori;
}
And I want to initiate the parameter "valori" with random numbers, how can I do it?
I would do it like
valori[n1].Values[n2].Values[n3]
But after the first "Value", MVS gives me an error. Maybe I have to allocate the memory first? I learned a little bit of c++, but I'm still new to c#
Also let me know if I forgot something important in my question
You need to create the sub-dictionaries for each key before using them
var list = new List<double> {d};
var d1 = new Dictionary<int, List<double>> {{n3, list }};
var d2 = new Dictionary<int, Dictionary<int, List<double>>> {{n2, d1}};
valori[n1] = d2;
You can also write this short in one line:
valori[n1] = new Dictionary<int, Dictionary<int, List<double>>> {{n2, new Dictionary<int, List<double>> {{n3, new List<double> {d}}}}};
When all dictionaries are actually created you can access them normally:
var savedList = valori[n1][n2][n3];
Since this syntax is very clunky and it is easy to make a mistake (missing if a sub-dictionary exists, overriding data, etc), I'd strongly suggest changing the datastructure or at least hiding it in a dedicated class
Maybe i'm mistaken but I can't think of situation that you would need this kind of structure but nevertheless here's my help:
First of all you need to assign the variable or you will get the error :
"Use of unassign local variable".So the code will be like:
Dictionary<int, Dictionary<int, Dictionary<int, List<double>>>> valori=new
Dictionary<int, Dictionary<int, Dictionary<int, List<double>>>>();
Secondly you need to add some data to the dictionary in order to use it later so
you should do :
valori.Add(2, new Dictionary<int, Dictionary<int, List<double>>>());
valori.Add(3, new Dictionary<int, Dictionary<int, List<double>>>());
valori.Add(4, new Dictionary<int, Dictionary<int, List<double>>>());
"notice that keys are different"
And instead of new Dictionary<int, Dictionary<int, List<double>>>() you should
enter a value of that type.
var for_cat_dict = new Dictionary<string, string>();
var category_Dict = new Dictionary<string,Dictionary<string,string>>();
for_cat_dict.Add(bean.getId(), bean.getId());
Now I want to add elements to the category_dict. So I tried..
category_Dict.Add(bean.getId(),[for_cat_dict]);
But it doesnt work... any solutions??
It's not really clear what you're trying to do, but
Category_Dict.Add(bean.getId(), for_cat_dict);
should at least compile. Whether it'll do what you want is another matter - it's not clear whether these are local variables, fields etc. (It also looks like you're not following .NET naming conventions in various ways...)
Dictionary<string, string> for_cat_dict = new Dictionary<string, string>();
Dictionary<string, Dictionary<string, string>> Category_Dict = new Dictionary<string, Dictionary<string, string>>();
Category_Dict.Add("somekey", for_cat_dict);
Suppose I have an array of strings like :
myArray["hello", "my", "name", "is", "marco"]
to access to this variable, I have to put an integer as index. So if I wanto to extract the third element I just do :
myArray[2]
Now, I'd like to use label instead of integer.
So for example somethings like :
myArray["canada"]="hello";
myArray["america"]="my";
myArray["brazil"]="name";
myArray["gosaldo"]="is";
myArray["italy"]="marco";
How can I do this on C#? Is it possible? Thanks
That's called an associative array, and C# doesn't support them directly. However, you can achieve exactly the same the effect with a Dictionary<TKey, TValue>. You can add values with the Add method (which will throw an exception if you try to add an already existing key), or with the indexer directly, as below (this will overwrite the existing value if you use the same key twice).
Dictionary<string, string> dict = new Dictionary<string, string>();
dict["canada"] = "hello";
dict["america"] = "my";
dict["brazil"] = "name";
dict["gosaldo"] = "is";
dict["italy"] = "marco";
C# has a Dictionary class (and interface) to deal with this sort of storage. For example:
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("canada", "hello");
dict.Add("america", "my");
dict.Add("brazil", "name");
dict.Add("gosaldo", "is");
Here are the docs: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
With a Dictionary you will be able to set the "key" for each item as a string, and and give them string "values". For example:
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("canada", "hello");
You're looking for an associative array and I think this question is what you're looking for.
Hi in Actionscript I may refer to a variable within an object thusly
objectName["variableName"] = "Some value";
How would I do the equivalent in c#
thanks
Dictionary is not part of the language & is provided in the framework.
Use Dictionary<Key, Value>.
e.g.
Dictionary<string, string> myData = new Dictionary<string, string>();
myData.Add("first", "stack");
myData.Add("second", "overflow");
Console.WriteLine(myData["first"]);
It depends.
If you genuinely mean an object you want:
objectName.variableName = "Some value";
If you want a Map (also known as an associative array) your best solution is a Dictionary:
Dictionary<string, string> d = new Dictionary<string, string>()
d["variableName"] = "Some value";
You could use a dictionary...
using System.Collections.Generic;
public class x{
public method1() {
var objectName = new Dictionary<string, object>();
objectName["variableName"] = "Some value";
}
}
Or, use strongly-typed properties (safer and fast running), recommended where you know variable names at compile-time.
public class Person{
public string Name {get;set;}
public int Age {get;set;}
}
// and use it as follows in your functions
var person1 = new Person() {
Name = "Fred",
Age = 21,
};
// again, to demonstrate different syntax to do same thing
var person2 = new Person();
person2.Name = "Danny";
person2.Age = 2;
person2.Age = "x"; // won't compile - expects int, hence safer
What type of variable? It sounds like a dictionary/hashtable is most suited. These have index operators [], so the syntax will be identical.
C# doesn't have "objects with variables". C# objects have properties which are accessed like MyObject.PropertyName.
What you're referring to is a key-value collection. A Dictionary is C# implementation of this. Note that a dictionary only allows for unique keys. You can use it generically so you can use it like this.
Dictionary<string, string> myValues = new Dictionary<string, string>();
myValues.Add("variableName", "variableValue");
or
Dictionary<string, int> familySizes = new Dictionary<string, int>();
familySizes.Add("simpsons", 5);
As you can see you can choose what datatypes you use.
I would like to use Dictionary as TKey in another Dictionary. Something similar to python. I tried this but it gives me errors.
Dictionary<Dictionary<string, string>, int> dict = new Dictionary<Dictionary<string, string>, int>();
Dictionary<string, string> dict2 = new Dictionary<string, string>();
dict2["abc"] = "def";
dict[dict["abc"] = 20;
What error is it giving you? Is it complaining about your missing bracket on line 4?
Line 4 looks like it should be:
dict[dict["abc"]] = 20;
However, you probably mean this, since "abc" is not a key of dict:
dict[dict2["abc"]] = 20;
But dict2["abc"] is a string, when the key of dict is supposed to be a Dictionary<string, string>.
But let's re-examine your original goal at this point before going to far down this path. You shouldn't be using mutable types as dictionary keys in the first place.
This may be the code you're looking for:
Dictionary<string, int> dict = new Dictionary<string, int>();
Dictionary<string, string> dict2 = new Dictionary<string, string>();
dict2["abc"] = "def";
dict[dict2["abc"]] = 20;
But it's hard to tell for sure.
Just to throw this in there, I often find that dealing with complicated dictionaries like you describe it's far better to use real names with them rather than trying to let the reader of the code sort it out.
You can do this one of two ways depending on personal preference. Either with a using statement to create a complier alias. Note you have to use the fully qualified type name since this is a compiler alias.
using ComplexKey = System.Collections.Generic.Dictionary<String, String>;
using ComplexType = System.Collections.Generic.Dictionary<
System.Collections.Generic.Dictionary<String, String>,
String
>;
Or you can go the full blown type way and actually create a class that inherits from Dictionary.
class ComplexKey : Dictionary<String, String> { ... }
class ComplexType : Dictionary<ComplexKey, String> { ... }
Doing this will make it far easier for both you and the reader of your code to figure out what you're doing. My general rule of thumb is if I'm creating a generic of a generic it's time to look at building some first class citizens to represent my logic rather.
It's because the "dict["abc"] is not dictionary, but "string".
The correct, what you asked is:
Dictionary<Dictionary<string, string>, int> dict = new Dictionary<Dictionary<string, string>, int>();
Dictionary<string, string> dict2 = new Dictionary<string, string>();
dict2["abc"] = "def";
dict[dict2] = 20;
But i'm not sure, this is what you realy want/need.