Solved

Optional Color parameter in Sub?

Posted on 2006-06-23
7
1,026 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

22 Experts available now in Live!

Get 1:1 Help Now