?
Solved

C++ to VB.NET Example of hand conversion anywhere?

Posted on 2017-09-20
4
Medium Priority
?
83 Views
Last Modified: 2017-09-21
Starting with small C++ console program with one function call is there any examples of how to convert that to an equivalent VB.NET program. I don't want to use the VISUALBASIC namespace.

Thanks
0
Comment
Question by:bob_mechler
  • 3
4 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 42303230
Here is the basic structure with some code comments...
I will start with C# as think that is a good logical transition then will show conversion from C# to VB.NET.  I find it better to write new code in the target language to make sure I am using best pattern there but tools like http://converter.telerik.com/ come in handy if you need to see what a potential equivalent syntax is.

// like header imports
using System.Text;

/// <summary>
/// Usually console applications use Program as class but can be whatever as long as it has static void Main.
/// In your project properties, you then tell it what class to use as the main entry point to the application.
/// </summary>
class Program 
{
    /// <summary>
    /// Executes main thread of the program.
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
        object o = new object();
        int x = 1;
        
        if (args.Length > 0)
        {
            // get your arguments for the function call here
        }

        Program p = new Program();
        p.doSomething(ref o, x);
    }

    /// <summary>
    /// A non-static member function example
    /// </summary>
    /// <param name="o">reference param like &param in C++</param>
    /// <param name="x">integer value param</param>
    /// <returns></returns>
    string doSomething(ref object o, int x)
    {
        return "Hello world!";
    }
}

Open in new window


Here is what the matching VB.NET looks like:
' like header imports
Imports System.Text

''' <summary>
''' Usually console applications use Program as class but can be whatever as long as it has static void Main.
''' In your project properties, you then tell it what class to use as the main entry point to the application.
''' </summary>
Class Program
	''' <summary>
	''' Executes main thread of the program.
	''' </summary>
	''' <param name="args"></param>
	Private Shared Sub Main(args As String())
		Dim o As New Object()
		Dim x As Integer = 1

				' get your arguments for the function call here
		If args.Length > 0 Then
		End If

		Dim p As New Program()
		p.doSomething(o, x)
	End Sub

	''' <summary>
	''' A non-static member function example
	''' </summary>
	''' <param name="o">reference param like &param in C++</param>
	''' <param name="x">integer value param</param>
	''' <returns></returns>
	Private Function doSomething(ByRef o As Object, x As Integer) As String
		Return "Hello world!"
	End Function
End Class

Open in new window


Note: you also can explicitly set x as ByVal x As Integer and can make a parameter option like Optional ByVal x As Integer = 1.  I hope that is enough to translate your C++ code.

Kevin
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 42303237
By the way, the other reason I think as a former Java or C++ programmer that starting in C# is a good transition is that in many cases .NET is .NET. You will find that some code changes are as simple as having semi-colon or not.

C#
Program p = new Program();
string s = p.doSomething(o, x);
// write output line to console
Console.WriteLine(s);
// waits for next input (press any key)
Console.Read();

Open in new window


VB
Dim p As New Program()
Dim s As String = p.doSomething(o, x)
' write output line to console
Console.WriteLine(s)
' waits for next input (press any key)
Console.Read()

Open in new window


Kevin
0
 

Author Comment

by:bob_mechler
ID: 42303310
You have nailed where I need to start from.  Your example is easy to follow.

C++ code however just isn't that intuitive without an existing program to start with.

Thanks,
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 42303327
You are most welcome and happy coding!
Respectfully yours, Kevin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question