Solved

Optional Color parameter in Sub?

Posted on 2006-06-23
7
1,010 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now