【问题标题】:How can I access a class variable via an array in VB.NET?如何通过 VB.NET 中的数组访问类变量?
【发布时间】:2009-03-23 19:57:13
【问题描述】:

如果我有以下类和声明:

Public Class objLocation
   Public SysLocationId As String
   Public NameFull As String
   Public LatRaw As String
   Public LongRaw As String
   Public Active As Integer
End Class

dim lLocation as new objLocation

我可以访问每个变量,因此lLocation.SysLocationId 等。是否有替代方法,所以我可以通过索引访问每个变量,例如 lLocation(0)、lLocation(1) 等,这给了我通过 for next 循环或与其他来源(如数据表)进行比较的灵活性。

【问题讨论】:

  • 类名上的匈牙利符号...这是不对的。类不是对象!
  • .Net 中任何地方的匈牙利语都不正确。

标签: vb.net class


【解决方案1】:

如果您的目标是比较,通常您要做的是实现IComparable 接口或重载>< 运算符(如果需要排序)或仅重载= 运算符(如果等价需要)。

您只需在一个位置编写一个函数,并在需要进行比较时调用该函数。与存储在数据库中的对象进行比较也是如此。将这些函数放在哪里取决于您的应用程序架构,但对于对象-对象比较,您可以将其作为 objLocation 类本身的一部分。

【讨论】:

    【解决方案2】:

    对此没有内置语言支持。但是,您可以通过在类上创建默认索引器属性来模拟这一点

    Public Class objLocation
      ...
    Default Public ReadOnly Property Indexer(ByVal index As Integer)
        Get
            Select Case index
                Case 0
                    Return SysLocationId
                Case 1
                    Return NameFull
                Case 2
                    Return LatRaw
                Case 3
                    Return LongRaw
                Case 4
                    Return Active
                Case Else
                    Throw New ArgumentException
            End Select
        End Get
      End Property
    

    那么你可以如下使用它

    Dim x As objLocation = GetObjLocation
    Dim latRaw = x(2)
    

    【讨论】:

    • 所有这些都是很好的答案,但我认为这个最适合我想做的事情,即按索引循环遍历类变量,获取值,然后将它们分配给数据库.
    【解决方案3】:

    不,你不能直接这样做。

    您必须使用反射来获取属性,但您必须注意不能保证返回的属性的顺序(如果您想对它们进行数字索引,这一点很重要)。

    因此,在使用属性(和索引)时,您必须保持排序顺序一致。

    【讨论】:

      【解决方案4】:

      您是否在寻找列表:

      Dim LocationList As List<objLocation>;
      
      For Each loc As objLocation In LocationList
          loc.whatever
      Next
      

      或使用索引:

      For i = 0 To LocationList.Length - 1
          LocationList(i).whatever
      Next
      

      对不起,如果 VB 语法不正确...我最近一直在做 C#,但没有 VB

      【讨论】:

        【解决方案5】:

        您可以按照以下方式进行操作。它是 C#,与在 VB 中使用索引器有些不同,但您绝对可以让它在 VB 中工作。

        public class ObjLocation
        {
           private String[] Properties = new String[5];
        
           public const Int32 IndexSysLocationId = 0;
           public const Int32 IndexNameFull = 1;
           public const Int32 IndexLatRaw = 2;
           public const Int32 IndexLongRaw = 3;
           public const Int32 IndexActive = 4;
        
           // Repeat this for all properties
           public String SysLocationId
           {
              get { return this.Properties[ObjLocation.IndexSysLocationId]; }
              set { this.Properties[ObjLocation.IndexSysLocationId] = value; }
           }         
        
           public String this[Int32 index]
           {
              get { return this.Properties[index]; }
              set { this.Properties[index] = value; }
           }
        }
        

        现在您有了与以前一样具有属性的对象,但存储在一个数组中,您还可以通过索引器访问它们。

        【讨论】:

          【解决方案6】:

          我在公共结构体中实现的这个方法返回存储在结构体中的字符串变量数组:

          Public Shared Function returnArrayValues() As ArrayList
          
              Dim arrayOutput As New ArrayList()
              Dim objInstance As New LibertyPIMVaultDefaultCategories()
              Dim t As Type = objInstance.GetType()
              Dim arrayfinfo() As System.Reflection.FieldInfo = t.GetFields()
              For Each finfo As System.Reflection.FieldInfo In arrayfinfo
                  Dim str As String = finfo.GetValue(objInstance)
                  arrayOutput.Add(str)
              Next
          
              Return arrayOutput
          End Function
          

          将它放在结构或类中。也许这个示例代码会有所帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-14
            • 2020-04-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多