Private Class JSON
        Public Const Array As String = "JSON:Array"
        Public Const Null As String = "JSON:Null"
    End Class
    
    Private mUseNewParseMethod As Boolean

    Public Function ConvertToJSON(ByVal dt As DataTable) As String
        Dim o As Object = SerialiseGeneric(dt, True)
        Return JsonConvert.SerializeObject(o)
    End Function

    Public Function SerialiseGeneric(ByVal o As Object, ByVal removeArray As Boolean) As Object
        Dim dt As DataTable = TryCast(o, DataTable)
        If dt IsNot Nothing Then
            Return SerialiseDataTable(dt)
        End If

        Dim dr As DataRow = TryCast(o, DataRow)
        If dr IsNot Nothing Then
            Return SerialiseDataRow(dr, removeArray)
        End If

        Dim s As String = TryCast(o, String)
        If s IsNot Nothing AndAlso s = JSON.Null Then
            Return Nothing
        End If

        If o IsNot Nothing Then
            Return o
        End If

        Return Nothing
    End Function

    Public Function SerialiseDataTable(ByVal dt As DataTable) As Object
        If  IsSingleRow(dt) Then
            Return SerialiseGeneric(dt.Rows(0), False)
        Else
            Dim ja As New JArray()
            For Each r As DataRow In dt.Rows
                ja.Add(SerialiseGeneric(r, True))
            Next
            Return ja
        End If
    End Function

    Public Function IsSingleRow(ByVal dt As DataTable) As Boolean
        If dt.ExtendedProperties.Contains("SingleRow") Then
            Return CBool(dt.ExtendedProperties("SingleRow"))
        End If
        'Fallback for older versions of blueprism
        Return dt.Rows.Count = 1
    End Function

    Public Function SerialiseDataRow(ByVal dr As DataRow, ByVal removeArray As Boolean) As Object
        Dim jo As New JObject()
        For Each c As DataColumn In dr.Table.Columns
            Dim s As String = c.ColumnName
            If removeArray AndAlso s = JSON.Array Then
                Return SerialiseGeneric(dr(s), True)
            End If
            jo(s) = JToken.FromObject(SerialiseGeneric(dr(s), False))
        Next
        Return jo
    End Function

    Public Function ConvertToDataTable(ByVal json As String) As DataTable
        Dim o As Object = JsonConvert.DeserializeObject(json)
        Return DirectCast(DeserialiseGeneric(o, True), DataTable)
    End Function

    Private Function DeserialiseGeneric(ByVal o As Object, ByVal populate As Boolean) As Object
        Dim a As JArray = TryCast(o, JArray)
        If a IsNot Nothing Then
            Return If(mUseNewParseMethod,
                DeserialiseArrayWithoutJArray(a, populate),
                DeserialiseArray(a, populate)
            )
        End If

        Dim jo As JObject = TryCast(o, JObject)
        If jo IsNot Nothing Then
            Return DeserialiseObject(jo, populate)
        End If

        Dim jv As JValue = TryCast(o, JValue)
        If jv IsNot Nothing And jv.Value IsNot Nothing Then 
            Return jv.Value
        End If

        Return JSON.Null
    End Function

    Private Function GetKey(ByVal kv As KeyValuePair(Of String, JToken)) As String
        If kv.Key IsNot Nothing Then
            Return kv.Key.ToString()
        End If
        Return ""
    End Function


    Private Function DeserialiseObject(ByVal o As JObject, ByVal populate As Boolean) As DataTable
        Dim dt As New DataTable

        For Each kv As KeyValuePair(Of String, JToken) In o
            Dim type As Type = GetTypeOf(DeserialiseGeneric(kv.Value, False))
				If Not dt.Columns.Contains(kv.Key) Then
					dt.Columns.Add(GetKey(kv), type)
				End If
        Next

		'changes done to accomodate missing below columns

        If Not dt.Columns.Contains("DOC_TYPE_CD") Then
            dt.Columns.Add("DOC_TYPE_CD", GetTypeOf(Nothing))
        End If
        If Not dt.Columns.Contains("DOC_CAT_CD") Then
            dt.Columns.Add("DOC_CAT_CD", GetTypeOf(Nothing))
        End If
        If Not dt.Columns.Contains("DOC_SUBTYPE_CD") Then
            dt.Columns.Add("DOC_SUBTYPE_CD", GetTypeOf(Nothing))
        End If

        If populate Then
            Dim dr As DataRow = dt.NewRow()
            For Each kv As KeyValuePair(Of String, JToken) In o
                dr(getKey(kv)) = DeserialiseGeneric(kv.Value, True)
            Next
            dt.Rows.Add(dr)
        End If

        Return dt
    End Function

    Private Function DeserialiseArrayWithoutJArray(ByVal jarr As JArray, ByVal populate As Boolean) As DataTable
        Dim dt As New DataTable

        Dim first As Type = Nothing
        For Each e As Object In jarr
            If first Is Nothing Then
                first = GetTypeOf(DeserialiseGeneric(e, False))
            End If
            If GetTypeOf(DeserialiseGeneric(e, False)) IsNot first Then
                Throw New Exception("Data Type mismatch in array")
            End If
        Next

        Dim columns As New Specialized.OrderedDictionary()
        Dim allTypesInColumnsMatch = jarr.All(
            Function(e)
                If Not TypeOf e Is JObject Then Return False
                For Each pair As KeyValuePair(Of String, JToken) In DirectCast(e, JObject)
                    Dim val As Object = pair.Value
                    If TypeOf val Is JValue Then val = CType(val, JValue).Value
                    Dim tp = If(val, CObj("")).GetType()

                    If columns.Contains(pair.Key) Then
                        If tp <> columns(pair.Key) Then Return False
                    Else
                        columns(pair.Key) = tp
                    End If
                Next
                Return True

            End Function
        )

        If allTypesInColumnsMatch Then
            For Each pair As DictionaryEntry In columns
                Dim key As String = CStr(pair.Key)
                Dim tp as Type = CType(pair.Value, Type)
                If tp = GetType(JObject) OrElse tp = GetType(JArray) Then
                    dt.Columns.Add(key, GetType(DataTable))
                Else
                    dt.Columns.Add(key, tp)
                End If
            Next
        ElseIf first IsNot Nothing Then
            dt.Columns.Add(JSON.Array, first)
        End If

        If populate Then
            For Each e As Object In jarr
                Dim dr As DataRow = dt.NewRow()
                If allTypesInColumnsMatch Then
                    For Each pair As KeyValuePair(Of String, JToken) In DirectCast(e, JObject)
                        dr(pair.Key) = DeserialiseGeneric(pair.Value, True)
                    Next
                Else
                    dr(JSON.Array) = DeserialiseGeneric(e, True)
                End If

                dt.Rows.Add(dr)
            Next
        End If

        Return dt
    End Function

    Private Function DeserialiseArray(ByVal o As JArray, ByVal populate As Boolean) As DataTable
        Dim dt As New DataTable

        Dim first As Type = Nothing
        For Each e As Object In o
            If first Is Nothing Then
                first = GetTypeOf(DeserialiseGeneric(e, False))
            End If
            If GetTypeOf(DeserialiseGeneric(e, False)) IsNot first Then
                Throw New Exception("Data Type mismatch in array")
            End If
        Next
        If first IsNot Nothing Then
            dt.Columns.Add(JSON.Array, first)
        End If
		 
					
        If populate Then
            For Each e As Object In o
                Dim dr As DataRow = dt.NewRow()
                dr(JSON.Array) = DeserialiseGeneric(e, True)
                dt.Rows.Add(dr)
            Next
        End If
		
		

        Return dt
    End Function

    Private Function GetTypeOf(ByVal o As Object) As Type
        If o Is Nothing Then Return GetType(String)
        If o.GetType Is GetType(DateTime) Then Return GetType(String)
		Return o.GetType
    End Function  

