Or, here's another method. Do a Web Search on "Factory Pattern"
You can implement it kinda like this
Public Class MyClass
Private Sub New()
' You can not directly instantiate this class
End Sub
Public Shared Function GetInstance(param1 as string, param2 as integer,
....) as MyClass
Dim result as new MyClass 'Since its private, the constructor can be
called inside the class
''' Code to possibly load from database
If CanLoadFromDB then
Return result
Else
Return Nothing
End If
End Function
End Class
Instead of :
Dim objGribble as New cGribble()
If objGribble Is Nothing Then ...
You'll end up with:
Dim objGribble as cGribble = cGribble.GetInstance()
If objGribble Is Nothing Then ...
Now this will work. If you're totally set about this type of approach, then
go for it, dude! However, if you use the approach that Carl mentioned, you
do get one major benefit. All you know from this approach is that the class
can not be created. Was it because of a database error? Was it because of
a division Error? What about Solar Flares? If you throw an exception
inside of the constructor, you can catch it and inform the user about the
error, as it was thrown, as opposed to the assumption that it was error "X".
"Jeff Carver" <je*********@hotmail.com> wrote in message
news:b6**************************@posting.google.c om...
I'm creating a class (using VB.NET) that must read data from a
database table during instantiation. If the data cannot be read
(connection problem, etc.), I want the instantiation to fail such that
the following "If" statement will evaluate to True:
Dim objGribble as New cGribble()
If objGribble Is Nothing Then ...
What code in the class's constructor (Sub New) would make that happen?