user3020047
user3020047

Reputation: 888

How to convert a list of different data types into string in vb.net

I created a list using a class which has different data types. It is populated as follows:

   [1/16/2015 10:30:14 PM] 241.167.2.72:5

   [1/17/2015 11:30:06 PM] 100.133.2.55:6

   [1/18/2015 12:30:33 PM] 206.140.3.10:7

Now I just want to display this in a textbox on a page. But get this:

   ClientWebApp.DedicatedServerApi.Formatted_DDoS_SampleFlows

   ClientWebApp.DedicatedServerApi.Formatted_DDoS_SampleFlows

   ClientWebApp.DedicatedServerApi.Formatted_DDoS_SampleFlows

I'm using this line of code with the ToString()...but obviously it is not correct.

strSampleFlowLine = formattedDDoSSampleFlow.ToString() & vbCrLf & vbCrLf

Any Ideas?

The code (and classes) that attempts to populate the text box on the page:

    Dim ddosDetailsInfoResult As New DedicatedServerApi.DDoSDetailsInfoResult
    Dim formattedDDoSSampleFlow As New DedicatedServerApi.Formatted_DDoS_SampleFlows
    ' Create a list field - an array of the DDoS sample flows.
    Dim formattedDDoSSampleFlowsList As New List(Of DedicatedServerApi.Formatted_DDoS_SampleFlows)

        If ddosDetailsInfoResult.DDoS_Details.Formattted_DDoS_SampleFlows_List.Count > 0 Then
            ' Note: had to add .ToList() as it was giving a syntax error: cannot be converted to a 1-dimensional array.
            ' Does not make sense as the "Formattted_DDoS_SampleFlows_List" variable is defined exactly like the receiving field.
            formattedDDoSSampleFlowsList = ddosDetailsInfoResult.DDoS_Details.Formattted_DDoS_SampleFlows_List.ToList()

            For Each formattedDDoSSampleFlow In formattedDDoSSampleFlowsList

                ' Example of entry in the list:    [1/16/2015 10:30:14 PM] 241.167.2.72:5

                strSampleFlowLine = formattedDDoSSampleFlow.ToString() & vbCrLf & vbCrLf

                ' Build the sample flow list textbox.
                txtGDHDApiSampleFlowList.Text = txtGDHDApiSampleFlowList.Text & strSampleFlowLine
            Next
        Else
            'An empty list.
            txtGDHDApiSampleFlowList.Text = ""
        End If

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Holds formatted DDoS sample flows settings.
'   [1/16/2015 10:30:14 PM] 241.167.2.72:5
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Public Class Formatted_DDoS_SampleFlows
    Private _open_bracket_delimeter As String

    Public Property Open_Bracket_Delimeter() As String
        Get
            Return _open_bracket_delimeter
        End Get

        Set(value As String)
            _open_bracket_delimeter = value
        End Set
    End Property

    Private _time_received As Date

    Public Property Time_Received() As Date
        Get
            Return _time_received
        End Get

        Set(value As Date)
            _time_received = value
        End Set
    End Property

    Private _close_backet_and_space_delimeter As String

    Public Property Close_Bracket_And_Space_Delimeter() As String
        Get
            Return _close_backet_and_space_delimeter
        End Get

        Set(value As String)
            _close_backet_and_space_delimeter = value
        End Set
    End Property

    Private _source_ip As String

    Public Property Source_IP() As String
        Get
            Return _source_ip
        End Get

        Set(value As String)
            _source_ip = value
        End Set
    End Property

    Private _colon1_delimeter As String

    Public Property Colon1_Delimeter() As String
        Get
            Return _colon1_delimeter
        End Get

        Set(value As String)
            _colon1_delimeter = value
        End Set
    End Property

    Private _source_port As String

    Public Property Source_Port() As String
        Get
            Return _source_port
        End Get

        Set(value As String)
            _source_port = value
        End Set
    End Property 
End Class

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Holds DDoSDetailsInfoResult.
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Public Class DDoSDetailsInfoResult
    Private _message As String

    Public Property Message() As String
        Get
            Return _message
        End Get

        Set(value As String)
            _message = value
        End Set
    End Property

    Private _ddos_details As New DDoS_Details

    Public Property DDoS_Details() As DDoS_Details
        Get
            Return _ddos_details
        End Get

        Set(ByVal value As DDoS_Details)
            _ddos_details = value
        End Set
    End Property
End Class    

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Holds DDoS details.
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Public Class DDoS_Details
    Private _formatted_ddos_sampleflows_list As New List(Of Formatted_DDoS_SampleFlows)

    Public Property Formattted_DDoS_SampleFlows_List() As List(Of Formatted_DDoS_SampleFlows)
        Get
            Return _formatted_ddos_sampleflows_list
        End Get

        Set(ByVal value As List(Of Formatted_DDoS_SampleFlows))
            _formatted_ddos_sampleflows_list = value
        End Set
    End Property
End Class

Upvotes: 0

Views: 63

Answers (2)

Marc Johnston
Marc Johnston

Reputation: 1286

use CDate to convert the original value into a Date. The ToString is being operated on the entire class. Then use String.Format to output the date into the correct format you want. Should look something closer to this:

strSampleFlowLine = String.Format("MM/dd/yyyy", CDate(formattedDDoSSampleFlow.something)) & vbCrLf & vbCrLf

Upvotes: 0

the_lotus
the_lotus

Reputation: 12748

Your Formatted_DDoS_SampleFlows class need to define what ToString does.

Something like:

Class Formatted_DDoS_SampleFlows

    Public Overrides ToString() As String

        Return _open_bracket_delimeter & _time_received.ToString() & _close_backet_and_space_delimeter & _source_ip
    End Sub

End Class

Upvotes: 2

Related Questions