# Visual Basic 9 (from Studio 2008) structure question

**URL:** <https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047>\
**Category:** Factual Questions\
**Created:** [September 6, 2009, 7:23am UTC](https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047 "2009-09-06T07:23:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nobody](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@Nobody](https://boards.straightdope.com/u/Nobody)\
**Post date:** [September 6, 2009, 7:23am UTC](https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047/1 "2009-09-06T07:23:55Z")

</div>

Say you have to variables dimmed to the same type of structure. Is there a way to make one equal the other without making each individual element equal each other?

If that sounds confusing, I’ll show an example of what I’m trying to do.

structure A  
dim A as integer  
dim B as integer  
end structure

Dim B as A, C as A

Now, is there a way to simply do a B = C instead of having to do  
B.A = C.A  
B.B = C.B

I’ve tried doing it myself and looking it up, but my Google Fu is week.

Thank you.

---

<div class="post-metadata">

**Author:** ![Imasquare](https://avatars.discourse-cdn.com/v4/letter/i/5f9b8f/32.png) [@Imasquare](https://boards.straightdope.com/u/Imasquare)\
**Post date:** [September 6, 2009, 11:33am UTC](https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047/2 "2009-09-06T11:33:03Z")

</div>

> [@Nobody](#):
>
> Say you have to variables dimmed to the same type of structure. Is there a way to make one equal the other without making each individual element equal each other?
> 
> If that sounds confusing, I’ll show an example of what I’m trying to do.
> 
> structure A  
> dim A as integer  
> dim B as integer  
> end structure
> 
> Dim B as A, C as A
> 
> Now, is there a way to simply do a B = C instead of having to do  
> B.A = C.A  
> B.B = C.B
> 
> I’ve tried doing it myself and looking it up, but my Google Fu is week.
> 
> Thank you.

I haven’t done any VB for a while but from memory B = C should automatically set all of the data members of B equal to those of C.

This page from Microsoft backs that up:

> [@](#):
>
> When you assign one structure variable to another, or pass a structure instance to a procedure argument, the current values of all the variable members are copied to the new structure.

[http://msdn.microsoft.com/en-us/library/2hkbth2a(VS.71).aspx](http://msdn.microsoft.com/en-us/library/2hkbth2a(VS.71).aspx)

---

<div class="post-metadata">

**Author:** ![Liberal](https://avatars.discourse-cdn.com/v4/letter/l/848f3c/32.png) [@Liberal](https://boards.straightdope.com/u/Liberal)\
**Post date:** [September 6, 2009, 12:16pm UTC](https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047/3 "2009-09-06T12:16:01Z")

</div>

Yeah, you \*used \*to be able to do that. Not sure about now, though. Maybe you need the “New” keyword.

---

<div class="post-metadata">

**Author:** ![Waffle\_Decider](https://avatars.discourse-cdn.com/v4/letter/w/77aa72/32.png) [@Waffle\_Decider](https://boards.straightdope.com/u/Waffle_Decider)\
**Post date:** [September 6, 2009, 1:30pm UTC](https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047/4 "2009-09-06T13:30:37Z")

</div>

> [@Imasquare](#):
>
> I haven’t done any VB for a while but from memory B = C should automatically set all of the data members of B equal to those of C.
> 
> This page from Microsoft backs that up:
> 
> [http://msdn.microsoft.com/en-us/library/2hkbth2a(VS.71).aspx](http://msdn.microsoft.com/en-us/library/2hkbth2a(VS.71).aspx)

For a structure, yes. However, one should also be mindful that B = C means different thing if they are a class instead.

---

<div class="post-metadata">

**Author:** ![Liberal](https://avatars.discourse-cdn.com/v4/letter/l/848f3c/32.png) [@Liberal](https://boards.straightdope.com/u/Liberal)\
**Post date:** [September 6, 2009, 2:52pm UTC](https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047/5 "2009-09-06T14:52:18Z")

</div>

> [@Waffle\_Decider](#):
>
> For a structure, yes. However, one should also be mindful that B = C means different thing if they are a class instead.

Right. For classes, B = C establishes independent references. A change made to data in B will not be reflected in the data held by C.

---

<div class="post-metadata">

**Author:** ![tim314](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tim314/32/3468_2.png) [@tim314](https://boards.straightdope.com/u/tim314)\
**Post date:** [September 6, 2009, 4:04pm UTC](https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047/6 "2009-09-06T16:04:06Z")

</div>

> [@Liberal](#):
>
> Right. For classes, B = C establishes independent references. A change made to data in B will not be reflected in the data held by C.

Is this really right? I would have thought the opposite.

I used to do lots of VB back in the pre .Net days, but now I mostly use C#. In C#, structs are value types and classes are reference types. If you equate value types you’re copying the data from one memory location to another, but if you equate reference types you’re making them point to the same memory location. So B = C for classes would mean any change to the data in B also changes the data in C.

Are you saying it’s the opposite in VB?

---

<div class="post-metadata">

**Author:** ![Waffle\_Decider](https://avatars.discourse-cdn.com/v4/letter/w/77aa72/32.png) [@Waffle\_Decider](https://boards.straightdope.com/u/Waffle_Decider)\
**Post date:** [September 6, 2009, 6:11pm UTC](https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047/7 "2009-09-06T18:11:00Z")

</div>

> [@tim314](#):
>
> Is this really right? I would have thought the opposite.
> 
> I used to do lots of VB back in the pre .Net days, but now I mostly use C#. In C#, structs are value types and classes are reference types. If you equate value types you’re copying the data from one memory location to another, but if you equate reference types you’re making them point to the same memory location. So B = C for classes would mean any change to the data in B also changes the data in C.
> 
> Are you saying it’s the opposite in VB?

I agree it’s probably the other way. If B and C are structures, they would refer to different objects in memory. Changes made to one would not affect the other. If B and C are classes, however, they would refer to the same object after the assignment. Changes made through B would also show up in C.

---

<div class="post-metadata">

**Author:** ![Nobody](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@Nobody](https://boards.straightdope.com/u/Nobody)\
**Post date:** [September 6, 2009, 7:31pm UTC](https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047/8 "2009-09-06T19:31:50Z")

</div>

OK, this is the weirdest thing. I was asking because I tried making a function a structure, and then tried making a variable equal the function with a single command, since going through the elements would mean running the function multiple times.

Example:  
structure a  
dim b as integer  
dim c as string  
end structure

dim b as a  
private function c () as a  
some code goes here  
end function

private sub whatever  
b = c()  
end sub

When I first tried something like this, I got an error, so I posted the question. I try it now, and no error.

Well, OK then, never-mind I guess :o.

---

<div class="post-metadata">

**Author:** ![Liberal](https://avatars.discourse-cdn.com/v4/letter/l/848f3c/32.png) [@Liberal](https://boards.straightdope.com/u/Liberal)\
**Post date:** [September 7, 2009, 12:48am UTC](https://boards.straightdope.com/t/visual-basic-9-from-studio-2008-structure-question/509047/9 "2009-09-07T00:48:41Z")

</div>

> [@tim314](#):
>
> Is this really right? I would have thought the opposite.
> 
> […snip…]
> 
> Are you saying it’s the opposite in VB?

No, you are right. My mistake. Obviously, since they are references, a chage in one is a change in all. My apologies to everyone.
