Click here to Skip to main content
15,941,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I'm stuck on a problem that is driving me crazy: I'm trying to develop a set of classes (actually structures) that represents basic VB.NET data types like Integer, Double, String, ... with the ability to set a DBNull value.
I mean a value of type (say) _Integer can be a number from -2^16 to +2^16-1 or VBNull.Value.
I've successfully written all the required operators and methods and all is working just fine except the assignment from an object type.
Public Structure _Integer
   Public Value As Integer
   Public IsNull As Boolean

   Public Sub New(ByVal obj As Object)
     If TypeOf(obj) Is DBNull Then
         Me.Value = 0
         Me.IsNull = True
         Me.Value = CInt(obj)
         Me.IsNull = False
     End If
   End Sub


   Public Shared Widening Operator CType(ByVal value As Integer) As _Integer
      Return New _Integer(value)
   End Operator

   Public Shared Widening Operator CType(ByVal value As DBNull) As _Integer
      Return New _Integer(value)
   End Operator
End Structure


Dim a As _Integer = 1  //Ok
Dim o As Object = 1
Dim b As _Integer = o //Exception: Cannot Cast From Object

Can somebody pleas help me find a way to initialize my custom type just like a basic type?

Thank you
Updated 4-Feb-11 10:42am

Before you pull all of your hairs (if you have any left ;P ) let me direct your attention to the drawing board. I am not sure what are you trying to achieve by creating your own _Integer that can have NULL value. Have you heard of Nullable Value Types[^]? Is there any thing the Nullable Values can't solve in your problem? I smells to be they are what you need, don't you think? Take a closer look on it decide for yourself.
Share this answer
Sergey Alexandrovich Kryukov 4-Feb-11 18:23pm    
I think you're absolutely right (well, 5), but I already posted it in my Answer earlier (I think, maybe at the same time within the time of editing...).
A just added a note, because OP probably still didn't get it. Please see.
Chances are, you're doing the job already done by Microsoft, available since v.3.5 if I'm not mistaken.

Please see:[^]. Primary purpose of feature is exactly same as yours.

Most search results on top are quite relevant, I checked up.

I looked at the deleted Answer by coccide.
I should note that the reported hassle is not a valid excuse for not using .NET nullable types.
You can always wrap conversion from data base to .NET nullable type. Having null replaced by zero is also not valid. Null and zero are two different values.

Share this answer
In addition to Yusuf's excellent advice there is a prospective problem with your _Integer.

If it is initialized to DBNull IsNull will return True and yet Value will return 0.

It cannot be both. This could cause no end of problems.
Share this answer
Thought I'd suggest an alternative since you cannot create an implicit conversion operator from Object. You can create a constructor that accepts an Object, then perform any initialization there. It's not perfect, but I think that's the best you're gonna get (you could also create a shared function that creates the instance for you). You could then do this:
Dim b As _Integer = New _Integer(o)

Or if you go the static function route:
Dim b As _Integer = _Integer.Create(o)
Share this answer
coccide 4-Feb-11 18:20pm    
I agree
Still I think that this is one of the cases where C++ is better than .NET Framework: I want to do a type that behaves like a basic one.
Damn it! :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900