Stefano
Stefano

Reputation: 11

Run vb.net code directly from cmd or Visual Studio

I'm starting to code with vb.net and i need to run the code directly, like it happens with java: in the cmd i can run the class files. Is there any similar possibility with vb.net, preferably directly from the visual studio hub?

Thanks!

Upvotes: 0

Views: 2931

Answers (2)

Sean Phillips
Sean Phillips

Reputation: 11

if you are looking for more then the Immediate Window. Look into Microsoft CodeDom. I have used it to compile C#, VB.net, C++, and Visual J#.

https://msdn.microsoft.com/en-us/library/y2k85ax6(v=vs.110).aspx

Upvotes: 0

Visual Vincent
Visual Vincent

Reputation: 18330

You can use the Immediate Window for that. It offers many different ways to interact with your code. To use it, start your application in debug mode from Visual Studio and press CTRL + ALT + I.

To execute a shared method you can type in the Immediate Window:

className.methodName()

(example)
MainFunctions.DoStuff()
DoMoreStuff()
  • Where className is optional if you're currently already inside the class (for example if you've hit a breakpoint in it).

If you want to execute an instance (non-shared) method you can either use the method above (without className, but you must currently be inside the class by hitting a breakpoint, for example), or you create a new instance of the class and execute the method:

Public Class MiscFunctions
    Public Sub PrintHelloWorld()
        Debug.WriteLine("Hello World!")
    End Sub
End Class


(Immediate Window)
New MiscFunctions().PrintHelloWorld()
Hello World!

Dim m As New MiscFunctions
m.PrintHelloWorld()
Hello World!

You can also print the value of a variable or the return value of a function by typing:

? variableOrFunctionNameHere

(example)
? ImageCount
4

The same rules for executing methods applies to evaluating functions too:

Public Class MiscFunctions
    Public Shared Function IsEven(ByVal Num As Integer) As Boolean
        Return Num Mod 2 = 0
    End Function

    Public Function Sum(ByVal a As Integer, ByVal b As Integer) As Integer 'Non-shared method.
        Return a + b
    End Function
End Class


(Immediate Window)
? MiscFunctions.IsEven(3)
False
? MiscFunctions.IsEven(8)
True
? New MiscFunctions().Sum(3, 9)
12

You can also dynamically evaluate expressions:

? ImageCount + 1 = 5 'Here, ImageCount is 4
True
? 2 * 4
8

Upvotes: 7

Related Questions