PDA

View Full Version : Visual Basic 9 (from Studio 2008) structure question


Nobody
09-06-2009, 03:23 AM
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.

Imasquare
09-06-2009, 07:33 AM
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

Liberal
09-06-2009, 08:16 AM
Yeah, you used to be able to do that. Not sure about now, though. Maybe you need the "New" keyword.

Waffle Decider
09-06-2009, 09:30 AM
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

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

Liberal
09-06-2009, 10:52 AM
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.

tim314
09-06-2009, 12:04 PM
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?

Waffle Decider
09-06-2009, 02:11 PM
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.

Nobody
09-06-2009, 03:31 PM
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.

Liberal
09-06-2009, 08:48 PM
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.