I am having trouble with this class. I am converting from VB6 VB.NET to C#.
In particular the Item
, AddBefore
and AddAfter
methods. For the Item
I am passing in a geometry shape.
I need to use Ordered Dictionary because I need a format of m_oCol(string, clsFeature)
. In this collection, I need to insert clsFeatures
in a certain order, could be 1, 6, 3, 4, 5, 2 because of processing rules. I have another class that accesses this class.
// ************************** Ordered Dictionary ****************
// https://stackoverflow.com/questions/2722767/c-sharp-order-preserving-data-structures
// http://www.go4expert.com/articles/understanding-c-sharp-dictionaries-t30034/
public OrderedDictionary m_oCol;
public Dictionary<string, string> m_oColReverse;
public clsFeatureCollection()
: base()
{
m_oCol = new OrderedDictionary();
m_oColReverse = new Dictionary<string, string>();
}
public IEnumerator GetEnumerator()
{
return m_oCol.GetEnumerator();
}
public void Add(IFeature pFeature, string strBefore = "", string strAfter = "", bool bReverse = false)
{
if (bReverse == true)
{
m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim());
}
if (!ContainsItem(pFeature.OID))
{
m_oCol.Add(pFeature.OID.ToString(), pFeature.ShapeCopy);
}
}
public void AddBefore(IFeature pFeature, string strBefore, bool bReverse = false)
{
if (bReverse == true)
{
m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim());
}
if (!ContainsItem(pFeature.OID))
{
if (strBefore != null)
{
int intStringBefore = Int32.Parse(strBefore);
int index = m_oCol.FindIndex(a => a.OID == intStringBefore);
if (index > 0)
{
m_oCol.Insert(index - 1, pFeature.OID.ToString(), pFeature.ShapeCopy);
}
else
{
m_oCol.Insert(0, pFeature.OID.ToString(), pFeature.ShapeCopy);
}
}
}
}
public void AddAfter(IFeature pFeature, string strAfter, bool bReverse = false)
{
if (bReverse == true)
{
m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim());
}
if (!ContainsItem(pFeature.OID))
{
if (!string.IsNullOrEmpty(strAfter))
{
int intStringAfter = Int32.Parse(strAfter);
int index = m_oCol.FindIndex(a => a.OID == intStringAfter);
m_oCol.Insert(index + 1, pFeature.OID.ToString(), pFeature.ShapeCopy);
}
else
{
m_oCol.Add(pFeature.OID.ToString(), pFeature.ShapeCopy);
}
}
}
public int Count
{
get { return m_oCol.Count; }
}
public void Remove(int Id)
{
m_oCol.RemoveAt(Id);
}
// public clsFeature this[int Position]
// {
// get { return m_oCol[Position]; }
// set;
// }
public clsFeature Item(int Position)
{
clsFeature value = default(clsFeature);
value = m_oCol[Position]; // .GetObjectData(, Position);
return value;
}
public void Clear()
{
m_oCol = new OrderedDictionary();
m_oColReverse = new Dictionary<string, string>();
}
public bool Reverse(string valueRenamed)
{
bool bReverse = false;
try
{
if (m_oColReverse.ContainsValue(valueRenamed))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
if (ex is ArgumentException | ex is IndexOutOfRangeException)
{
bReverse = false;
}
}
return bReverse;
}
public bool ContainsItem(int oidValue)
{
bool bContainsItem = false;
int intOID = oidValue;
try
{
// dictionary
if (m_oCol.Contains(intOID))
{
bContainsItem = true;
}
else
{
bContainsItem = false;
}
return bContainsItem;
}
catch (Exception ex)
{
if (ex is ArgumentException | ex is IndexOutOfRangeException)
{
bContainsItem = false;
}
}
return bContainsItem;
}