Running multiple C# classes in one project in Visual Studio - visual-studio

I have a C# project in Visual Studio which has several classes under it. I am trying to run each class separately but when ever I hit the start or debug buttons,only one of the classes (the first one I created) runs.I tried right-clicking the other classes but they don't have the run option. I am using Visual Studio Express 2013
Update (To clarify the question)
Under the Solution C-SharpTutorial i have two .cs files (ArrayTest.cs and Program.cs). What am asking is if it's possible to run these files separately. Right now, I am only able to run the Program.cs file which is the first one i created.

I assume that by classes you actually mean projects. Because one Solution contains one or more projects, and projects can be run.
The answer to that is here: http://msdn.microsoft.com/en-us/library/a1awth7y.aspx
To set a single startup project
In Solution Explorer, select the desired startup project within your solution.
On the Project menu, choose Set as StartUp Project.
Otherwise, please clarify your question.
Okay, i assume you have a console application. While you cannot "run classes", you can set a startup method: https://stackoverflow.com/a/49585943/1974021
A class is a set of methods. To execute (non-static) methods, a class must be instantiated. But the runtime does not know how to call an arbitrary constructor. Therefore, the program execution starts in a static method called "Main".
If multiple classes contain a suitable Main() method, you should be able to select the desired one according to the link above.

It sounds like you mean Projects, not Classes. To change the project that is executed when you start debug mode, you can right click on the project and select "Set Active Project".
If you set breakpoints in any of the other projects that are referenced, they will still be hit and you will be able to debug using Visual Studio.
If you need to run multiple projects, you will need to run these manually from the bin\Debug folder, and then use the "Attach To Process" feature in Visual Studio to attach the debugger to those processes so that you can debug them.
Update
No, you cannot 'run' two different classes separately. A console application has only one entry point. However, if you're learning C# and testing code, you can use a switch statement.
For example:
void main()
{
Console.Write("Choose Option (1/2):");
var key = Console.ReadKey().KeyChar;
switch (key)
{
case "1":
{
var arrayTest = new ArrayTest();
arrayTest.Run();
break;
}
case "2":
{
var anotherTest = new AnotherTest();
anotherTest.Run();
break;
}
}
}
This way, when the app runs it will prompt you for a key, and you can press 1 or 2 to execute whatever you want.
With that said, for writing basic test code, I find using LINQPad significantly more productive as it bypasses the need of writing all of the boilerplate console application code.

There are 2 Reasons
Every Class have their own Main() Method
2.C# has case sensitive so method name like Main() not like main() it won't show in project properties window
---->Kept as startup project under project properties-->> Application--->
select a project which u want run

There are two types of reasons for this:
Every class has their own Main() method.
C# is case-sensitive, so method names like Main() are not like main() and won't show in the project properties window.
Solution: Keep as startup project under project properties --> Application --> select a project which you want run.

Related

With Xcode UI tests, is it possible to test application in external project/repository?

