Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I pass an object by reference
    primarykey
    data
    text
    <p>I am trying to pass an object from an xna game into a method in an xna game library I'm writing. The object could be basically any type, both reference and value types.</p> <p>The game library is for debug purposes and should print out to the screen the current value of the object passed in. It currently does this by calling <code>ToString</code> on the object but in the future the string will be formatted depending on the underlying type.</p> <p>Here is the game library method that receives the argument</p> <pre><code> public void AddToDebug(object obj) { var type = obj.GetType(); _debugTypes.Add(obj, type); } </code></pre> <p>And here is an example of using it from within the main game project</p> <pre><code>VD.AddToDebug(_counter); VD.AddToDebug(_message); </code></pre> <p><code>_counter</code> is an int and <code>_message</code> is a string.</p> <p>The issue is that changes in these values are not reflected on screen because (I assume) they are passed by value and not by reference.</p> <p>I tried adding the ref keyword to ensure they are passed by reference but that causes an error in the method calls <code>The ref argument type does not match parameter type</code></p> <p>Is there a way I can pass value types by reference without having to specify the actual type (I don't want to have to create method overloads for all value types if I can avoid it).</p> <p>Any other suggestions also welcomed. Thanks</p> <p><strong>UPDATE</strong></p> <p>After trying several different ways I eventually settled on the simple approach of passing a <code>Func&lt;T&gt;</code> into the <code>AddToDebug</code> method as shown below. I realised that, at least at the moment, I don't need the underlying value but only a formatted string representation of it.</p> <p>In the future I may expand the library to automatically scan a passed in type and display values of all its members. If it comes to that I will likely use the suggestion from @Hans Passant to pass in a reference to the Class in question.</p> <p>That said I will accept the answer from @Lee as it was most useful in my final implementation by suggesting the use of <code>Func</code>.</p> <p>This is the code I have now and would welcome any suggestions for improvement. Thank you all for your help.</p> <pre><code> public void AddToDebug&lt;T&gt;(Func&lt;T&gt; getValue, Color? color = null) { var w = new DebugEntry&lt;T&gt;(getValue, color.HasValue ? color.Value : Color.White); _values.Add(w); } public class DebugEntry&lt;T&gt; : IStringFormatter { private readonly Func&lt;T&gt; _getValue; public Color Color { get; private set; } public DebugEntry(Func&lt;T&gt; getValue, Color color) { _getValue = getValue; Color = color; } public string ToFormattedString() { return _getValue.Invoke().ToString(); } } public interface IStringFormatter { string ToFormattedString(); Color Color { get; } } // Example usage VD.AddToDebug(() =&gt; _counter); VD.AddToDebug(() =&gt; String.Format("This message says {0} times", _message)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload