I am making an AI in WPF and i want to use a separate C# file for the AI. When I type start(); in one file I want it to find it from the other file and use it, and when I type myImage.visibilty = visibility.hidden so that it will hide the image from the xaml from the original c# file.
Here is my second C# file
using System.Windows;
namespace Hexapawn
{
public class AI2 : MainWindow
{
public AI2()
{
InitializeComponent();
//somecode
}
public void start()
{
//somecode
}
}
}
I have tried
using myproject.Myfile;,
using myFile;
but it isn't able to use methods from the other file or change the xaml.
How about a partial class. Your main window class is already a partial class. You can declare 1 more partial class of the same class in a separate file and use all the methods from it in your first class.
However, your AI2 class seems more like a Model to me which will deal with non-UI business logic. You can implement the MVVM pattern which is more suited for WPF-based applications and make your new AI2 class as the Model of MVVM.
Related
I have created a console application that creates CSV data when it is run. I want to move this app to another project as a class, and then call that class when a button is pressed. How would I do this?
The class I will move looks like this
namespace ConsoleApp3
{
class Program
{
static void Main()
//content
}
public class MyClass
//more content
}
and the button would look like this
//form data
<a class="button">Download CSV</a>
My controller has simple actions...
public IActionResult Index(){
return View();
}
public IActionResult DownloadCSV(){
//do I need something here?
return View();
}
A good option is to create this class as a library
-like this: Build a C# Standard library
Once your new library is created you need to Reference it from the other project where you want to use this class
Creating a class as a library will let you use it in any other project/application in the future.
So I would strongly recommend to keep your repetitive classes as libraries, such as CSVManager class or ConnectionManager class and any other class you might use more than once.
I'm currently going through this tutorial:
https://tomassetti.me/getting-started-with-antlr-in-csharp/#note1
But I've issues at the point where I want to create the SpeakVisitor class, in the tutorial the class is inheriting from a class called SpeakBaseVisitor which was generated by ANTLR itself.
So I looked through my whole solution and I couldn't find this created class...
All I got are two pretty much empty class files called SpeakLexer and SpeakParser which got created after I selected Combined Grammar in the "Add → New Item" dialog.
SpeakLexer.cs
namespace _006_ANTLR
{
partial class SpeakLexer
{
}
}
SpeakParser.cs
namespace _006_ANTLR
{
partial class SpeakParser
{
}
}
Do you got any idea why there is no SpeakBaseVisitor automatically generated in my case?
Greetings! :-)
This question already has answers here:
Using a class file/reference it?
(4 answers)
Closed 6 years ago.
I am creating a WPF and I have also created a side menu for different processes I want to perform. Currently all my code resides in the mainwindoe.xaml.cs. I would like to break out my into seperate files. For example menuitem1 code in one file, menuitem2 code in another file, etc. I prefer this method as I feel it is cleaner and easier to maintain. However I have tried doing Project-->Add Page-->Class but I don't know how to reference the code in the new page. Any help would be greatly appreciated.
Thank you,
Kent
Well, in your class file you have the following:
namespace myNamespace
{
public class MyClass
{
public void MyMethod() { }
}
}
Let's assume that you have this in an assembly named MyDll.dll. You'd use it as follows:
You add a reference to MyDll.dll within the solution explorer
You include the namespace with using myNamespace;
Then you can use your class doing MyClass test = new MyClass();
If you don't add the namespace like Number 2., you'd use your class like:
myNamespace.MyClass test = new myNamespace.MyClass();
You can to put all files in the same or a abstraced namespace. And You have to work with classes in c#.
For example
yourapp.mainwindoe
yourapp.menuitem1
yourapp.menuitem2
Addionally You have to set the classes You Need to Access from another Namespace to at least internal security Setting.
namespace yourapp.mainwindoe
{
class YourClass
{
internal static YourMethod()
{
yourapp.menuitem1.YourOtherMethod();
}
}
namespace yourapp.menuitem1
{
class YourClassOther
{
internal static YourOtherMethod()
{
// do something here...
}
}
I have a common functions AppPath() that is used in several windows forms of my application, instead of declaring it in each form, I would like to place it in a separate cs file.
I have created a separate class cs file, which I gave a different namespace (modUtilities) and put all functions inside a modUtilities class.
namespace modUtilities
{
public class modUtilities
{
// all functions such as AppPath()....
}
}
But I can't figure out how to use function from modUtilities inside different windows forms where I need it. I am trying to use "using modUtilities", instead of creating new instance (modUtilities modU = new modUtilities())
Can someone help me?
if your methods are static within the class, you can do
using modUtilities;
then
var something = modUtilities.AppPath();
if not, you'll need to create an instance of the class.
modUtilities mod = new modUtilities();
I am not a seasoned pro at C# and am running into an error I cannot find a discrete solution to. Currently I have one class that is part of my namespace
namespace BlackBoxStudio
{
public partial class Project : Form
{
This Project class is what implements the various functions of my form. What I am trying to do is make a label.text field accessable to another .cs class within the same namespace. As so, I have made the corresponding fields public:
public string lb_InformationText
{
get { return lb_Information.Text; }
set { lb_Information.Text = value; }
}
To call these I get or set methods in my RsaFunctions class I make a new private project as so:
namespace BlackBoxStudio
{
class RsaFunctions
{
//private Project proj = new Project();
However the line that is commented our causes my vshost32.exe application to fail and the program dies. Why is this causing the vshost32.exe to fail when I un-comment the corresponding line? Or how could I better structure my classes so this does not happen? Any tips would be much appreciated about my strategy for making new classes and such.