• Welcome to Valhalla Legends Archive.
 

C# 2 -- ParamArray?

Started by Joe[x86], August 19, 2006, 09:52 PM

Previous topic - Next topic

Joe[x86]

This should almost go without explaining. I'd like to make an addChat method which can be called like this, with any number of arguments:

            addChat(this.rtbChat, Color.Red, "Testing ", Color.Blue, "multicolor ", Color.Yellow, "output.");

I hate being spoonfed, or asking to be, but I'm really stuck here. I guess ParamArrayAttribute needs to be used, but I don't know how. So far, here's what I have:

        /**
         * <summary>
         * addChat routine that allows multiple arguments
         * </summary>
         * <param name="rtb">The RichTextBox to output to</param>
         * <param name="data">The data to be put into the RichTextBox</param>
         */
        private void addChat(System.Windows.Forms.RichTextBox rtb, Object[] data)
        {
            rtb.SelectionStart = rtb.Text.Length;
            rtb.SelectionColor = Color.White;
            rtb.SelectedText = "[" + DateTime.Now.ToLongTimeString() + "] ";
            for (int i = 0; i < data.Length; i += 2)
            {
                rtb.SelectionColor = (System.Drawing.Color)data[i];
                rtb.SelectedText = (String)data[i + 1];
            }
        }


So, anyone know what to do?
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

dxoigmn

Use the params keyword.


private void addChat(System.Windows.Forms.RichTextBox rtb, params Object[] data)

Joe[x86]

Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

MyndFyre

Couple other notes:

C#'s code comments use triple-forward-slash for code comments.

         /// <summary>
         /// addChat routine that allows multiple arguments
         /// </summary>
         /// <param name="rtb">The RichTextBox to output to</param>
         /// <param name="data">The data to be put into the RichTextBox</param>


The only other thing I would point out is that this isn't a necessarily good approach to AddChat.  You're going to run into a few issues with performance.  Color objects are structs, and when they're referred to as Objects, they get boxed so that they can be passed by reference.  This has an overhead in copying to the heap from the stack, back when the value is used, and you need to cast.

An alternative is to create a class that holds the color and text data.  Something simple:


internal class ChatInfo {
  public Color Color;
  public string Text;
  public ChatInfo(Color color, string text) {
    this.Color = color;
    this.Text = text;
  }
}

// your addChat function:
private void AddChat(RichTextBox rtb, params ChatInfo[] info);

// wherever you're addchatting:
AddChat(this.rtb, new ChatInfo(Color.Red, "Testing"), new ChatInfo(Color.Blue, "multicolor "), new ChatInfo(Color.Yellow, "output."));


I realize this is slightly more verbose, but it's *much* more object-oriented, much safer, more efficient, more straightforward.

Incidentally, if you didn't want it to be params (there are situations where that's important sometimes), you could have wrapped your calling code this way:


AddChat(this.rtb, new object[] { Color.Red, "Testing", Color.Blue, "multicolor ", Color.Yellow, "output." });


That suffers from the same problems that I listed above.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Joe[x86]

Crap, I was so close. I tried passing an array of Object, but it never struch me to use object.

I always kind of wondered why you used ChatNode in your projects, but I just figured it was for the whole link thing. That explains a bit, I guess. :)
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

MyndFyre

Quote from: Joex86] link=topic=15566.msg156958#msg156958 date=1156074336]
Crap, I was so close. I tried passing an array of Object, but it never struch me to use object.

object is a C# alias for System.Object.  It doesn't matter which capitalization you use.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Joe[x86]

Odd. I tried writing the function as addChat(RichTextbox, Object[]) too, but I guess I got some syntax wrong. Reguardless, it works right now. :)
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

MyndFyre

Quote from: Joex86] link=topic=15566.msg156983#msg156983 date=1156185371]
Odd. I tried writing the function as addChat(RichTextbox, Object[]) too, but I guess I got some syntax wrong. Reguardless, it works right now. :)

Did you get that the third character from the right is a brace and not a parenthesis?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Joe[x86]

Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.