Click here to Skip to main content
14,694,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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
        If file1items(x) = file1items(x + 1) Then
            c = c + 1
            c = 1
            filetable1.Add(New Data(file1items(x), c))
        End If

Public Class Data Public PN As String Public Count As Integer
Public Property Name() As String
        ' 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
        ' 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
Updated 6-May-16 10:03am
What makes you thinking that you can cast it? Where a string is expected, supply a sting, not Data or anything else.
Be of strings are a kind of universal data is a very usual fallacy in the beginners these days, try to avoid this misconception.

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(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)[^].


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900