473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Set Class Instance to Nothing During Instantiation?

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?
Nov 18 '05 #1
3 1707
You can't. You're constructor can throw an exception...which how you
should do it anyways. if that isn't satisfactory, you can wrap your
constructor call in a static property of your class:

dim objGribble as cGribble = cGribble.CreateInstance()
if objGriggle is nothing then..
where CreateInstance() looks something like:

Public shared Property CreateInstance() as CGribble
get
try
return new cGribble()
catch ex as YourCustomException 'for the love of god don't
swallow this exception
return null
end try
end get
end property

karl

"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?

Nov 18 '05 #2
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?

Nov 18 '05 #3
Thanks, Karl and David. These are very interesting approaches, and
they're definitely going into my snippet file! I'm not sure yet which
I'll use for this current project, but I imagine I'll be using them
both in different situations in the future.
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
5
by: Steven Blair | last post by:
I have the following code: Session = new CurrentUser("TEST"); When I postback to the server, the Session is null. My guess is a only the refence to my actual class is stored, rather than the...
3
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
2
by: ashishjainism | last post by:
I am unable to install my windows service and the installutil is throwing the following error: Running a transacted installation. Beginning the Install phase of the installation. See the...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
4
by: Devon Null | last post by:
I have been exploring the concept of abstract classes and I was curious - If I do not define a base class as abstract, will it be instantiated (hope that is the right word) when a derived class is...
5
by: cangove | last post by:
I have a class where I have defined the following overloaded methods: static void* operator new (size_t size, const std::nothrow_t&) throw (); static void* operator new (size_t size); ...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.