Ive been playing around with delegates trying to learn and I ran into one small problem Im hoping you can help me with.
class myClass
{
OtherClass otherClass = new OtherClass(); // Needs Parameter
otherClass.SendSomeText(myString);
}
class OtherClass
{
public delegate void TextToBox(string s);
TextToBox textToBox;
public OtherClass(TextToBox ttb) // ***Problem***
{
textToBox = ttb;
}
public void SendSomeText(string foo)
{
textToBox(foo);
}
}
the form:
public partial class MainForm : Form
{
OtherClass otherClass;
public MainForm()
{
InitializeComponent();
otherClass = new OtherClass(this.TextToBox);
}
public void TextToBox(string aString)
{
listBox1.Items.Add(aString);
}
}
Obviously this doesnt compile because the OtherClass constructor is looking for TextToBox as a parameter. How would you recommend getting around the issue so I can get an object from myClass into the textbox in the form?
You can change the OtherClass to something like
class OtherClass
{
public delegate void TextToBox(string s);
TextToBox textToBox;
public OtherClass()
{
}
public OtherClass(TextToBox ttb) // ***Problem***
{
textToBox = ttb;
}
public void SendSomeText(string foo)
{
if (textToBox != null)
textToBox(foo);
}
}
But I am not quite sure what you wish to achive with
class myClass
{
OtherClass otherClass = new OtherClass(); // Needs Parameter
otherClass.SendSomeText(myString);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.