CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jay Kimble -- The Dev Theologian

Philosophizing about the .Net religion

Adventures in C#: Why the 'optional' keyword is evil...

[I've begun to do a few more C# projects and so I bought a couple books on the subject.  I'm going to start writing down a few observations along the way].

About a year ago I had someone tell me that the optional keyword in VB (used to define optional parameters) is something that really shouldn't be used.  The advice I got was "don't use it!"  I hate advice like this, because I don't really learn anything... it always begs the question "why?"  When there is no answer to the "why?' I figure that either: 1) there is a reason, but the person doesn't know it, or 2) the person is blowing smoke and wants to act like they know all.

After a year and a half, I now know that the answer is that you shouldn't use optional.  So here's some code (since I'm usually prone to not showing any):

' VB.Net
' Code that implements an optional method
Public Class MyClass
     Public Shared Sub MyMethod(ByVal x as String, Optional y as Integer = -1)
          ' Do something with the data
     End Sub
End Class

Public Class SomeOtherClass
     Public Shared Sub CallCode()
          MyClass.MyMethod("Some String value") ' y is optional so we don't need it
     End Sub
End Class

All that is fine and dandy if you are using only VB.Net.  C# forces you to pass all parameters (whether they are optional or not).  Interestingly enough, there is an optional attribute [it looks like this in C#: "[optional]" ;-) ] that will make a parameter optional for VB.Net when VB is calling C# assemblies, but C# calling the method will still need to pass all parameters (regardless of whether it was created in VB or C#).

I was wrong in what I thought was going on behind the scenes... I guess they (the MS compiler guys) are accounting for some old school VB methods (compatibility).  Anyway, what I thought was going on behind the scenes appears to be the correct way if you want to work in both languages.  It uses overloaded methods (same function name different parameters -- number and type).  Here's the example again using overloaded methods:

' VB.Net
' Code that implements overloaded methods
Public Class MyClass
     Public Shared Sub MyMethod(ByVal x as String) ' implements the Optional y param with a default of -1
          MyMethod(x, -1)
     End Sub

     Public Shared Sub MyMethod(ByVal x as String, y as Integer)

          ' Do something with the data
     End Sub
  
End Class

Public Class SomeOtherClass
     Public Shared Sub CallCode()
          MyClass.MyMethod("Some String value") ' y is now truly optional and the method is callable from C#
     End Sub
End Class

[Wow, I did the post mentioned Adventures in C#, but there's no C# code... the rebel in me is satisfied... Now as long as someone doesn't find and error -- I didn't run this through VS.Net before posting... BTW, Sahil, I managed to use somewhat good form in my code... OK, my param names suck, but beyond that what do ya think?]


Check out Devlicio.us!

Our Sponsors

This Blog

Syndication

News

CodeBetter.Com Home
Current Threat level
Terror Alert Level