带字符串和整数的数组 [英] Arrays with string and integer

查看:103
本文介绍了带字符串和整数的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:

我有一个定义为'Data'的数组,它带有一个字符串和一个整数。我正在学习,因为我去了。

我想我到目前为止设法填充这个数组。为了确保这一点,我想打印那些并看到这个数组中的元素。

我尝试了一些使用Msg框的东西,比如

 MsgBox(filetable1( 0) ))



我收到以下错误:

抛出异常:  System.InvalidCastException'   Microsoft.VisualBasic.dll 

其他信息:转换来自类型' 数据键入 String' 无效。

如果 一个处理程序 例外,程序可以安全地继续。





我尝试过:



  Dim  filetable1 作为 列表( 数据)
对于 x = 0 (lineCount1 - 2
''' ''c = 1
Array.Sort(file1items)
如果 file1items(x)= file1items(x + 1 然后
c = c + 1
否则
c = 1
filetable1.Add(数据(file1items(x),c))
结束 如果

下一步
公开 数据公共 PN 作为 字符串 公开计数作为 整数
公共 属性名称() As 字符串
获取
' 获取属性值。
返回 PN
结束 获取
设置 ByVal As 字符串
' 设置属性值。
PN =价值
结束 设置
结束 属性
公开 < span class =code-keyword> Property ct() As Integer
获取
' 获取属性值。
返回计算
结束 获取
设置 ByVal 作为 整数
' 设置属性值。
Count = Value
结束 设置
结束 Property
公共 Sub Capitalize()
' 将物业的价值大写。
PN = UCase(PN)
计数= UCase(计数)
结束 Sub
公共 Sub (PName As String ,ct1 作为 整数
名称= PName
ct = ct1
结束 Sub

结束 < span class =code-keyword> Class

解决方案

您的代码正在尝试将整个数据实例转换为一个字符串,它显然不能这样做。



您必须告诉它要显示的数据实例的哪个成员:

 MsgBox(filetable1 [0] .Name)





 MsgBox( filetable1 [0] .ct)





 MsgBox(String.Format(Name: {0}值:{1},filetable1 [0] .Name,filetable1 [0] .ct))


除了解决方案1:



用于提供您定义的类型( class struct )的通用字符串表示的常用技术是覆盖 System.Object.ToString()

Object.ToString方法(系统) [ ^ ]。



(是的,即使对于 struct ,这种覆盖也是可能的,尽管这些类型是值类型;这就是.NET统一类型系统的工作原理。)



有些人可能会说这是你的头脑,但最好不要这么想。覆盖对象方法是一项至关重要的事情。如果 ToString()(重要:没有任何参数),这是一种类型无关方式来提供字符串表示;在很多情况下,只需计算字符串,就可以直接修改演示文稿。例如, ListBox ComboBox 等UI类型将根据此函数返回的内容显示项目。



参见:重写System.Object.ToString()并实现IFormattable | David Hayden [ ^ ]。



计算表示具有多个属性(或者有时是其他成员)的对象的字符串的最佳方法是function string.Format

String.Format方法(系统) [ ^ ]。



-SA

Hi:
I have an array defined as 'Data' which carries a string and a integer. I am just learning as I go.
I think I have so far managed to populate this array. To ensure that, I want to print those and see the elements in this array.
I tried a few things using Msg box like

MsgBox(filetable1(0))


I get the following error:

Exception thrown: 'System.InvalidCastException' in Microsoft.VisualBasic.dll

Additional information: Conversion from type 'Data' to type 'String' is not valid.

If there is a handler for this exception, the program may be safely continued.



What I have tried:

Dim filetable1 As New List(Of Data)
    For x = 0 To (lineCount1 - 2)
        '''''c = 1
        Array.Sort(file1items)
        If file1items(x) = file1items(x + 1) Then
            c = c + 1
        Else
            c = 1
            filetable1.Add(New Data(file1items(x), c))
        End If

    Next
Public Class Data Public PN As String Public Count As Integer
Public Property Name() As String
    Get
        ' Gets the property value.
        Return PN
    End Get
    Set(ByVal Value As String)
        ' Sets the property value.
        PN = Value
    End Set
End Property
Public Property ct() As Integer
    Get
        ' Gets the property value.
        Return Count
    End Get
    Set(ByVal Value As Integer)
        ' Sets the property value.
        Count = Value
    End Set
End Property
Public Sub Capitalize()
    ' Capitalize the value of the property.
    PN = UCase(PN)
    Count = UCase(Count)
End Sub
Public Sub New(PName As String, ct1 As Integer)
    Name = PName
    ct = ct1
End Sub

End Class

解决方案

Your code is trying to convert the entire instance of Data to a string, which it obviously cannot do.

You have to tell it which member of the Data instance you want to display:

MsgBox(filetable1[0].Name)


or

MsgBox(filetable1[0].ct)


or

MsgBox(String.Format("Name: {0}    Value: {1}", filetable1[0].Name, filetable1[0].ct))


In addition to Solution 1:

The usual technique used to provide general-purpose string representation of the types you defined (class or struct) is overriding System.Object.ToString():
Object.ToString Method (System)[^].

(Yes, such override is possible even for struct, despite the fact that such types are value types; this is how .NET unified type system works.)

Some may say this is the over your head, but better don't think this way. Overriding Object methods is a fundamentally important thing. In case of ToString() (important: without any parameters), this is a type-agnostic way to provide a string representation; there are many cases where you can modify the presentation where you cannot do it in a direct way, by just calculation of strings. For example, UI types like ListBox or ComboBox will show the items based on what this function returns.

See also: Overriding System.Object.ToString() and Implementing IFormattable | David Hayden[^].

The best way to calculate some string representing an object with several properties (or, sometimes, other members) is the function string.Format:
String.Format Method (System)[^].

—SA


这篇关于带字符串和整数的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