If I have a class:
class Foo
{
public int a = 1;
public int b = 2;
public float c = 10.1f;
public Foo()
{
}
}
and I want to send its parameters to another class that displays the parameters as a string.
How can I do this using delegates and events?
I've created a delegate outside of any class and under the common namespace:
public delegate void OnClick(object sender);
I have created another class that will display the object name as a string:
class InfoBar
{
public Color colour;
public SpriteFont font;
Rectangle quadrangle;
public Texture2D texture;
public bool visible;
string objectSelectedName = "None";
public InfoBar(Vector2 position, int width, int height, Texture2D texture, SpriteFont font, Color colour)
{
this.colour = colour;
this.font = font;
this.quadrangle = new Rectangle((int)position.X, (int)position.Y, width, height);
this.texture = texture;
visible = true;
OnClick d1 = new OnClick(ClickHandler);
}
public void Draw(SpriteBatch sprite_Batch)
{
if (!visible)
{
return;
}
// Draw the info bar
sprite_Batch.Draw(texture, quadrangle, colour);
Vector2 position = new Vector2();
position.X = quadrangle.Center.X;
position.Y = quadrangle.Center.Y;
sprite_Batch.DrawString(font, objectSelectedName, position, colour);
}
public void Update()
{
if (visible)
{
}
}
/// <summary>
/// Event handler for when an object is selected
/// </summary>
void ClickHandler(object sender)
{
objectSelectedName = sender.ToString();
}
}
How do I get Foo to send itself and therefore its available parameters to InfoBar when invoking InfoBar's OnClick?
I've studied the MSDN tutorial
here but I can't figure out what is going on.
Can anyone please help?