I have a formdlg which can be accessed from two 2 forms
For button click on Form1, it needs to be instance- can have multiple formdlg
But from the other place, I would need only a single instance of formdlg
Any ideas
Thank u
Following is an example code of the class which can provide the answer for you.
class formdlg
{
static formdlg instance;
public static formdlg GetInstance()
{
if (instance == null)
instance = new formdlg();
return instance;
}
}
Since the constructor is public you can call new in the Form1 to get multiple instances anytime you want.
In form2 use the static function GetInstance to retreive the single instance everytime.
Hope this helps.
Simply,
Using Singleton
using System;
public class myForm : Form
{
private static myForm Current;
private myForm() {}
public static myForm Instance
{
get
{
if (Current == null)
{
Current = new myForm();
}
return Current;
}
}
}
Related
How can i find instance of class from another application layer. I have to refresh one propertie from DAL(data acces layer) using my MV(model view). What is simplest way to finish my task. Is this possible??
I mean something like:
SomeClass someClass = FindInstance<SomeClass>([params]);
thanks for help.
What I beleive you are attempting to do is create a singleton object. This is it in it's most simple form.
public class SomeClass
{
//single instance used everywhere.
private static SomeClass _instance;
//private constructor so only the GetInstance() method can create an instance of this object.
private SomeClass()
{
}
//get single instance
public static SomeClass GetInstance()
{
if (_instance != null) return _instance;
return _instance = new SomeClass();
}
}
Now to access the same instance of your object, you can just call
SomeClass singleton = SomeClass.GetInstance();
If you want to use more advanced techniques then you could consider using something like dependency injection, this however is a different discussion.
EDIT:
public class SomeClass
{
private static SomeClass _instance;
private SomeClass()
{
}
public static SomeClass GetInstance()
{
if (_instance == null)
throw new Exception("Call SetInstance() with a valid object");
return _instance;
}
public static void SetInstance(SomeClass obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
_instance = obj;
}
}
I solved my problem with:
SomeClass instance = ServiceLocator.Current.GetInstance<SomeClass>();
I have to add a item in the listBox1 from a static function, but it doens't work because of the static^^; is it possible to call windows forms (like the listBox1) from a static function in c#?
what i want to do:
public static void ListBoxTest()
{
listBox1.Items.Add("something");
}
You cannot access non-static methods inside the static method without creating an instance, else you can use something like the following, by Changing the function signature :
public static void ListBoxTest(ListBox listBox1)
{
listBox1.Items.Add("something");
}
and call the function as:
ListBoxTest(listBox1);
You can try this;
private static Form1 _instance;
public Form1()
{
InitializeComponent();
_instance = this;
}
public static void ListBoxTest()
{
_instance.listBox1.Items.Add("something");
}
Form1.cs
public String Return_inf()
{
names = U_name.Text;
return names;
}
And i try to store it in another class string variable like :
public string CheckLogin()
{
Form1 f = new Form1();
string name=f.Return_inf();
}
But the variable is empty ....
The reason your variable name is empty is because you are creating a brand new Form1 object in your CheckLogin() method, instead of using an already existing Form1 object.
You could have your classes have references to one another.
Here is one example you could try by having the forms have references to one another
Form1 class:
public class Form1 : Form
{
// Class variable to have a reference to Form2
private Form2 form2;
// Constructor
public Form1()
{
// InitializeComponent is a default method created for you to intialize all the controls on your form.
InitializeComponent();
form2 = new Form2(this);
}
public String Return_inf()
{
names = U_name.Text;
return names;
}
}
Form2 class:
public class Form2 : Form
{
// Class variable to have a reference back to Form1
private Form1 form1;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
}
public string CheckLogin()
{
// There is no need to create a Form1 object here in the method, because this class already knows about Form1 from the constructor
string name=form1.Return_inf();
// Use name how you would like
}
}
There are other ways of doing this, but IMO this would be the basics of sharing data between two forms.
you can do it by defining static class like this
static class GlobalClass
{
private static string U_name= "";
public static string U_name
{
get { return U_name; }
set { U_name= value; }
}
}
and you can use it as follow
GlobalClass.U_name="any thing";
and then recall it like this
string name=GlobalClass.U_name;
I have a class which implements the Singleton design pattern. However, whenever i try to get an instance of that class, using Activator.CreateInstance(MySingletonType) only the private constructor is called. Is there any way to invoke other method than the private constructor?
My class is defined as follow:
public class MySingletonClass{
private static volatile MySingletonClassinstance;
private static object syncRoot = new object();
private MySingletonClass()
{
//activator.createInstance() comes here each intantiation.
}
public static MySingletonClassInstance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new MySingletonClass();
}
}
return instance;
}
}
}
And the instantiation as follow:
Type assemblyType = Type.GetType(realType + ", " + assemblyName);
IService service = Activator.CreateInstance(assemblyType, true) as IService;
Activator.CreateInstance, except for one extreme edge-case, always creates a new instance. I suggest that you probably dont want to use Activator here.
However, if you have no choice, the hacky hack hack hack is to make a class that inherits from ContextBoundObject, and decorate it with a custom subclass of ProxyAttribute. In the custom ProxyAttribute subclass, override CreateInstance to do whatever you want. This is all kinds of evil. But it even works with new Foo().
Hei i do not know why are you creating an object of singleton class using reflection.
the basic purpose of singleton class is that it has only one object and has global access.
however you can access any of your method in singleton class like :
public class MySingletonClass {
private static volatile MySingletonClass instance;
private static object syncRoot = new object();
private MySingletonClass() { }
public static MySingletonClass MySingletonClassInstance {
get {
if (instance == null) {
lock (syncRoot) {
if (instance == null)
instance = new MySingletonClass();
}
}
return instance;
}
}
public void CallMySingleTonClassMethod() { }
}
public class program {
static void Main() {
//calling a
methodMySingletonClass.MySingletonClassInstance
.CallMySingleTonClassMethod();
}
}
I want to create a custom message box for a program so I added a windows form item. I would like it to behave like MessageBox in that it is static and I just call MessageBox.Show(a, b, c, ...). In the forms designer, however, I don't see how I can make it static. Can I just add static to the code? Is there a property setting I'm missing in the designer mode?
Thanks!
MessageBox is not a static class, the Show method however is. Make Show static, in code. E.g.
public class MyMessageBox : Form
{
public static int MyShow()
{
// create instance of your custom message box form
// show it
// return result
}
}
It is a regular class with one method as static which instantiate new instance and act.
public class MyMessageBox
{
public static MyResult Show(params)
{
var myMessageBox = new MyMessageBox();
myMessageBox.Message = params ...
return myMessageBox.ShowDialog();
}
}
Add a static method to your form that displays itself and returns a DialogResult:
public partial class MyMessageBoxForm : Form {
public static DialogResult Show(string message) {
using (MyMessageBoxForm form = new MyMessageBoxForm(message)) {
return form.ShowDialog();
}
private MyMessageBoxForm(string message) {
// do something with message
}
}
If you want create static Form1 for access to it without object reference, you can change Program.cs:
public class Program
{
public static Form1 YourForm;
[STAThread]
static void Main(string[] args)
{
using (Form1 mainForm = new Form1())
{
YourForm = mainForm;
Application.Run(mainForm);
}
YourForm = null;
}
}
and call Form1 class methods from any place of your program:
Program.YouForm.DoAnything();
Do not forget to call Invoke for access from other threads.