Couple o' noob questions

  • Thread starter Thread starter supranoob
  • Start date Start date
S

supranoob

What is the difference between
Dim thing as New SomeThing
and
Dim thing as SomeThing = New SomeThing?

Also, what causes this? If I take my thing and set a property of it to a
value and then add that thing to a collection, why is it that if I
change the property in my original thing it also changes in the one in
the collection? Or how do I make them independent of each other?

Thanks all.
 
supranoob said:
What is the difference between
Dim thing as New SomeThing
and
Dim thing as SomeThing = New SomeThing?


No difference.
Also, what causes this? If I take my thing and set a property of it
to a value and then add that thing to a collection, why is it that
if I change the property in my original thing it also changes in the
one in the collection?

It is the same thing.
Or how do I make them independent of each
other?

Create a copy and store it's reference in the collection.


See also:
http://msdn.microsoft.com/library/en-us/vbcn7/html/vbconValueRefTypes.asp


Armin
 
No difference.


It is the same thing.


Create a copy and store it's reference in the collection.


See also:
http://msdn.microsoft.com/library/en-us/vbcn7/html/vbconValueRefTypes.asp


Armin

Thanks for the link, Armin. I did not realize that there were value and
reference types. I was only familiar with value and reference in regards
to passing byval or byref. What is the best way to make copy then?

This also adds a little confusion though. If an obect is a reference
type and like stated above I take the original thing that I added to a
collection and set it to a new thing, why are they now two seperate
entities? Shouldn't the one in the collection now be new as well? Is
there some king of copy going on in the background?

Also, what takes precedence here? I create objectA wich has a property
that is of type objectB. I then create objectB and set objectA's
property to that newly created objectB. ObjectA's property accepts
objetB byVal however if I change a property in B it still changes in A.
Should the byval have copied B in A's property leaving me free to do to
the original B what I want without having to worry about screwing up A?


Hope this makes sense and someone can understand my confusion.

Thanks.
 
supranoob said:
Thanks for the link, Armin. I did not realize that there were value
and reference types. I was only familiar with value and reference in
regards to passing byval or byref. What is the best way to make copy
then?

This also adds a little confusion though. If an obect is a reference
type and like stated above I take the original thing that I added to
a collection and set it to a new thing, why are they now two
seperate entities?

Because you created a new one.
Shouldn't the one in the collection now be new as
well?

As long as you don't change the reference in the collection, it will still
point to the first object.
Is there some king of copy going on in the background?

No, just the second execution of "New". :-)
Also, what takes precedence here? I create objectA wich has a
property that is of type objectB. I then create objectB and set
objectA's property to that newly created objectB. ObjectA's property
accepts objetB byVal however if I change a property in B it still
changes in A. Should the byval have copied B in A's property leaving
me free to do to the original B what I want without having to worry
about screwing up A?


ByVal does create a copy, but with a reference type, it's a copy of the
reference, not a copy of the object. See also link below.
Hope this makes sense and someone can understand my confusion.

I do. :-)

Have a look here:

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/41e83beb9988aabf



Armin
 
Because you created a new one.


As long as you don't change the reference in the collection, it will still
point to the first object.
Thanks Armin, that helps. My eye only twitches a little bit now ;).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top