Results 1 to 8 of 8

Thread: [2005] optional parameters cannot have structure types

  1. #1

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    [2005] optional parameters cannot have structure types

    That's the error I get for this sig:

    VB Code:
    1. Public Sub DrawSphere(ByVal e As System.Windows.Forms.PaintEventArgs, _
    2.                             ByVal X As Integer, _
    3.                             ByVal Y As Integer, _
    4.                             ByVal Width As Integer, _
    5.                             ByVal Height As Integer, _
    6.                             Optional ByVal Outline As Color = Color.DimGray, _
    7.                             Optional ByVal ColorA As Color = Color.Black, _
    8.                             Optional ByVal ColorB As Color = Color.Gray)
    It's pointing to Color (in As Color = whatever). The weird thing is that I have not created a structure called Color. Any help?

  2. #2
    Addicted Member
    Join Date
    Sep 2004
    Location
    UK
    Posts
    129

    Re: [2005] optional parameters cannot have structure types

    Color is defined in the System.Drawing namespace:

    System.Drawing.Color

  3. #3
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: [2005] optional parameters cannot have structure types

    I don't know for sure, but it sounds like you cannot defined optional variables as anything (ie Optional ByVal Outline, as opposed to Optional ByVal Outline As Color). I don't know for sure, but try taking out the rest of the definition for the three Optional variables and see if that helps.
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

  4. #4

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: [2005] optional parameters cannot have structure types

    supercharged, thanks but i knew that, that doesn't seem to be the problem.
    zani, already tried that a while ago, they have to be defined that way.
    something else is the problem... thanks for responses.

  5. #5
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: [2005] optional parameters cannot have structure types

    I just figured it out. An optional parameter can only be a standard type (integer, boolean, character etc). It cannot be a structure or class. I don't know why
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,913

    Re: [2005] optional parameters cannot have structure types

    When you declare an optional parameter you have to specify a constant as the default value. You cannot do that with a structure unless it is one of the in-built types. Colours are properties of the Color type, i.e. NOT constants. You can declare an optional parameter that is a class type but for the same reason the only default value you can give it is Nothing. The one exception to this is String because it is also an in-built type, even though it's a class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: [2005] optional parameters cannot have structure types

    dang. thanks for the info jmcilhinney. guess i'll have to figure it out another way.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,913

    Re: [2005] optional parameters cannot have structure types

    I'd suggest not using optional parameters at all and use overloading instead, which you have to do in this case. I guess the advantage of optional parameters is that the caller can provide none, any or all of the arguments with values, whereas with overloading you have to define the specific combinations. In this case I'd declare one overload with no colours and one with all three.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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