I would like to refactor my code and implement a method instead of writing/copying the same code again again.
I check TOC whether a certain Layer exists. If TOC does not have it I create the FeatureLayer. I use this process a lot in my applications.
So, here is the method but it does not work
public Task<FeatureLayer> AddLayerToTOC(Uri layerUri, string layerName)
{
return QueuedTask.Run(() =>
{
MainGridEnabled = false;
FeatureLayer fLayer;
TOCLayers = MapView.Active.Map.Layers;
var TOCLayerNames = TOCLayers.Select(p => p.Name);
if (TOCLayerNames.Contains(layerName) == false)
{
fLayer = LayerFactory.Instance.CreateFeatureLayer(layerUri, MapView.Active.Map, 0, layerName);
}
else
{
foreach (Layer layer in TOCLayers)
{
if (layer.Name == layerName)
{
fLayer = layer as FeatureLayer;;
}
}
}
MapView.Active.ZoomToSelected();
MainGridEnabled = true;
return fLayer;
});
}
I get CS0165: Use of unassigned local variable
Problem coming from else {} part because VS thinks fLayer not assigned but I will do that while application is running.
Is there a solution to this or another way of doing it?