C to Visual Basic (VB.net) converter Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

C to Visual Basic (VB.net) converter 29


Objetive:

Create a program to convert simple C programs, such as the following one, to Visual Basic (VB.net):

Note: the resulting program must compile correctly. Test it with other similar C programs.


Code:

Imports System
Imports System.IO
Class ConvertOfCToCSharp
    Private Shared Sub Main()
        Console.Write("Name of file: ")
        Dim name As String = Console.ReadLine()

        If Not File.Exists(name) Then
            Console.WriteLine("Not found!")
        Else

            Try
                Dim fileC As StreamReader = New StreamReader(name)
                Dim fileCSharp As StreamWriter = New StreamWriter(name & ".cp")
                Dim line As String

                Do
                    line = fileC.ReadLine()

                    If line IsNot Nothing Then
                        line = line.Replace("#include ", "using System;/npublic class Example{")
                        line = line.Replace("int main()", "public static void Main()")

                        If line.Contains("scanf(") Then
                            line = line.Replace("scanf(""%d"", &", "")
                            line = line.Replace(");", " = Convert.ToInt32(Console.ReadLine());")
                        End If

                        line = line.Replace("printf", "Console.Write")
                        line = line.Replace("%d", "{0}")
                    End If
                Loop While line IsNot Nothing

            Catch e As Exception
                Console.WriteLine("Error, " & e.Message)
            End Try
        End If
    End Sub
End Class