I am searching for a way to separate the UI tests from the original repository where the main app stays. Is it possible to have the UI test code in another repository, "reference" the main app in some way, and test it?
Starting in Xcode 9, this is possible as long as you're willing to give up a few features. After creating a UI test bundle, you can go to the target settings to specify that it has no target application. (You may also be able to create a UI test bundle without specifying one to begin with.) After you've removed the target application, rather than using the default initializer for XCUIApplication, you can use the initializer that accepts a bundle identifier.
Your tests will end up looking something like the following:
var app: XCUIApplication {
return XCUIApplication(bundleIdentifier: "com.yourdomain.product")
}
func setup() {
app.launch()
}
func test() {
app.navigationBar.buttons["Add"].tap()
}
If you go this route, you will lose the ability to use a few features. Namely:
You won't be able to use Xcode's built in test recorder. The record button is disabled unless you have a target application configured. That being said, you could create an empty app in your project and just switch to the application you'd like to record instead. (That being said, I haven't found Xcode's record feature to be very useful, even for single project apps.)
Xcode won't automatically install your application for you. That usually isn't an issue since is located in a different repository (and sometimes isn't even an Xcode project), so you'll just need to install it before starting your tests. The iOS simulator allows you to drag and drop a .app file, so installing the app is easy enough.

visual studio extension (VSPackage) add items to Test Explorer context menu

I am writing an extension to Visual studio 2012 using VSPackage. I need to add a context menu entry to Test Explorer and on click of this menu item, I need to get the selected unit test(s). I tried to add an item using
((CommandBars)DTE.CommandBars)["Test Window Context Menu"].Controls.Add(Type: MsoControlType.msoControlButton);
and adding an event handler by subscribing to the event
DTE.Events.CommandBarEvents[command].Click
I succeeded in adding an item to Context menu but the Click event handler never gets fired. MSDN said, I needed to set the OnAction property of the command to a valid string value for the Click event handler to get fired. It didn't work either.
Then, I figured out I needed to add a command through the VSCT file in a VSPackage. However, I am not able to find the Test Window Context menu so that I can attach the command to it. Also, I need to get all the unit tests (TestCase objects) listed in the Test Explorer.
Any help is greatly appreciated!
Usually these are the files I look for Visual Studio shell GUIDs or command, context menu, group, etc IDs:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VSSDK\VisualStudioIntegration\Common\Inc\stdidcmd.h
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VSSDK\VisualStudioIntegration\Common\Inc\vsshlids.h
Actually they are included in the top of your newly created .vsct file (<Extern href="vsshlids.h" />). I guess you've already checked them. I did a quick search, but what I found for "Test" is just a test ribbon and a test dialog. Probably now that you're looking for. It might be still useful for someone finding this post.
You might also want try it brute force style:
Search your Program Files (x86)\Visual Studio [VERSION] for regexp: ^#define.*TEST.*$
This shall give you the defines containing TEST.
Also you might want to consider asking Microsoft directly.
I wrote some exploratory code to loop over commands in that context menu. I also played around with registering a priority command target and seeing what group GUID and command ID I got. The GUID for that context menu appears to be {1e198c22-5980-4e7e-92f3-f73168d1fb63}. You can probably use that to add a command via the .vsct file without using the DTE.CommandBars to add it dynamically.
Here's my experiment code which lists the GUID and command ID of the commands currently in that context menu, in case it helps anyone.
var bars = ((Microsoft.VisualStudio.CommandBars.CommandBars)DTE.CommandBars);
var teContextMenu = bars["Test Window Context Menu"];
var ctls = teContextMenu.Controls;
foreach (var ctl in ctls)
{
var cmdCtl = ctl as Microsoft.VisualStudio.CommandBars.CommandBarControl;
string guid; int id;
DTE.Commands.CommandInfo(ctl, out guid, out id);
Debug.WriteLine($"{cmdCtl?.accName} {guid} {id}");
}
This article on command routing was helpful to me:
https://learn.microsoft.com/en-us/visualstudio/extensibility/internals/command-routing-algorithm
My experimental priority command target, where I set a breakpoint to see what GUID and command IDs were sent, is registered as follows. The TestCommandInterceptor class is a bare-bones implementation of IOleCommandTarget.
var cmdService = GetService(typeof(SVsRegisterPriorityCommandTarget)) as IVsRegisterPriorityCommandTarget;
var target = new TestCommandInterceptor();
cmdService.RegisterPriorityCommandTarget(0, target, out _testCmdInterceptorRegistrationCookie);
I would still like to know the answer to the second part of this question about how to determine the selected tests.

How Can I Add an "ATL Simple Object" to Old ATL DLL Project Upgraded to VS 2010?

We have a DLL project which has existed for a long time (maybe as far back as Visual Studio 6) which has been updated for each new version of VS. The project contains a number COM classes implemented using ATL.
After upgrade to VS 2010, the project still builds fine. However, if I try to right-click the project and choose Add -> Class... -> ATL Simple Object, I get an error box that says this:
ATL classes can only be added to MFC EXE and MFC Regular DLL projects or projects with full ATL support.
This worked in VS 2008.
When I look at the project properties, Use of MFC was set to Use Standard Windows Libraries and Use of ATL was set to Not Using ATL. I changed these to Use MFC in a Shared DLL and Dynamic Link to ATL respectively, but still get the same error.
I know how to add new ATL objects without using the wizard, and I could try to recreate the project from scratch using VS 2010 to make it happy. But does anyone know of any easy way to get VS to allow me to use the ATL Simple Object wizard with a project that it doesn't recognize as a project "with full ATL support"?
Check this thread out.
It seems that adding this fragment info your ATL C++ code make it work. You don't need to actually build the project, just remove this stuff away after you are done with the wizard (provided that solution works for you).
// Added fake code begins here
class CAppModule :
public CComModule
{
};
// Added fake code ends here, below is regular ATL project stuff
CAppModule _Module;
This is where it all comes from, in $(VisualStudio)\VC\VCWizards\1033\common.js:
/******************************************************************************
Description: Returns a boolean indicating whether project is ATL-based.
oProj: Project object
******************************************************************************/
function IsATLProject(oProj)
{
try
{
var oCM = oProj.CodeModel;
oCM.Synchronize();
// look for global variable derived from CAtlModuleT
var oVariables = oCM.Variables;
for (var nCntr = 1; nCntr <= oVariables.Count; nCntr++)
{
var oVariable = oVariables(nCntr);
var strTypeString = oVariable.TypeString;
if (strTypeString == "ATL::CComModule" || strTypeString == "ATL::CAutoThreadModule")
{
return true;
}
Same problem here, but the project source already had CComModule _Module;
Fixed it, based on the IsATLProject script shown above, by changing it to
**ATL::**CComModule _Module;

Debugging UDK using nFringe in Visual Studio 2005

This is a pretty niche question, so I am not expecting a huge response...
Basically, I am learning how to use the UDK by following some tutorials, namely this one:
http://forums.epicgames.com/showthread.php?p=27043379#post27043379
So far everything is going pretty well. The only real hangup I've had is getting everything to work in Visual Studio 2005 using this nFringe plugin. For a long time, couldn't get them to work at all. I've gotten into two or three chapters of the tutorial, and I've managed to use Visual Studio to edit the code, but I can't build the scripts within VS; I have to go to UDK Frontend to do that. And worse still, I can only really use Log commands in the unrealscripts to debug anything.
So my question is this: is it even possible to configure these tools in a way that I can put breakpoints in VS and have them be caught when I test the game? I feel as though I don't have something setup correctly.
Yes it is possible. Here are some info which might be useful to you.
First, both your .sln and your .ucproj files must be located in Development/src. Then, under visual studio, right-click your project (.ucproj file in the solution explorer) and open its properties.
You must set, under the General tab:
Target Game: UnrealEngine 3 Mod
UCC Path: ....\Binaries\Win32\UDK.exe
Reference Source Path: ..\Src
Under the Build tab:
check "Build debug scripts"
Under the Debug tab:
Start Game Executable: ....\Binaries\Win32\UDK.exe
Load map at startup: the name of your startup map, without path nor extension
Start with the specified game type: put your GameInfo class used for your mod, ie. MyMod.MyGameInfo
Disable startup movies can be checked to gain time at launch
Enable unpublished mods must be checked.
In your command line, the parameter -vadebug specifies that the breakpoints will be enabled.
After that, you must be able to build your script from Visual, and launch your game by pressing F5.
Breakpoints should work but you can't put them on a variable declaration, you have to put them on a function call, an assignment or a condition statement.
Hope this will help.
I havnt tried using breakpoints yet but I know its possable to build with nfringe and visual studio . You need to add a line to the
udk game / config / udk engine .ini
search for
editpackages
exactly like that , then youll see a block like this
EditPackagesInPath=....\Development\Src
EditPackages=Core
EditPackages=Engine
EditPackages=GFxUI
EditPackages=GameFramework
EditPackages=UnrealEd
EditPackages=GFxUIEditor
EditPackages=IpDrv
EditPackages=OnlineSubsystemPC
EditPackages=OnlineSubsystemGameSpy
EditPackages=OnlineSubsystemLive
EditPackages=OnlineSubsystemSteamworks
then add your own line pointing to a folder named what ever you want but make sure it has a folder in it named Classes and it has the uc files you wnat to compile in it
ModEditPackages=MyTestProject
if you used that line then you are tellign udk you have a folder named
MyTestProject
located in your development/src folder and you want it to compile everything in there

Visual Studio Build Event Macros - Solution Configuration Name

In my Post-build event I call a batch file and pass it the current build config.
C:\Dev\Project\Build\build.bat /$(Configuration)
This passes the Project configuration name to the build script.
Is there anyway to pass the current Solution configuration name?
I created a VS2010 extension for this, it lets you use $(SolutionConfiguration) and $(SolutionPlaform). $(BuildMacro) build macros. You can download the source and build it yourself from here.
Showing some code, it's just about registering to UpdateSolution_Begin method
of IVsUpdateSolutionEvents VS and setting some Environment.SetEnvironmentVariable() there.
_IVsSolutionBuildManager = (IVsSolutionBuildManager)GetService<SVsSolutionBuildManager>();
_UpdateSolutionEvents = new UpdateSolutionEvents(); // IVsUpdateSolutionEvents
int hr;
uint pdwCookie;
hr = _IVsSolutionBuildManager.AdviseUpdateSolutionEvents(_UpdateSolutionEvents, out pdwCookie);
Not directly. When you use Edit/Macros on a property field, the only configuration name listed is the one for the project that you already have.
You can, however, define your own macro. For each solution configuration, create a new property sheet, use the "User Macros" tab to define a macro whose name is "SolutionConfiguration" and whose value is the name of the configuration, then add this property sheet to the appropriate project configuration of every project in the solution.
If there's a better way I'd love to learn of it.
There is property SolutionConfigurationContents witch is created by Msbulid during soluton file processing it contains solution configuration in it. When building from VS it will contains project (not solution) configuration.

Resources