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

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
LVL 1
bob_mechlerProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
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
Kevin CrossChief Technology OfficerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bob_mechlerProgrammerAuthor Commented:
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
Kevin CrossChief Technology OfficerCommented:
You are most welcome and happy coding!
Respectfully yours, Kevin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.