Jump to content
  • Advertisement
Sign in to follow this  
craigory

[.net] Delegates

This topic is 4439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a class called TextBox, it basically stores some text, position, renders it, etc. I'm passing a delegate into TextBox when I create it, is there anyway that I can have this delegate function modify some of the data members in TextBox? C# won't let me compile, because obviously the function that I want to pass in can't see the variables in TextBox. Is there a way around this?

Share this post


Link to post
Share on other sites
Advertisement
How about simply creating required properties for TextBox which the passed method can use? You can't make the passed method access private fields of the class directly (unless you pass a method of TextBox of course).

What is the context btw? There might be another solution to what you are trying to do.

Share this post


Link to post
Share on other sites
In this instance I wanted the passed method to change the string in the textbox every frame.

I could just make the delegate have to return a string, then when I call the delegate in my TextBox have it set it's string to the returned value. But then every TextBox that I passed a method into would have to change it's string.

Share this post


Link to post
Share on other sites
Why are you making the TextBox dependant on an outside source to function?

Why are you not just having a Text property of the TextBox which you can set?

// this is executed each frame
fpsTextBox.Text = "FPS: " + fps;

Share this post


Link to post
Share on other sites
It's dependant because TextBox is a GUI element that is passed into a scene which is rendered by my engine. I don't have direct access to the main loop. But I think I'll just make something like DynamicTextBox that will inherit from textbox and require a delegate that changes it's string.

Share this post


Link to post
Share on other sites
Quote:
Original post by craigory
It's dependant because TextBox is a GUI element that is passed into a scene which is rendered by my engine. I don't have direct access to the main loop. But I think I'll just make something like DynamicTextBox that will inherit from textbox and require a delegate that changes it's string.
That sounds like quite the complex system you have. Would it be worth a redesign? You should have access to both the draw and update events of the game loop. In update, you'll change all positions and text and stuff and in draw, you'll just render all the elements.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!