assigning user-structured types as a whole

Wolow

Registered User.
Local time
Today, 20:38
Joined
Dec 27, 2009
Messages
10
Hi, I have missing knowledge/a problem
Can I assign one variable of a user defined type to another of the same type?
Passing by value in a procedure does not compilet, but if I do this:

public type structtype
a as integer
b as integer
c() as string
cCount as long
end type

dim a as structype, b as structtype

with a
.a = 1
.b = 2
redim .c(1 to 3)
.c(1) = "works"
.c(2) = "really"
.c(3) = "well"
.cCount = 03
end with

a = b

If I run it like this, it compiles and ACTUALLY WORKS!!! B has all values of a, no reference. This is amazing, as it seems to be even possible to have dynamic array corretly transferred, even if these arrays are of user defined types.
BUT:
I have no idea what kind of damage this possibly could do, e.g. writin into memory areas that should be protected, you name it.

Does anyone know if this "working" is a bug or if this is acutally correct?

Thanks a lot

Wolow
 
Why do you think it's a bug? Aren't variables a and b of the same structtype of which contains an array as one of its elements?
 
you couldnt be writing into protected memory - its not like the old days with text based displays when you could address the screen display area directly, and achieve instant popup windows, just by saving 2K of a screen buffer (80x25), and pushing a different 2K into the screen memory.

=============
both struc_a and struc_b are variables that are instantiated on the stack, and pointed to by token represented by the variable (although that probably isnt the technically accurate description!)

struc_b= struc_a just copies the data stored in the block of memory represented by struc_a, into the block represented by struc_b

now i am not exactly sure how the compiler actually handles the data pointed to by a redimmed structure - but i am sure it will be consistent. I expect the array itself is at the end of another pointer (so that the redim can work) In the same way, i expect strings are pointed to as well - since strings also do not get allocated a fixed amount of memory. (i mean what happens when you lengthen a string - does the compiler move the data to a continuous area of memory - or does it maintain the string as a chain of pointed segments)

at some point you can fill the stack - try a deep recursion - and you will get a programme crash with a stack overflow error.

==========
the beauty of high level languages is that you can just declare variables, and generally leave the compiler to manage them - which is also why not disposing of certain temporary structures (objects) correctly leads to memory leakage, etc.


=========
and although you cant do it in vba, its amazing what you can achieve with a pointer in a language that supports it.
 
Last edited:
Well, it got me concerned that I cannot pass a structure like by val. For the above example:

sub dosmethingweird(byval a as structtype)

wont compile. This indicates for me that there is something iffy about assignin user defined types. In reality this byVAl would not be anything else the a = b, correct?
 
i just tried this

I got a compiler error saying you cannot pass a user defined structure byval.
I was able to pass it by reference.

That is any typed structure, irrespective of the array - my type just contained a single long integer.
This isnt necessarily good or bad - it just is - as long as you are aware of it, you can get round it. If you want a local copy just declare a variable in the sub, and copy the argument - then you have a local copy.

I was able to pass an array by both value or reference.

================
You have to think about programme structure in terms of what is really happening at the machine level

so when you pass a parameter by reference, what REALLY happens is that you actually just the address/pointer of the variable in memory - so the sub/function is actually processing the real data

when you pass a parameter byval, a copy of the variable is placed on the stack.

Now I guess it would have been possible to implement this for a user-defined structure - but the compiler writers just decided not to permit a copy of a complex structure to be made.

It doesnt mean there is anything iffy - its just a design decision, like many others

==============

and its things like that that make c (say) a far more powerful language than vba.

you can do things in c that are not possible in vba.

Here are some examples

as already stated, you do not have a pointer data type in vba - which is such a powerful facility. A pointer is just a memory address, so it could have been implemented - and in fact it MUST exist in the background, as I am sure colections, for example, must use a pointer chain. But it isnt exposed to VBA

in some languages you can have recursion - in others not - if you are used to recursion, you will understand how elegant and powerful it can be, while at the same time how hard it can be to use and even understand

in vba an error handler is not re-entrant - ie you cannot intercept another error while an error handler is still processing the current error

in vba the DIR function is non recursive, so unforunately you cannot use the function to
scan multiple levels of a folder structure.

etc etc

it doesnt mean there is anything strange - its just the way MS decided to implement VB.
 
Last edited:
Yea, makes sense.
I am happy if the assigning of structure types is not crating havoc, to thats fine with me.
Thanks for the detailed information :)
 

Users who are viewing this thread

Back
Top Bottom