|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Cancel Constructor (Me = Nothing)Hi,
Most of my Objects have a constructor like this "Public Sub New(ID as Integer)". But when this ID isn't valid, I end up with an instantiated object, that doesn't contain values. Is there a way to kind of cancel the constructor when it detects that there isn't an object with this ID? The main reason is that I like to test on: "MyObject Is Nohting", but in these case is isn't nothing, but doesn't contain any values... Thanks a lot in advance, Pieter [FU2 microsoft.public.dotnet.general]
On Tue, 6 Jun 2006 17:20:19 +0200, Pieter wrote: > Most of my Objects have a constructor like this "Public Sub New(ID as No, you can't "cancel" a constructor. However, if you detect that some> Integer)". > But when this ID isn't valid, I end up with an instantiated object, that > doesn't contain values. Is there a way to kind of cancel the constructor > when it detects that there isn't an object with this ID? > > The main reason is that I like to test on: "MyObject Is Nohting", but in > these case is isn't nothing, but doesn't contain any values... parameters passed to your constructor are invalid, you can throw an exception such as e.g. ArgumentException or ArgumentOutOfRangeException that can be then catched in the client code wherever it makes sense. PS: please set the follow up group when cross-posting Nope, you can't cancel the construction of the object. The correct thing to
do in this case is to throw an ArgumentException (or possibly ArgumentOutOfRangeException) /claes Show quoteHide quote "Pieter" <pietercou***@hotmail.com> wrote in message news:%23ckb6yXiGHA.1272@TK2MSFTNGP03.phx.gbl... > Hi, > > Most of my Objects have a constructor like this "Public Sub New(ID as > Integer)". > But when this ID isn't valid, I end up with an instantiated object, that > doesn't contain values. Is there a way to kind of cancel the constructor > when it detects that there isn't an object with this ID? > > The main reason is that I like to test on: "MyObject Is Nohting", but in > these case is isn't nothing, but doesn't contain any values... > > Thanks a lot in advance, > > Pieter > Or consider an alternative approach:
Make the constructor private and add a shared CreateObject method. This method can return null if the passed in id is not valid. If the id is valid it creates and returns a valid object using the private constructor. /claes Show quoteHide quote "Claes Bergefall" <louplou@nospam.nospam> wrote in message news:OisnW4XiGHA.3408@TK2MSFTNGP05.phx.gbl... > Nope, you can't cancel the construction of the object. The correct thing > to do in this case is to throw an ArgumentException (or possibly > ArgumentOutOfRangeException) > > /claes > > "Pieter" <pietercou***@hotmail.com> wrote in message > news:%23ckb6yXiGHA.1272@TK2MSFTNGP03.phx.gbl... >> Hi, >> >> Most of my Objects have a constructor like this "Public Sub New(ID as >> Integer)". >> But when this ID isn't valid, I end up with an instantiated object, that >> doesn't contain values. Is there a way to kind of cancel the constructor >> when it detects that there isn't an object with this ID? >> >> The main reason is that I like to test on: "MyObject Is Nohting", but in >> these case is isn't nothing, but doesn't contain any values... >> >> Thanks a lot in advance, >> >> Pieter >> > > Pieter,
>Is there a way to kind of cancel the constructor Throw an exception. It will not, however, cancel the creation of the>when it detects that there isn't an object with this ID? object. It's too late for that. Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. "Pieter" <pietercou***@hotmail.com> schrieb: Either throw an exception or add a shared factory method which performs > Most of my Objects have a constructor like this "Public Sub New(ID as > Integer)". > But when this ID isn't valid, I end up with an instantiated object, that > doesn't contain values. Is there a way to kind of cancel the constructor > when it detects that there isn't an object with this ID? > > The main reason is that I like to test on: "MyObject Is Nohting", but in > these case is isn't nothing, but doesn't contain any values... initialization and returns 'Nothing' if construction fails. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/>
Show quote
Hide quote
"Pieter" <pietercou***@hotmail.com> wrote in message You might also consider using a factory and return a Null Object when the ID news:%23ckb6yXiGHA.1272@TK2MSFTNGP03.phx.gbl... > Hi, > > Most of my Objects have a constructor like this "Public Sub New(ID as > Integer)". > But when this ID isn't valid, I end up with an instantiated object, that > doesn't contain values. Is there a way to kind of cancel the constructor > when it detects that there isn't an object with this ID? > > The main reason is that I like to test on: "MyObject Is Nohting", but in > these case is isn't nothing, but doesn't contain any values... > > Thanks a lot in advance, > > Pieter does not exist. Then the object has values and you can use the .IsNull property as needed. The Null Object can eliminate testing for == null in multiple places throughout your code. SP
bring an already running app to the front
Loading CrystalReports.rpt, I Am Asked To Enter Login ID And Password DAAB problem most natural behavior on mouse wheel Upload a file in a web server with a WinForm Lost focus ChartFX Slowness how do I get date and time of compilation Rollover button sampel datagrid Header Center |
|||||||||||||||||||||||