大家好,怎么样?
我为了我的生活而无法解决这个问题。我一直在转换GIS应用程序。我所拥有的基本上是VB中的一个集合,我需要将它转换为c#中的链表。
欢迎任何帮助入门。
VB代码在
之下Imports ESRI.ArcGIS.esriSystem
Public Class clsFeature
Private m_OID As Integer
Private m_Geometry As ESRI.ArcGIS.Geometry.IGeometry
Public Sub New(ByRef iOID As Integer, ByRef pGeometry As ESRI.ArcGIS.Geometry.IGeometry)
m_OID = iOID
m_Geometry = pGeometry
End Sub
Public ReadOnly Property OID() As Integer
Get
OID = m_OID
End Get
End Property
Public ReadOnly Property Geometry() As ESRI.ArcGIS.Geometry.IGeometry
Get
Geometry = m_Geometry
End Get
End Property
End Class
Friend Class clsFeatureCollection
Implements System.Collections.IEnumerable
' linkedlist???????????????????????????????????????
Private m_oCol As Collection
Private m_oColReverse As Collection
Public Sub New()
MyBase.New()
m_oCol = New Collection
m_oColReverse = New Collection
End Sub
Public Sub Add(ByRef pFeature As ESRI.ArcGIS.Geodatabase.IFeature, Optional ByRef strBefore As String = "", Optional ByRef strAfter As String = "", Optional ByRef bReverse As Boolean = False)
'Create a new cFoo object based on parameters
'passed to this method, then add the new cFoo to
'the private collection, and key it by a
'unique identifier built into cFoo
'so we can retrieve it quickly later
'Add the new foo object to the collection
If bReverse Then
m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim())
End If
If Not ContainsItem(pFeature.OID.ToString().Trim()) Then
If strBefore <> "" Then
m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim(), strBefore)
ElseIf strAfter <> "" Then
m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim())
Else
m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim())
End If
End If
End Sub
Public Sub AddBefore(ByRef pFeature As ESRI.ArcGIS.Geodatabase.IFeature, ByRef strBefore As String, Optional ByRef bReverse As Boolean = False)
'Create a new cFoo object based on parameters
'passed to this method, then add the new cFoo to
'the private collection, and key it by a
'unique identifier built into cFoo
'so we can retrieve it quickly later
'Add the new foo object to the collection
If bReverse Then
m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim())
End If
If Not ContainsItem(pFeature.OID.ToString().Trim()) Then
If strBefore <> "" Then
m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim(), strBefore)
Else
m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim())
End If
End If
End Sub
Public Sub AddAfter(ByRef pFeature As ESRI.ArcGIS.Geodatabase.IFeature, ByRef strAfter As String, Optional ByRef bReverse As Boolean = False)
'Create a new cFoo object based on parameters
'passed to this method, then add the new cFoo to
'the private collection, and key it by a
'unique identifier built into cFoo
'so we can retrieve it quickly later
'Add the new foo object to the collection
If bReverse Then
m_oColReverse.Add(pFeature.OID.ToString().Trim(), pFeature.OID.ToString().Trim())
End If
If Not ContainsItem(pFeature.OID.ToString().Trim()) Then
If strAfter <> "" Then
m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim(), , strAfter)
Else
m_oCol.Add(New clsFeature(pFeature.OID, pFeature.ShapeCopy), pFeature.OID.ToString().Trim())
End If
End If
End Sub
Public ReadOnly Property Count() As Short
Get
'Return the number of objects in m_oCol
Count = m_oCol.Count()
End Get
End Property
Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
GetEnumerator = m_oCol.GetEnumerator
End Function
Public Sub Remove(ByRef vIndex As Object)
'Remove the specified object. Note here
'that this method will operate on either
'the index of the object we want removed
'or the key of the object we want removed
m_oCol.Remove(vIndex)
End Sub
Public Function Item(ByRef vIndex As Object) As clsFeature
'Retrieve the specified object. Note here
'that this method will operate on either
'the index of the object we want
'or the key of the object we want
Item = m_oCol.Item(vIndex)
End Function
Public Sub Clear()
'remove all objects from the private collection
m_oCol = New Collection
m_oColReverse = New Collection
End Sub
Public Function Reverse(ByRef val_Renamed As Object) As Boolean
Try
If m_oColReverse.Contains(val_Renamed) Then
Return True
Else
Return False
End If
Catch ex As Exception
If TypeOf ex Is ArgumentException Or TypeOf ex Is IndexOutOfRangeException Then
Reverse = False
End If
End Try
End Function
Public Function ContainsItem(ByRef val_Renamed As Object) As Boolean
Try
If m_oCol.Contains(val_Renamed) Then
Return True
Else
Return False
End If
Catch ex As Exception
If TypeOf ex Is ArgumentException Or TypeOf ex Is IndexOutOfRangeException Then
ContainsItem = False
End If
End Try
End Function
C#代码 - 一旦我得到正确的链表,我就可以完成其余的
namespace NSTDB_QC_Utility
{
public class clsFeature
{
private int m_OID;
private IGeometry m_Geometry;
public clsFeature(int iOID, IGeometry pGeometry)
{
m_OID = iOID;
m_Geometry = pGeometry;
}
public int OID
{
get { return m_OID; }
}
public IGeometry Geometry
{
get { return m_Geometry; }
}
}
public class clsFeatureCollection : System.Collections.IEnumerable
{
// used dictionary -> Should really use a linked list because of the strBefore and strAfter
// possible but need a way to handle m_ocol and strbefore - result was to reverse m_ocol on the strBefore
public LinkedList<clsFeature> m_oCol;
// public Dictionary<int, object> m_oCol;
public Dictionary<string, string> m_oColReverse;
public clsFeatureCollection()
: base()
{
m_oCol = new LinkedList<clsFeature>();
// m_oCol = new Dictionary<int, object>();
m_oColReverse = new Dictionary<string, string>();
}
public IEnumerator GetEnumerator()
{
return m_oCol.GetEnumerator();
}
答案 0 :(得分:0)
VB 'Collection' is a strange beast in that it allows treating the collection alternately as a keyed list or positional list so a single .NET collection will not capture everything. Also, it's 1-based, unlike nearly all other collections. I came up with the following in order to understand the VB 'Collection' type better - feel free to use it. Note that it's intended as a drop-in replacement so it uses 1-based parameters (yuk). I made the replacement collection generic as an improvement - replace 'Collection' with 'Collection<object>' for the exact VB equivalent, but you might want to specify the actual type even though you don't do this in VB.
public class Collection<T>
{
private System.Collections.Generic.List<T> objects = new System.Collections.Generic.List<T>();
private System.Collections.Generic.List<string> keys = new System.Collections.Generic.List<string>();
public void Add(T newObject, string key = null, object before = null, object after = null)
{
if (after != null)
{
if (after as string != null)
Insert(newObject, keys.IndexOf(after as string) + 1, key);
else
Insert(newObject, (int)after, key);
}
else if (before != null)
{
if (before as string != null)
Insert(newObject, keys.IndexOf(before as string), key);
else
Insert(newObject, (int)before - 1, key);
}
else
Insert(newObject, objects.Count, key);
}
private void Insert(T newObject, int index, string key)
{
objects.Insert(index, newObject);
keys.Insert(index, key);
}
public void Clear()
{
objects.Clear();
keys.Clear();
}
public bool Contains(string key)
{
return keys.Contains(key);
}
public int Count
{
get
{
return objects.Count;
}
}
public void Remove(string key)
{
RemoveAt(keys.IndexOf(key));
}
public void Remove(int positionOneBased)
{
RemoveAt(positionOneBased - 1);
}
private void RemoveAt(int index)
{
objects.RemoveAt(index);
keys.RemoveAt(index);
}
public T this[int positionOneBased]
{
get
{
return objects[positionOneBased - 1];
}
}
public T this[string key]
{
get
{
return objects[keys.IndexOf(key)];
}
}
public System.Collections.Generic.IEnumerator<T> GetEnumerator()
{
return objects.GetEnumerator();
}
}
答案 1 :(得分:-1)
以下是我提出的最终工作解决方案。它与其他类似的集合集成。我最初使用了一个列表,但我需要一个字符串作为其他集合的键
// ************************** Ordered Dictionary - works ****************
// http://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 OrderedDictionary m_oColReverse;
public clsFeatureCollection()
: base()
{
m_oCol = new OrderedDictionary();
m_oColReverse = new OrderedDictionary();
}
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.ToString()))
{
m_oCol.Add(pFeature.OID.ToString(), new clsFeature(pFeature.OID, 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.ToString()))
{
if (strBefore != null)
{
int index = GetIndex(m_oCol, strBefore);
if (index > 0)
{
m_oCol.Insert(index - 1, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy));
}
else
{
m_oCol.Insert(0, pFeature.OID.ToString(), new clsFeature(pFeature.OID, 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.ToString()))
{
if (!string.IsNullOrEmpty(strAfter))
{
int index = GetIndex(m_oCol, strAfter);
m_oCol.Insert(index + 1, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy));
}
else
{
m_oCol.Insert(0, pFeature.OID.ToString(), new clsFeature(pFeature.OID, pFeature.ShapeCopy));
}
}
}
public int Count
{
get { return m_oCol.Count; }
}
public void Remove(int Id)
{
m_oCol.RemoveAt(Id);
}
public clsFeature Item(int Position)
{
try
{
clsFeature value = (clsFeature)m_oCol.Cast<DictionaryEntry>().ElementAt(Position).Value;
return value;
}
catch (Exception)
{
throw;
}
}
public void Clear()
{
m_oCol = new OrderedDictionary();
m_oColReverse = new OrderedDictionary();
}
public bool Reverse(string valueRenamed)
{
bool bReverse = false;
try
{
if (m_oColReverse.Contains(valueRenamed))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
if (ex is ArgumentException | ex is IndexOutOfRangeException)
{
bReverse = false;
}
}
return bReverse;
}
public bool ContainsItem(string oidValue)
{
bool bContainsItem = false;
string intOID = oidValue.ToString();
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;
}
public static int GetIndex(OrderedDictionary dictionary, string key)
{
for (int index = 0; index < dictionary.Count; index++)
{
if (dictionary[index] == dictionary[key])
{
return index;
}
}
return -1;
}
// ****************************** End Ordered Dictionary - works **********************