How to call a class inside of an application - C# - c#

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.

Related

How do I make my library work with using-directive?

I created a class library with .Net 6.0 which looks like this:
namespace MyLib
{
public static class MyLib {
public static void someFunction() { }
}
}
I created a nuget package and added that package to another application. My expectation was that I would put using MyLib at the top of a class and then could call MyLib.someFunction() in the code. But instead, I always have to call MyLib.MyLib.someFunction(), with the using-directive doing nothing.
Can someone explain to me what I'm doing wrong?

How do I use a separate C# file on one xaml file

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.

How to make function "invisible" in a class library c#

I am creating a class library and have different functions inside. I also have a Console application that can access this functions once they reference the class library. I would like to know how to make a function "invisible" so the client won't be able to see it exist and they can only use it if they write it out perfectly.
I have this function in my class Library :
public string custommessage(string messagetosend)
{
string receivedmessage = CallServer(messagetosend);
return receivedmessage;
}
and basicly when I am in a different program referencing the library I dont want to see this function in my list of avaiable functions to chose from :
Append
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
to your method as an attribute. This will hide the function from intellisense.

In WPF how do I reference and use a cs file from MainWindow.xaml.cs [duplicate]

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...
}
}

vshost32.exe failing due to poor naming class and object managment

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.

Categories

Resources