Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: GS Newsgroups: comp.lang.basic.visual.misc Subject: Re: What is a class? Date: Sat, 25 Feb 2012 22:55:37 -0500 Organization: A noiseless patient Spider Lines: 63 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15"; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 26 Feb 2012 03:55:46 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="UVJBuiIZprDyxzUkJsiwfw"; logging-data="8431"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+aTxbuaaOjUE2O8kIWIs0E" X-Antivirus-Status: Clean X-Newsreader: MesNews/1.08.03.00-gb X-Antivirus: avast! (VPS 120225-2, 02/25/2012), Outbound message X-Face: G"ln~:.wBqHZznO'(lJjjprxGYAjIF7#^u)lx,@"H'F#uXm%j`T6kxat5rq092aW;K*#m4jZ(2aW$34N&B&E@ j~tjGV-aC18j1y>zi.\[ZGXsd Cancel-Lock: sha1:lr2lrlmXbz4AGbE1fnzZudL3ZAs= X-ICQ: 543516788 Xref: x330-a1.tempe.blueboxinc.net comp.lang.basic.visual.misc:801 Jim Mack wrote : >> Jim Mack wrote: >> >>>> Dim duck As monster >>>> Set duck = New monster >>>> >>>> "Dim duck As New monster" is essentially the same thing as the above 2 >>>> lines. >>> >>> For the purpose of this discussion, maybe. But "Dim As New" is frowned >>> on because it adds an implicit 'is nothing' check on every reference to >>> the object, and you lose control over the object lifetime. >> >> Interesting. First I've heard of it (but then, I don't use VB very much any >> more, and can't recall having ever used VB for "serious" OOP). >> >> Is this documented anywhere? Help file, MSDN, whatever? > > The help files do mention it, but obliquely. What is said (from memory) is > that when you 'Dim As New' the object is not immediately created, as it would > be if you did a Dim followed by a Set = New, but rather is instantiated on > first reference. > > The obvious question is, how does it know which is the first reference? It > knows that because _every_ reference contains a check to see if it's been > created yet -- the implicit 'is nothing' check I mentioned. > > What follows from that is that you can't then legitimately test "If x Is > Nothing" yourself, because doing so references X, and so creates it... that's > how you lose (some) control over object lifetime. I thought that the object created by "Dim As New" existed until the exit from the scope they were created in. So... In a procedure: (local scope) Dim colItems As New Collection ..lives for the entire procedure if not explicitly destroyed beforhand. In a module: (module scope) Dim colItems As New Collection ..lives for the duration code executes within the module if not explicitly destroyed beforhand. Global scope: Dim colItems As New Collection ..lives for the entire runtime if not explicitly destroyed beforhand. Also, I've created objects in this manner, tested them other than "If colItems Is Nothing" via ways like "If colItems.Count > 0" so I know if the created instance exists. Also, creating a new instance by using "If colItems Is Nothing" doesn't cause any problems when you access its items later, indicating VB is not at all confused about which 'instance' you're referring to. -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion