[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2017-09-20
4
Medium Priority
?
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

656 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