[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Optional Parameter of type Nullable(Of Integer)

Posted on 2007-11-28
8
Medium Priority
?
2,055 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

650 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