Starting a console?

Hi,

I've got a console-based project, which contains a class like so:

using System;
using System.Collections.Generic;
using System.Text;
namespace vm
{
public class clsVirtualMachine {
public void Execute(string Code)
{
Console.WriteLine(Code);
}
}
}

Part of the same solution I also have a testbed project, which consists of a form, a textbox and a button. Then the button is clicked the following happens:

using vm;
(....)
private
void cmdRun_Click(object sender, EventArgs e)
{

// New instance of code runner
clsVirtualMachine TheVM = new clsVirtualMachine();
MessageBox.Show(txtCode.Text);
TheVM.Execute(txtCode.Text);

}

When the button is clicked a msgbox pops up with what's in the text box, but no console pops up Is this because the clsVirtualMachine is being incorporated into the TestBed windows app Is there any way to get the VirtualMachine class to be completely independant



Answer this question

Starting a console?

  • _Prabhu Sadasivam

    Hi David,

    Yes, since you are referencing your console application in your test bed app you are not going to see the console window. In your case, this is OK because you are testing the execution of your VirtualMachine class and analyzing the returned text - this is an example of integration testing (testing multiple parts at once to observe your desired result).

    You might want to consider using NUnit (link) to create a set of unit tests which will test your discrete pieces of functionality. This way, you can have a suite of tests that you can run in an automated way and even incorporate into an automated build process.

    Cheers,
    -Paul


  • Rep

    If you want a console window in your WinForms app, you can try calling "AllocConsole" Win32 API.

    [DllImport("kernel32.dll")]
    static extern bool AllocConsole();

    This will attach a console window to your app - and all the console outputs will go to this.

    -vj

  • Steve Huckett

    Thanks very much Vijay - that worked fine.
  • Scott Stalter 1

    Thanks very much - I will try that, looks just what I need.
  • Jose Rocco

    Hi Paul

    As I thought :( Unfortunately my VM class is console based.

    I made a simple windows forms app so I could type some code in then send it to the VM for that to do its stuff. When I've finished coding the VM it will accept its input from a disk file so the current setup is only for dev purposes - but it would be much easier if I didn't have to keep a copy of notepad open to type in code for it to test!

    I'll have a look at NUnit, thanks alot for the link.

    David

  • Starting a console?