Solved

Optional Color parameter in Sub?

Posted on 2006-06-23
7
1,043 Views
Last Modified: 2010-03-01
I have a function that fills my listviews from a dataview.  There are a  couple occasions where i'd like to change the backcolor when filling it.  How do I have an optional parameter in the sub declaration for color, so I can default it to white and don't need to set it everytime I call it.

But I'm not doing it right.

It says "Optional Parameters cannot have structure types"

Here is my code

Private Sub FillListView(..., Optional ByVal intDateColumn As Integer = Nothing, Optional ByRef clr As Color = Color.White)
   ....
End Sub

Please help.
0
Comment
Question by:Brothernod
  • 3
  • 2
  • 2
7 Comments
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 150 total points
ID: 16971988
Private Sub FillListView(..., Optional ByVal intDateColumn As Integer = Nothing, Optional ByRef strColor As String = "White")
    Dim colConvert As System.Drawing.ColorConverter = New System.Drawing.ColorConverter

            Dim clr As System.Drawing.Color = CType(colConvert.ConvertFromString(strColor), System.Drawing.Color)
End Sub
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16972004
you probably should use ByVal instead of ByRef
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16972547
How about placing the "clr" parameter at the beginning of the parameter list and make it required:

    Private Sub FillListView(ByVal clr As Color, ..., Optional ByVal intDateColumn As Integer = Nothing)
        ' ...
    End Sub

Then have another version that does not have the "clr" parameter at the beginning (using Overloading here):

    Private Sub FillListView(..., Optional ByVal intDateColumn As Integer = Nothing)
        FillListView(Color.White, ..., intDateColumn)
    End Sub

Then you can call the version that takes the "clr" parameter, filling in whatever color you want.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:Brothernod
ID: 16972574
I like the sound of that idle_Mind if you could tell me how do do it.  Is this when i'm adding parameters to a function call from the system libraries it often has more than one set of parameter choices?  I'd LOVE to know how to do this.
0
 
LVL 1

Author Comment

by:Brothernod
ID: 16972583
and Yzlat is def getting some points since that makes sense and works too.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 150 total points
ID: 16972619
Right....here is an example:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' all of the calls below are valid...
        Foo(911)
        Foo(911, 0)
        Foo(Color.Blue, 1000)
        Foo(Color.Red, 2000, 3000)
    End Sub

    Private Sub Foo(ByVal param1 As Integer, Optional ByVal param2 As Integer = 42)
        Foo(Color.White, param1, param2)
    End Sub

    Private Sub Foo(ByVal clr As Color, ByVal param1 As Integer, Optional ByVal param2 As Integer = 42)
        MsgBox(clr.ToString())
    End Sub

End Class
0
 
LVL 1

Author Comment

by:Brothernod
ID: 16974201
Thanks guys.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question