Problems when running project with two main mehods in VisualStudio - c#

In my project I have two different classes with main methods to create different objects of different classes. But when I want to run one of the classes with a main method, it starts and run the other object that is created in the other main method!?
Perhaps a little bit confusing, but I hope the reader can understand what I'm trying to explain. I have googled and found some info about do some changes in the properties for the classes, but I couldn't find out how. Preciate some help! Thanks!
EDIT:
Perhaps I was unclear. I have main class called TaxProgram and it contain the main method that creats an object of the class Product.
Then I have another class called StartBox and it also contain a main method but it creates an object of the class Box.
The problem is when I press F5 to Debug, I thought that the merked tab with the class name StartBox should start and create an object of the box class, but now I knew that VisualStudio doesn't work that way. I have tried to change the properties to be able to start the StartBox insetad, but without any luck.
The image gives a view of my classes

You need to open project properties + Application Tab + set Startup object.

If you mean a standard Main signature :
static void Main(string[] args)
Then, you should only have one function like this in your program. Rename the other one.
IIRC there is an option in the project properties to set which class's Main is called at startup, but I can't confirm this until I get to work.

Click with the right button in your solution, then select "Set StartUp Projects..."

In the project properties, in the Application tab, is an option for Startup object. You can select the class that contains your intended entry point, as long as it contains a valid Main method.

Related

Referencing a solution inside another solution C#

I am new at C#. I am creating a project that contains two solutions and I cannot reference one from the other. I need to use the classes that are in the AssestManagment to ManejoDeActivos. Is it possible to reference based on the screenshot below?
Reference other Solution
Technically, the project "ManejoDeActivos" is to have only the front end of the applicaction. It has the GUI. The AssestManagment project connects to a database, which will allow to add information to the DB. Inside the "ManejoDeActivos" we have a GUI that allow to add a new asset through a form. I having problems calling the Assest class that will allow to create a new object of an asset. I tried to add AssestManagment to ManejoDeActivos, however when I go to references I do not see the AssestManagment project and if I tried to browse it in my computer the project solution is not showing up
You could take the code needed from one of the solutions/projects, put it into a class library project, and compile it into a DLL. You can then reference that DLL inside of another project and access the code.
I would suggest reading How To Ask to learn how to ask a question properly.
Simplest approach will be to reference all the projects of the old solution on the new solution.
It might be cumbersome as the number of projects increase but would accomplish what I’m thinking you’re trying to accomplish.

C# beginner program

I'm using Visual Studio and when I try to compile the code below I get two errors;
CS0103 The name 'Console' does not exist in the current context
CS0017 C# Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
How do I fix this?
using System;
namespace App2
{
class Class1
{
static void Main()
{
Console.Writeline("Hello");
}
}
}
It looks like Class1 is not the only class that has a static void Main() method defined. Usually, when you create a console application, there's a class called "Program" that already contains a method Main. There should be no need to add another class.
Just modify the existing Main method. This should solve the second error.
The Main method is a bit like the Highlander of methods - there can only be one.
As for the first error: You do need to target the Windows platform to be able to use Console, so you need to create a Console application or a Windows Forms/WPF application or the like.
The first error is caused by a typo. To correct it, change Writeline to WriteLine (with a capital letter L).
The second error is caused by the fact that you did not specify clearly which entry point the program should use. To fix this, follow these steps:
Right-click on your project in Solution Explorer, and open the "Properties" menu. You will see the similar page and all that you should do is explicitly select entry-point.
As Thorsten Dittmar said, ensure you don't have a duplicate static void Main method,
CS0103 The name 'Console' does not exist in the current context
Ensure you did not create an Universal Windows Platform (UWP) project/Android. You can't call Console on it.
As many have suspected I fixed the error by going into the installer, modified and checked off the .NET Desktop Development.
It works now.

How to access a class which is in a folder c# (wpf)?

I'm building a WPF application. I'm almost finished with one part of it, and now I want to clean up the code a bit. English is not my native language (so sorry if this gets marked as a duplicate) and I couldn't find anything about this, so I hope someone can help me out.
If I have public voids in a class called 'class.cs' and this class is in a folder called 'classes', how do I access the class so I can use its functions?
At the top of the file where you want to use the class include a using statement.
using [namespace];
Then you will be able to instantiate the class
Class testClass = new Class();
testClass.Method();
If you aren't sure what the namespace for your Class class is, then you can write the line to instantiate the class and then use the intellisense (assuming you are using a development ide like Visual Studio) to automatically include the reference. Alternatively, you can look at the top of your class.cs file to see what namespace has been declared.
check what namespace it's in , then what the class name is and call it as
"Namespace.class"
in case that you can't see it still , check if the class is public and the class.cs properties have the build property set to compile
I'm not sure from where You are trying to access this class. But I think the easiest way of doing it probably will be just typing your class name in place where You are trying to use it and press 'ctrl+.' which should show You context menu with options to add using statement for this class.
If You are trying to access this class from other project in solution then above way will only work with VS2017(because it will also add missing project reference) otherwise You need to add reference for this project and then add missing using statement for your class(in your case 'using ProjectName.classes').
The folder you are talking about is called a namespace. You first need to import the namespace and then you can use your class.
*Normally the IDE will prompt you.

No main() in c# project

