?
Solved

Optional Parameter of type Nullable(Of Integer)

Posted on 2007-11-28
8
Medium Priority
?
2,045 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 800 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 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 800 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 400 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 800 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

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!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

770 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