Visual basic (VB.net) Online Compiler

Write, Run & Share VB.net code online using OneCompiler's VB.net online compiler for free. It's one of the robust, feature-rich online compilers for VB.net language, running on the latest version 16. Getting started with the OneCompiler's VB.net compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as VB.net. OneCompiler also has reference programs, where you can look for the sample code to get started with.

Read input from STDIN in VB.net

OneCompiler's VB.net online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample VB.net program which takes name as input and prints hello message with your name.

Public Module Program
	Public Sub Main(args() As string)
	 Dim name as String = Console.ReadLine()    ' Reading input from STDIN
   Console.WriteLine("Hello " & name)           ' Writing output to STDOUT
	End Sub
End Module

About VB.net

Visual Basic is a event driven programming language by Microsoft, first released in the year 1991.

Key Features

  • Beginner's friendly language.
  • Simple and object oriented programming language.
  • User friendly language and easy to develop GUI based applications.

Syntax help

Variables

Variable is a name given to the storage area in order to identify them in our programs.

Simple syntax of Variable declaration is as follows

Dim variableName [ As [ New ] dataType ] [ = initializer ]

Variable initialization

variableName = value

Conditional Statements

1. If

If condition-expression Then 
    'code
End If

2. If-else

If(conditional-expression)Then
   'code if the conditional-expression is true 
Else
  'code if the conditional-expression is false 
End If

3. If-else-if ladder

If(conditional-expression)Then
   'code if the above conditional-expression is true 
Else If(conditional-expression) Then
        'code if the above conditional-expression is true 
    Else
        'code if the above conditional-expression is false 
End If

4. Nested-If

If(conditional-expression)Then
   'code if the above conditional-expression is true
   If(conditional-expression)Then
         'code if the above conditional-expression is true 
   End If
End If

5. Select Case

Select [ Case ] expression
   [ Case expressionlist
      'code ]
   [ Case Else
      'code ]
End Select

Loops

1. For..Next

For counter [ As datatype ] = begin To end [ Step step ]
   'code
   [ Continue For ]
   'code
   [ Exit For ]
   'code
Next [ counter ]

2. For..Each

For Each element [ As datatype ] In group
   'code
   [ Continue For ]
   'code
   [ Exit For ]
   'code
Next [ element ]

3. While

While conditional-expression
   'Code 
   [ Continue While ]
   'Code
   [ Exit While ]
   'Code
End While

4. Do-while

Do { While | Until } conditional-expression
   'Code
   [ Continue Do ]
   'Code
   [ Exit Do ]
   'Code
Loop
Do
   'Code
   [ Continue Do ]
   'Code
   [ Exit Do ]
   'Code
Loop { While | Until } conditional-expression

Procedures

Procedure is a sub-routine which contains set of statements. Usually Procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Procedures are of two types.

1. Functions

Functions return a value when they are called.

[accessModifiers] Function functionName [(parameterList)] As returnType
   'code
End Function

2. Sub-Procedures

Sub-procedures are similar to functions but they don't return any value.

Sub ProcedureName (parameterList)
'Code
End Sub