The issue I'm having is trying to find the run order for a number of classes in a project. There is no explicit main and I have looked in obj/debug and found nothing which might have been generated at compile time. Have you any ideas where I might find something. Also I cannot find any xaml files either. It's a console function.
This is a quick way to find the entry point (if there is one):
Without any specific source file open, press F11 to start the debugger at the first line. This should make your entry point obvious.
Note that if this is a Console Application like you said, there must be a static main() method.
Here is the documentation for Main():
The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.
If you check the project properties, under "Application", there is a "Startup object" listed. This should tell you the entry point to the application, which is the type which contains the Main method that is actually being used.
If it's a console application then you will usually find the Main method in the auto-generated Program.cs file.
If there is no main method, the project is a class library.
Other than #Gray's answer, you could also Ctrl+F for Main.

Relative URI problems - designing WPF window in VS / Blend, but opening the window using a different c# environment

Please excuse the newbie question - my background is Unix and I am very green when it comes to WPF...
//Context//
I need to design a single page WPF-based UI, using Visual Studio, or Blend.
Additionally, I need this to be portable outside the microsoft generated solution... this is because I need to be able to define the business logic within a different c# environment, that is exposed by software called 'NinjaTrader' (for those who may use it).
I am restricted to .NET 3.5
//Additional info//
Right now, I am trying to get to proof-of-concept as quickly as possible. I have successfully implemented the UI using winforms. by simply designing the form within VS, and then copying the auto-generated designer code into my own abstract class definition (say 'myForm') within NinjaTrader. I then have to inherit myForm into a new class definition within a 'standard' NinjaTrader.Indicator class (this is necessary to be able to instantiate the myForm2 class), and by passing the outer class to the constructor of myForm2, and overriding the methods I need for the business logic, I can display a UI upon 'OnStartUp' of the NinjaTrader.Indicator class, and implement the behaviour that I need.
//My problem//
WPF is proving more difficult so far. I have tried copying the class definitions from 'WPFWindow.xaml.cs', and also what is necessary from 'WPFWindow.g.cs' into a single .cs file (due to limitations within NinjaTrader, I must use a single file for this), and then instantiating this class from another NinjaTrader.Indicator class.
Everything is in the same namespace, though I am repeatedly stumped when it comes to the relative Uri and it seems I get the following message, no matter what I do:
Cannot locate resource 'wpfwindow/wpfwindow.xaml'
Now - I am aware that the relative Uri will not be the same, when running from within a different application, and I have even tried copying the WPFWindow.xaml file to a new folder (within what seems to be the current working directory, and without any spaces) and calculating the relative :
string cpath = Directory.GetCurrentDirectory();
string installPath = #"C:\Program Files (x86)\NinjaTrader 7\bin64\WPFWindow\WPFWindow.xaml";
Uri cwUri = new Uri(#cpath);
Uri instUri = new Uri(#installPath);
string relPath = cwUri.MakeRelativeUri(instUri).ToString();
System.Uri resourceLocater = new System.Uri(relPath, System.UriKind.Relative);
System.Windows.Application.LoadComponent(this, resourceLocater);
However, I am still getting the same error.
I would be extremely grateful if anyone has any advice / suggestions as to how I might try to proceed.
Is there another way that I can load an XAML file (that is external to the current application / project), without using a relative Uri in order to display a single UI window? Any advice on what the bare minimum (less is definitely more reliable in this case) I would need to implement in order to do this and define the business logic, would be extremely helpful...
Thanks in advance for any advice
J
EDIT: (& Reponse to Brian S):
Thanks for the response, and also for the reccomendation on material. It is entirely possible that I have misunderstood the necessary architecture involved with WPF...
Therefore, please let me know it that is the case... My understanding is as follows:
The graphical composition, content, layout, etc is all specified within the XAML file. Behaviour, interaction, event handling etc is specified in the code-behind.
Once the XAML markup has been written, I had made the assumption that, in order to instantiate the window I had designed, all I needed to do was to load / parse it into a tree of objects within - lets say - a class that inherits System.Windows.Window, and is named as the same class as in x:class="myClass", within the XAML file.
Upon re-reading what I wrote previously, I realise that I was not all that clear about the situation. I can use multiple files for the solution, without a problem, the issue arises as follows:
With classes that are partial, across 2 files - lets say file1.cs and file2.cs, class methods will only be recognized within the same file that they are defined (unless I actually instantiate the class - after which I can call it's methods). So - for example - I could not specify InitializeComponent() in file1.cs, and then define a constructor in file2 that refers to InitializeComponent()... I hope that makes sense...
What I can do, which seems to work, is to avoid defining methods outside of the file that I need to reference them in. And thus, avoiding the use of partial classes as far as possible.
I can define a class in one file (say class1.cs), then instantiate it from another file (say class2.cs) no problem, by using:
private class1 mynewclass1 = new class1(args)
I can also use:
'System.Windows.Application.LoadComponent(args);'
Though I seem to be running into issues with the relative addressing, as no matter what I try it does not seem to find the .xaml file..
Is there a way that I can try to load the xaml file without using relative addressing? I have been looking at XamlReader, though I'm not sure it can do what I need....
Thanks again,
J
If I follow, you're trying to treat the WpfWindow.xaml file as a loose resource file located in the same directory. This is not how WPF works - the WpfWindow.xaml file is a partial class with the codebehind. This is possible with XAML Resource Dictionaries, but I don't believe it is possible with the WpfWindow.xaml file because they need to be compiled together.
If you want a single-file solution, it is a lot more work, but everything you do in XAML, you can do through code. So you can create a window, add controls, position them and connect behaviors all through the codebehind. Obviously, this is not the ideal approach, but it sounds like you've got some pretty significant limitations (I'm not familiar with NinjaTrader).
The MSDN Documentation will provide help on the code necessary to manually construct and layout a WPF window, or if you are looking for another resource, Applications = Code + Markup, by Charles Petzold goes through doing anything you need to do with WPF from codebehind.

Categories

Resources