➕ Adding Visual Basic Classes to your C# MG Projects

I was reading a recent post:

And it reminded me to look into VB as a scripting language or whatnot…

So… I did a shallow dig and put the following together:

Note: I used the UWP variants to each project, so for the VB Class project I used the UWP listing in VS and MG too.

Take a look here:

and following that a little, I pieced together this post…

Create a new MG UWP project
Create a UWP VB Class Library project call it ClassLibrary1VBTest or whatever and build! Must build! See F5 reference below
Add a reference to the VB project in the MG project, click add reference on the right in solution explorer [EDIT after right clicking References] and click browse and look for the DLL of ClassLibrary1VBTest in the output directory [Debug or Release, either is fine for testing]

So far so good…

In your VB project replace all code with:

Public Class Class1

    Public Function VisualBasicCode()
        Debug.WriteLine("VB Method Called")
        Dim WorldSize As Double = 3.141

        Return WorldSize
    End Function

End Class

…. and build F5 err I mean ALT+Shift+B

In your MG project

Add

using ClassLibrary1VBTest;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;

to the top [I think adding a reference adds the VB link automatically, did not notice lol

Go to your update method and add:

    Class1 vbClass = new Class1();

    Debug.WriteLine(vbClass.VisualBasicCode().ToString());

and viola, VB in your C# MG projects… Look at your Output panel.

Notice how we are grabbing the Double [Float] value in WorldSize and pushing it as a string to the Output console.

Let’s up the game…

Change the VB code to:

Public Class Class1

    Public Function VisualBasicCode()
        Debug.WriteLine("VB Method Called")
        Dim WorldSize As Double = 3.141

        Return WorldSize
    End Function

    Public Function Countdown(ByVal UserInput As Integer) As Integer

        UserInput = UserInput - 1

        If UserInput < 0 Then
            Return 0
        End If

        Return UserInput
    End Function

End Class

Change Update to:

protected override void Update(GameTime gameTime)
{
    // TODO: Add your update logic here

    Class1 vbClass = new Class1();

    Debug.WriteLine(vbClass.VisualBasicCode().ToString());
    Debug.WriteLine(vbClass.Countdown(Counting).ToString());
    Counting = vbClass.Countdown(Counting);
    // Notice how we use the code from the VB code to manipulate our C# project code.

    if (Counting == 0)
    {
        Exit();
    }


    if (Keyboard.GetState().IsKeyDown(Keys.D))
    {
        Counting = 1000;
    }

    base.Update(gameTime);
}

Just after:

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    int Counting = 100;

Add that Int Struct

Now, we have already created a mini game here, you have 100 units of cycle time to press D on your keyboard before the app exits :stuck_out_tongue: have fun!

And in debug you should be seeing this now:

I noticed something curious while coding this… I changed the WorldSize from a string to a double but left the “” enclosure around 3.141 and it still worked……. weird…

Anyway, I hope this helps anyone wondering how to combine VB and C# with MG.

Remember if you want to read values instead of throwing them at the Console, just do this:

        Counting = vbClass.Countdown(Counting);

To grab the return value generated by the VB Function.

Or simply

SomeString = vbClass.VisualBasicCode()

To grab that WorldSize, but remember to either add .ToString(); if you want it as a string or change the VB code to:

    Dim WorldSize As String = "3.141"

To grab WorldSize using the previous code without .ToString(); Obviously the code being grabbed will be in the Return object from whichever function you are pulling from.

Sometimes coding simple things in VB is more refreshing than trying to figure all the elements with C#, VB is just cleaner sometimes.

I hope people with more experience can actually provide more ways one might use VB in conjunction with a project to share more ideas.

I wonder if we can pass model data to VB as well?

Hoping this is clear enough for beginners, do ask if not.

Valentine signing off.

Happy Coding!

Sometimes coding simple things in VB is more refreshing than trying
to figure all the elements with C#, VB is just cleaner sometimes.

I hope people with more experience can actually provide more ways one
might use VB in conjunction with a project to share more ideas.

I do everything in VB, and sometimes incorporate a little C# code in my projects if I don’t feel like translating it. I find I memorize and read code faster in VB.

I make class libraries and call them just the way you described above.

I wonder if we can pass model data to VB as well?

Sure. Its just data.

2 Likes

Thank you! :pray:

I thought that but I was wondering how extensive one might do it as passing just small bits of data was all I could muster at the time.

Thanks again.