Solved

Optional Color parameter in Sub?

Posted on 2006-06-23
7
1,066 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 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 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with another query 10 54
VB.net Excel VSTO Add-in version compatability 2 35
vb.net code 9 28
I need to clear List Box in MVVM panel. 15 26
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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