Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Maybe it is a beginner question but I can not seem to figure it out. How do I run the method as seen in the picture?
[[image]: https://i.stack.imgur.com/1QlnL.png][1]
By the looks of your picture your code for the ExcelRun may not be in the same class or linked to the class where you are trying to run it.
Example to get it to work:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ExcelRun();
Bogo.excelRun2();
Bogo n = new Bogo();
n.excelRun3();
}
private void ExcelRun()
{
MessageBox.Show("Have the Method Available in the same Class or callable from another.");
}
}
public class Bogo
{
public static void excelRun2()
{
MessageBox.Show("Hello there");
}
public void excelRun3()
{
MessageBox.Show("I am here");
}
}
This tutorial might help:
https://www.tutorialspoint.com/csharp/csharp_methods.htm
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Disclaimer: I'm really new to WPF.
After looking around online I've understood that the HttpClient should be used as a singleton, shared between windows in WPF.
However, I can't seem to find a clear startup entry-point as you'd find in MVC (startup, duh!).
Where should I instantiate my HttpClient, and how can I use it across multiple windows?
Currently I have two windows; Login and MainWindow. Both really basic. Example:
public partial class Login : Window
{
public Login()
{
InitializeComponent();
}
private void BtnLoginSubmit_Click(object sender, RoutedEventArgs e)
{
}
}
In my App.xaml.cs I've instantiated a HttpClient object which I can access from my MainWindow:
App.xaml.cs:
public partial class App : Application
{
public HttpClient httpClient { get; set; }
}
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
private static ObservableCollection<string> states;
public static void Add(string state)
{
states.Add(state);
}
public MainWindow()
{
InitializeComponent();
((App)Application.Current).httpClient = new HttpClient();
states = new ObservableCollection<string>();
states.Add("Initialized");
states.CollectionChanged += states_CollectionChanged;
LblStates.ItemsSource = states;
Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
}
static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.SessionLock)
{
}
MainWindow.Add(e.Reason.ToString());
}
void states_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
LblStates.Items.Refresh();
}
}
}
The most basic approach would be to add a static class that holds a static instance of HttpClient.
internal static class HttpClientManager
{
public static HttpClient Client = InititializeHttpClient();
}
You can then reference the client from anywhere, like HttpClientManager.HttpClient.
I don't see why you'd need to necessarily share the HttpClient across different windows - the best approach would be to invoke it whenever you need it, unless there is some need to preserve the state. That said, you can create your HttpClient at app-level, and define a HttpClient there.
You can define it in App.xaml.cs and re-use whenever necessary. It doesn't necessarily need to be static, you just need to refer to the specific instantiated entity.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a C# application that opens up two GUIs at startup.
One of the GUI opens three virtual COM Ports. I want the other GUI that is a part of the same application to show the data that is being sent on those virtual COM Ports. Is there anyway to share this COM Port declaration that I do in the Main/First GUI that I am opening up.
Three ways:
1) Copy the instance of the connection from Window1 to Window2.
window2.Connection = this.Connection;
2) Make it the connection a public static member
class Window1 : Window
{
public static SerialConnectionClass Connection { get; set; } = new SerialConnectionClass();
}
You can access the connection like that:
class Window2 :Window
{
...
private void func()
{
Window1.Connection.Send("");
}
}
3) Make a static ConnectionManager
static class ConnectionManager
{
public static SerialConnectionClass Connection { get; set; } = new SerialConnectionClass();
public static EventHandler MessageReceived;
public static void Send(string text)
{
Connection.Send(text);
}
...
}
Use it in Window1 and Window2 like this:
class Window2 :Window
{
...
private void func()
{
ConnectionManager.init("COM1");
ConnectionManager.MessageReceived += this.MessageReceived;
ConnectionManager.Send("test123");
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I had an interview question that asked roughly the following: with dependency injection, how do you prevent all of the classes from being instantiated? What if you only want a few, but not all? There are good reasons, they said, for example to avoid them all being in memory at the same time...
I've tried to research this question but it's hard to even figure out the best search term is. And no answers could I find.
A simple way would be to create it with a constructor and instantiate a single class based on a flag or switch. Here is a simple example that shows that only a single OtherClass or OtherClass2 would be instantiated.
I would like to add however that it isn't really Dependency Injection that allows the prevention of instantiation. You could get rid of the DI and have tightly coupled classes and still have a single instance of a class. So maybe I am not understanding the question correctly or maybe the interviewer asked it in a different context/manner?
class AppStart
{
OneClass One;
int _whatToCreate = 0;
public int WhatToCreate
{
get { return _whatToCreate; }
set { _whatToCreate = value; }
}
public void Start()
{
if (_whatToCreate > 0)
{
One = new OneClass(new OtherClass());
}
else
{
One = new OneClass(new OtherClass2());
}
One.PerformSomething();
}
}
class OneClass
{
IDoSomething _doSomething;
public OneClass(IDoSomething doSomething)
{
_doSomething = doSomething;
}
public void PerformSomething()
{
_doSomething.DoSomething();
}
}
class OtherClass : IDoSomething
{
public void DoSomething()
{
//throw new NotImplementedException();
}
}
class OtherClass2 : IDoSomething
{
public void DoSomething()
{
//throw new NotImplementedException();
}
}
interface IDoSomething
{
void DoSomething();
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I would like to ask if I have a class Person and Food,
and I want to have method of "eat".
Should I put eat method in class Person or method eaten in class Food ?
Thank you.
It depends that you say Person.Eat(food) or Food.EatenBy(person)
If you preffer Person.Eat(food)
public class Person
{
public void Eat(Food food)
{
//...
}
}
If you preffer Food.EatenBy(person)
public class Food
{
public void EatenBy(Person person)
{
//...
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm creating a simulator of ecosystems where species can be used to simulate various diseases, my problem is that I start using 4 species but if I need more ... I need more variables to store, my question is, Is there any way through Reflection to let me create dynamic variables during the execution of an event in my program? Thank you! i'm using Windows Presentation Foundation and C#
The normal way to handle this is to have a base class for your disease species and then use a collection to hold them all:
public abstract class DiseaseBase
{
public abstract void Spread();
}
public class Anthrax : DiseaseBase
{
public override void Spread()
{
GetPostedToPolitician();
}
}
public class BirdFlu : DiseaseBase
{
public override void Spread()
{
Cluck();
SneezeOnHuman();
}
}
public class SwineFlu : DiseaseBase
{
public override void Spread()
{
//roll in mud around other piggies
}
}
public class ManFlu : DiseaseBase
{
public override void Spread()
{
//this is not contagious
//lie in bed and complain
//get girlfriend to make chicken soup
//serve chicken soup with beer and baseball/football/[A-Za-z0-9]+Ball
}
}
public List<DiseaseBase> DiseaseCollection = new List<Disease>();
So everything gets stored in the collection as the base class (DiseaseBase), and with the appropriate use of abstract methods in the base and/or interfaces you can always handle each disease instance as the base object.