Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Optional Color parameter in Sub?

Posted on 2006-06-23
7
Medium Priority
?
1,123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 600 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 86

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

Accepted Solution

by:
Mike Tomlinson earned 600 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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