Is it a Copy or the acutal Object

  • Thread starter Thread starter milop
  • Start date Start date
M

milop

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
 
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
 
Hi, Pete. Thanks for responding.

My question then is what will "Using" do with the dataset inside of
FuntionB?
 
Using will cause the DataSet to be disposed before being returned from the
method.

I would recommend not using Using.
 
"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
 
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
 
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?
 
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
 
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?
 
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
 
Back
Top