Hello.

I'm uncertain about what Function A (below) receives from Function B, a copy
of the dataset or the actual dataset object:

Function FunctrionA () As Dataset

Dim ds as dataset = FunctionB()

'' What is "ds" at this point?

End Function

Function FunctionB() As Dataset

Using cn as New Connection ... to a database
Using ds as Dataset = ...stored procedure call
Return ds
End Using
End Using

End Function


Thanks in advance,

Mike

Re: Is it a Copy or the acutal Object by Peter

Peter
Mon Mar 17 12:49:33 CDT 2008

On Mon, 17 Mar 2008 10:28:59 -0700, milop <milop@slomins.com> wrote:

> I'm uncertain about what Function A (below) receives from Function B, a
> copy
> of the dataset or the actual dataset object:

The original object (IMHO "actual" is misleading, since if it _were_ being
copied, you'd have two "actual" objects...both would be legitimate DataSet
instances in their own right).

Pete

Re: Is it a Copy or the acutal Object by milop

milop
Mon Mar 17 12:55:01 CDT 2008

Hi, Pete. Thanks for responding.

My question then is what will "Using" do with the dataset inside of
FuntionB?

"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
news:op.t7584vbt8jd0ej@petes-computer.local...
> On Mon, 17 Mar 2008 10:28:59 -0700, milop <milop@slomins.com> wrote:
>
>> I'm uncertain about what Function A (below) receives from Function B, a
>> copy
>> of the dataset or the actual dataset object:
>
> The original object (IMHO "actual" is misleading, since if it _were_ being
> copied, you'd have two "actual" objects...both would be legitimate DataSet
> instances in their own right).
>
> Pete



Re: Is it a Copy or the acutal Object by PRSoCo

PRSoCo
Mon Mar 17 13:26:05 CDT 2008

Using will cause the DataSet to be disposed before being returned from the
method.

I would recommend not using Using.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


"milop" wrote:

> Hi, Pete. Thanks for responding.
>
> My question then is what will "Using" do with the dataset inside of
> FuntionB?
>
> "Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
> news:op.t7584vbt8jd0ej@petes-computer.local...
> > On Mon, 17 Mar 2008 10:28:59 -0700, milop <milop@slomins.com> wrote:
> >
> >> I'm uncertain about what Function A (below) receives from Function B, a
> >> copy
> >> of the dataset or the actual dataset object:
> >
> > The original object (IMHO "actual" is misleading, since if it _were_ being
> > copied, you'd have two "actual" objects...both would be legitimate DataSet
> > instances in their own right).
> >
> > Pete
>
>
>

Re: Is it a Copy or the acutal Object by Peter

Peter
Mon Mar 17 13:30:49 CDT 2008

On Mon, 17 Mar 2008 10:55:01 -0700, milop <milop@slomins.com> wrote:

> Hi, Pete. Thanks for responding.
>
> My question then is what will "Using" do with the dataset inside of
> FuntionB?

Huh? Nothing unusual. I don't understand the question. If you know what
the "Using" statement in VB does generally, then you know what it will do
in this situation.

If you don't know what the "Using" statement does, then your question
isn't really about the return value (though, arguably it's not a good idea
to dispose an object that you're returning :), which is what "Using"/"End
Using" winds up doing).

Basically, I wouldn't specify an object in a "Using" statement that I
wanted to return to the caller. But it's not illegal to do so, and for
some objects it might actually work okay (for example, an object that has
expensive, disposable resources but which preserves some other useful
state that can be retrieved after disposal).

Pete

Re: Is it a Copy or the acutal Object by Scott

Scott
Mon Mar 17 13:29:53 CDT 2008

"Using" is used with objects that support a Dispose() method.

When the "End Using" statement gets hit, the object that is delcared with
"Using" will get its Dispose() method called automatically.

In the case of a DataSet, using "Using" will only be of value if you are
reading or writing XML data to/from a file. Since your code isn't doing
that, you don't need to use "Using.

-Scott


"milop" <milop@slomins.com> wrote in message
news:%23IUNhgFiIHA.6084@TK2MSFTNGP06.phx.gbl...
> Hi, Pete. Thanks for responding.
>
> My question then is what will "Using" do with the dataset inside of
> FuntionB?
>
> "Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
> news:op.t7584vbt8jd0ej@petes-computer.local...
>> On Mon, 17 Mar 2008 10:28:59 -0700, milop <milop@slomins.com> wrote:
>>
>>> I'm uncertain about what Function A (below) receives from Function B, a
>>> copy
>>> of the dataset or the actual dataset object:
>>
>> The original object (IMHO "actual" is misleading, since if it _were_
>> being copied, you'd have two "actual" objects...both would be legitimate
>> DataSet instances in their own right).
>>
>> Pete
>
>



Re: Is it a Copy or the acutal Object by milop

milop
Mon Mar 17 15:19:50 CDT 2008

Hi, Pete.

I do know. the reason why I questioned it is that FunctionA was able to bind
a control the dataset returned from FunctionB, even thought FunctionB used
"Using".

I have a feeling the GC didn't collect the Dataset object until FunctionA
used it.

What do you think?

