Results 1 to 6 of 6

Thread: Passing a color value into a procedure

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Question Passing a color value into a procedure

    Pardon my stupidity.
    How can I specify a color as an optional arg in a sub with a default, and then pass in a value?

    e.g. I tried this and it failed miserably
    Optional ByVal RayColor As Color = System.Drawing.Color.White

    Color complains
    "Optional parameters cannot have structure types"

    System.Drawing.Color.White complains
    "Constant expression is required"

    Thanks, (a very confused) DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    You are going to have to use the actual values for the colors and turn the optional args into string or seperate rgb values, depending on how you do it. Like it says, optional paramaters cannot use structures. Color is a struct.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422
    Thanks,
    I thought System.Drawing.Color.White equated to a color constant value - for white, e.g. like in VB6
    ? vbWhite
    returns 16777215

    In the .Net Command window I tried
    ? System.Drawing.Color.White
    and it returned
    Code:
    {System.Drawing.Color}
        A: 255
        AliceBlue: {System.Drawing.Color}
    	:
        MintCream: {System.Drawing.Color}
        < More... (The first 100 of 151 items were displayed.) >.
    I never knew "white" could be so complicated.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  4. #4
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Instead of "Optional" you could use overloaded subs.

    VB Code:
    1. Private Sub A()
    2.         A(Color.White)
    3.     End Sub
    4.     Private Sub A(ByVal C As Color)
    5.         'Do your stuff
    6.     End Sub
    If you call A with a color the second sub will be called.
    If you call A without a color the first sub will be called, which in turn just calls the second one with your default color.

    Does this help?
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    That's the most unheard of thing I ever heard of

    But it does work!

    Thanks Pax
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  6. #6
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Your welcome.

    Actually, that's the way MS does it.
    All functions that has several combinations of parameters are made like that.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width