2 questions

  • Thread starter Thread starter gary
  • Start date Start date
G

gary

I am newbie to dotnet.

I wanna make codefile.cs store global variables and procedures so that
form1,form2...formx could call them. but I have 2 questions

1. may I create codefile.cs like the following:

using System.Windows.Forms;

public void MsgBox(string s)
{
MessageBox.Show(s,"Info",btns,icons);
}


2. I call MsgBox in form1.cx, may I call it directly? And if I wanna get a
form2 instance in form1.cs, may I call it like the following?
form2 f2=new form2(this);
f2.Show();

thanks.
 
Hi gary,

See answers inline

I am newbie to dotnet.

I wanna make codefile.cs store global variables and procedures so that
form1,form2...formx could call them. but I have 2 questions

1. may I create codefile.cs like the following:

using System.Windows.Forms;

public void MsgBox(string s)
{
MessageBox.Show(s,"Info",btns,icons);
}

That should work.
2. I call MsgBox in form1.cx, may I call it directly? And if I wanna get
a
form2 instance in form1.cs, may I call it like the following?
form2 f2=new form2(this);
f2.Show();

thanks.

That should work too, but form1 needs a reference to an instance of the
class having MsgBox before it can use it. You can however make the
codefile methods static and then simply refer to the class.

public class Test : Form
{

public Test()
{
MyClass.MsgBox("Hello World");
}



[STAThread]
public static void Main()
{
Application.Run(new Test());
}
}

public class MyClass
{
private static MessageBoxButtons btns = MessageBoxButtons.YesNo;
private static MessageBoxIcon icons = MessageBoxIcon.Error;

public static void MsgBox(string s)
{
MessageBox.Show(s, "Info", btns, icons);
}
}
 
Back
Top