"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
news:op.t76a1nyl8jd0ej@petes-computer.local...
> On Mon, 17 Mar 2008 10:55:01 -0700, milop <milop@slomins.com> wrote:
>
>> Hi, Pete. Thanks for responding.
>>
>> My question then is what will "Using" do with the dataset inside of
>> FuntionB?
>
> Huh? Nothing unusual. I don't understand the question. If you know what
> the "Using" statement in VB does generally, then you know what it will do
> in this situation.
>
> If you don't know what the "Using" statement does, then your question
> isn't really about the return value (though, arguably it's not a good idea
> to dispose an object that you're returning :), which is what "Using"/"End
> Using" winds up doing).
>
> Basically, I wouldn't specify an object in a "Using" statement that I
> wanted to return to the caller. But it's not illegal to do so, and for
> some objects it might actually work okay (for example, an object that has
> expensive, disposable resources but which preserves some other useful
> state that can be retrieved after disposal).
>
> Pete



Re: Is it a Copy or the acutal Object by milop

milop
Mon Mar 17 15:21:15 CDT 2008


> Hi, Peter.
>
> As I just explained to Peter Duniho, FunctionA was able to bind a control
> to the dataset returned from FunctionB, even though FunctionB used
> "Using".
>
> I have a feeling the GC didn't collect the Dataset object until after
> FunctionA used it (?)
>
> What do you think?
>
> Thanks,
>
> Mike
>
> "Peter Ritchie [C# MVP]" <PRSoCo@newsgroups.nospam> wrote in message
> news:C0405002-0FE9-46E7-A968-6969DD586911@microsoft.com...
>> Using will cause the DataSet to be disposed before being returned from
>> the
>> method.
>>
>> I would recommend not using Using.
>>
>> --
>> Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
>> http://www.peterRitchie.com/blog/
>> Microsoft MVP, Visual Developer - Visual C#
>>
>>
>> "milop" wrote:
>>
>>> Hi, Pete. Thanks for responding.
>>>
>>> My question then is what will "Using" do with the dataset inside of
>>> FuntionB?
>>>
>>> "Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
>>> news:op.t7584vbt8jd0ej@petes-computer.local...
>>> > On Mon, 17 Mar 2008 10:28:59 -0700, milop <milop@slomins.com> wrote:
>>> >
>>> >> I'm uncertain about what Function A (below) receives from Function B,
>>> >> a
>>> >> copy
>>> >> of the dataset or the actual dataset object:
>>> >
>>> > The original object (IMHO "actual" is misleading, since if it _were_
>>> > being
>>> > copied, you'd have two "actual" objects...both would be legitimate
>>> > DataSet
>>> > instances in their own right).
>>> >
>>> > Pete
>>>
>>>
>>>



Re: Is it a Copy or the acutal Object by milop

milop
Mon Mar 17 15:26:24 CDT 2008

Let me retype that. I typed it quickly and, gramatically, I sounded like an
idiot:

Hi, Pete.

I do know about "Using", or so I thought. The reason why I questioned it is
that FunctionA was able to bind
a data control to the dataset returned from FunctionB, even though FunctionB
used
"Using".

I have a feeling the GC didn't collect the Dataset object until after
FunctionA
used it, a timing thing of sorts (?)

What do you think?

"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
news:op.t76a1nyl8jd0ej@petes-computer.local...
> On Mon, 17 Mar 2008 10:55:01 -0700, milop <milop@slomins.com> wrote:
>
>> Hi, Pete. Thanks for responding.
>>
>> My question then is what will "Using" do with the dataset inside of
>> FuntionB?
>
> Huh? Nothing unusual. I don't understand the question. If you know what
> the "Using" statement in VB does generally, then you know what it will do
> in this situation.
>
> If you don't know what the "Using" statement does, then your question
> isn't really about the return value (though, arguably it's not a good idea
> to dispose an object that you're returning :), which is what "Using"/"End
> Using" winds up doing).
>
> Basically, I wouldn't specify an object in a "Using" statement that I
> wanted to return to the caller. But it's not illegal to do so, and for
> some objects it might actually work okay (for example, an object that has
> expensive, disposable resources but which preserves some other useful
> state that can be retrieved after disposal).
>
> Pete



Re: Is it a Copy or the acutal Object by Peter

Peter
Mon Mar 17 15:49:16 CDT 2008

On Mon, 17 Mar 2008 13:26:24 -0700, milop <milop@slomins.com> wrote:

> I do know about "Using", or so I thought. The reason why I questioned it
> is
> that FunctionA was able to bind
> a data control to the dataset returned from FunctionB, even though
> FunctionB used "Using".
>
> I have a feeling the GC didn't collect the Dataset object until after
> FunctionA
> used it, a timing thing of sorts (?)
>
> What do you think?

Um. :) The first thing that comes to mind is that you seem to be
confusing disposal of objects with garbage collection. They aren't the
same thing.

Disposing an object tells that object to release any resources it deems
appropriate. This could be anything, but normally it would just be
unmanaged resources (e.g. operating system objects). But in no case would
calling Dispose() on an object affect whether that object itself could be
garbage collected. At best, it could affect other objects that object
references.

So, whether or not you call Dispose() on the DataSet object, the reference
to the DataSet object still exists, and can still be used to whatever
extent the DataSet class allows after being disposed. This could in fact
even include being bound to a control, and given that that seems to work
for you, it appears that DataSet is fine doing that, at least as you're
using the DataSet class there.

Personally, I wouldn't call Dispose() on an object I'm not done with. For
objects that support some kind of early disposal or releasing of
resources, they generally implement some other method to do that, such as
Close() for example. The Dispose() method, even if it's not explicitly
enforced by .NET, carries an implication that you're done with the
object. So don't call it unless you're actually done.

In any case, whether or not it's fine to do that, calling Dispose() on the
DataSet object wouldn't affect whether the object itself is collected by
the garbage collector.

Pete