Valhalla Legends Forums Archive | .NET Platform | C# 2 -- ParamArray?

AuthorMessageTime
JoeTheOdd
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:

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

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:

[code]        /**
        * <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];
            }
        }[/code]

So, anyone know what to do?
August 20, 2006, 2:52 AM
kamakazie
Use the [tt]params[/tt] keyword.

[code]
private void addChat(System.Windows.Forms.RichTextBox rtb, params Object[] data)
[/code]
August 20, 2006, 3:14 AM
JoeTheOdd
Pretty, thanks.
August 20, 2006, 3:29 AM
Myndfyr
Couple other notes:

C#'s code comments use triple-forward-slash for code comments.
[code]
        /// <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>
[/code]

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:

[code]
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."));
[/code]

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:

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

That suffers from the same problems that I listed above.
August 20, 2006, 10:16 AM
JoeTheOdd
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. :)
August 20, 2006, 11:45 AM
Myndfyr
[quote author=Joe[x86] 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.
[/quote]

object is a C# alias for System.Object.  It doesn't matter which capitalization you use.
August 20, 2006, 11:02 PM
JoeTheOdd
Odd. I tried writing the function as addChat(RichTextbox, Object[]) too, but I guess I got some syntax wrong. Reguardless, it works right now. :)
August 21, 2006, 6:36 PM
Myndfyr
[quote author=Joe[x86] 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. :)
[/quote]

Did you get that the third character from the right is a brace and not a parenthesis?
August 21, 2006, 8:20 PM
JoeTheOdd
As an error message? No.
August 24, 2006, 1:52 AM

Search