Solved

Optional Parameter of type Nullable(Of Integer)

Posted on 2007-11-28
8
2,029 Views
Last Modified: 2012-05-05
I'm trying to create a sub that can be called from various places in my code.  It will interrogate the database and return a datatable.  Now I want an optional ID parameter in the sub, which gets passed to the database.  

So I could call this:
   getData(536)   which would return the database row with ID 536

Or I can call:
   getData()         which should return every row

The problem is that in Visual Studio I get the following error:
      BC31405: Optional parameters cannot have structure types.

Is my approach possible to implement?  I know I can remove the optional keyword and call getData(Nothing) but I'd like to know the alternatives if there are any!
Protected Sub getData(Optional ByVal Id as Nullable(Of Integer)) As DataTable
  ....
End Sub

Open in new window

0
Comment
Question by:Rouchie
[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
8 Comments
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20368945
Try using ByVal Id as Nullable(Of Integer), and remember you need a Function to return something.
Protected Function ...(ByVal Id as Nullable(Of Integer)) as DataTable
  If Id.HasValue Then
    ' Return selection
  Else
    ' Return whole table 
  End If
End Function

Open in new window

0
 
LVL 38

Accepted Solution

by:
PaulHews earned 200 total points
ID: 20368963
You don't really need a nullable type here... I usually do something like this.
Private Function GetData(Optional ByVal ID As Integer = -1) As DataTable
    If ID = -1 Then
        'get all rows'
    Else
        'select rows where id = id parameter'
    End If
End Function

Open in new window

0
 
LVL 19

Assisted Solution

by:SteveH_UK
SteveH_UK earned 200 total points
ID: 20369013
Nullable is a better option, because it forces the developer to intentionally choose what to do.  Yes it is harder to call -- you can always get around this by overloading the function --- but you make your code clear.

Using -1 is a bad idea, because it attaches special significance to a particular number.  If the number is truly always >= 0, then you should use an unsigned type.
0
Independent Software Vendors: 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 38

Assisted Solution

by:PaulHews
PaulHews earned 200 total points
ID: 20369330
>Using -1 is a bad idea, because it attaches special significance to a particular number.  If the number is truly always >= 0, then you should use an unsigned type.

Kind of like when you use IndexOf that returns -1 when the object is not found?  It's just a convention, and not a terrible one.  We use them all the time in .NET.

With the code I posted  you can call
 GetData() or GetData(Number)

which was exactly what the questioner asked for.  You can ignore the value of -1 while using the function as it is the optional default.

If you want to be picky, passing a null to the function isn't particularly self-explaining either.  What is the significance of the null in question?  It's just a convention, it doesn't really mean the ID is null.

To continue being picky, the really clear way to do it is with two functions:

Private Function getAllData() As DataTable

and

Private Function getDataByID(ByVal ID as Integer) As DataTable

but it's more a question of style than anything.
0
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20369478
Actually, your last comment is what I offered in using an overloaded function.  I agree there are conventions, but not all of them are good and many come from a historical perspective.  With a language that supports Null values, it makes far more sense to use them.

The problem with using -1 is that it reduces the range of acceptable values, and suggests that it is passing a valid value.

I was not suggesting that your response wasn't _a_ solution, but I disagree strongly that using arbitrary values to control behaviour is good practice.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
ID: 20370933
I support PaulHews's solution. It is more legible and simple than Nullable (specially in vb.net)
About the -1 stuff, it is clear that is an invalid ID, so it can be used without worries. Not a bad idea at all, and not a big deal, because the function GetData() doesn't return it, it returns a valid DataSet or Nothing.
Inside GetData you can evaluate it as:
If ID<0 Then...

This approach is vastly used in WinAPI and .net libraries. By example, .net IndexOf()
0
 
LVL 25

Author Comment

by:Rouchie
ID: 20380549
Thanks for your replies.  Your advice has confirmed my suspicions.

I'll have a think about which approach is going to be best in the long run.  Just out of interest, what does this error actually mean?  Why isn't it possible to do something like:

  Protected Function getData(Optional ByVal Id as Nullable(Of Integer) = Nothing) As DataTable
0
 
LVL 19

Assisted Solution

by:SteveH_UK
SteveH_UK earned 200 total points
ID: 20381483
The Nullable(Of Integer) type is actually represented by a structure, a bit like an envelope around an Integer value.  This is distinct from a reference type.  

Basically, these optional parameters are a VB.NET language construct and there are some things they do not support.  There isn't a really good reason for it in your case.  Note that "Nullable(Of Integer) = Nothing" is a statement involving a conversion, because Nothing is not the same Type as Nullable(Of Integer), so that throws up further errors.  Using an overloading function would let you do exactly what you want, in much the same way you are actually causing the compiler to do it.

You might also want to consider reading:
http://jelle.druyts.net/2003/05/17/OptionalParametersVBNET.aspx
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

688 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