How to use Dictionary?
-
Hi,all I want to use Dictionary<> to make a map.I create a class named ILayer.So,I write Dictionary g_LyrNameToILayer = new Dictionary(); But it was wrong,who can tell me what shoud i do? Thanks a lot!
The generic
Dictionary<>
class has to be parameterized with two types (one for the keys and one for the values). Guessing from your variable name you must write something like this:Dictionary<string, ILayer> g_LyrNameToILayer = new Dictionary<string, ILayer>();
Regards, mav -- Black holes are the places where God divided by 0...
-
The generic
Dictionary<>
class has to be parameterized with two types (one for the keys and one for the values). Guessing from your variable name you must write something like this:Dictionary<string, ILayer> g_LyrNameToILayer = new Dictionary<string, ILayer>();
Regards, mav -- Black holes are the places where God divided by 0...
-
Thanks,What you said is right,but,i want the Key is type of string,and the Value is type of pointer,It seems to be that pointer can't be a para in Dictionary. so,how shoud i do?
What mav is saying should be sufficient if you're mapping up 'Name' with an instance of 'ILayer'.
Dictionary<string, ILayer> layerDict = new Dictionary<string, ILayer>();
List<ILayer> layers = GetLayers(); //some method of propagating layers.int counter = 0;
foreach(ILayer layer in layers)
{
layerDict.Add("Layer" + counter, layer);
counter++;
}...
//Retrieve a layer
ILayer layerOne = null;if(layerDict.ConstainsKey("Layer1"))
{
layerOne = layerDict["Layer1"];
}//do something with the layer.
Best regards! -Larantz-
for those about to code, we salute you
http://www.tellus-software.com -
What mav is saying should be sufficient if you're mapping up 'Name' with an instance of 'ILayer'.
Dictionary<string, ILayer> layerDict = new Dictionary<string, ILayer>();
List<ILayer> layers = GetLayers(); //some method of propagating layers.int counter = 0;
foreach(ILayer layer in layers)
{
layerDict.Add("Layer" + counter, layer);
counter++;
}...
//Retrieve a layer
ILayer layerOne = null;if(layerDict.ConstainsKey("Layer1"))
{
layerOne = layerDict["Layer1"];
}//do something with the layer.
Best regards! -Larantz-
for those about to code, we salute you
http://www.tellus-software.com -
What mav is saying should be sufficient if you're mapping up 'Name' with an instance of 'ILayer'.
Dictionary<string, ILayer> layerDict = new Dictionary<string, ILayer>();
List<ILayer> layers = GetLayers(); //some method of propagating layers.int counter = 0;
foreach(ILayer layer in layers)
{
layerDict.Add("Layer" + counter, layer);
counter++;
}...
//Retrieve a layer
ILayer layerOne = null;if(layerDict.ConstainsKey("Layer1"))
{
layerOne = layerDict["Layer1"];
}//do something with the layer.
Best regards! -Larantz-
for those about to code, we salute you
http://www.tellus-software.com