Scope of structure declarations

  • Thread starter Thread starter John Dann
  • Start date Start date
J

John Dann

I'd like to use the same different variables of the same structure in
multiple classes etc. But even though I declare a structure as eg:

Public Structure ExampleStructure
Dim A as short
Dim B as short
etc
End Structure

in one class and works OK within that class, I'm getting a 'Type
'ExampleStructure' is not defined' error when I go to declare a
variable of type ExampleStructure in any class/form etc file other
than the one in which the structure is initially defined.

What am I doing wrong please?

John Dann
 
* John Dann said:
I'd like to use the same different variables of the same structure in
multiple classes etc. But even though I declare a structure as eg:

Public Structure ExampleStructure
Dim A as short
Dim B as short
etc
End Structure

in one class and works OK within that class, I'm getting a 'Type
'ExampleStructure' is not defined' error when I go to declare a
variable of type ExampleStructure in any class/form etc file other
than the one in which the structure is initially defined.

What am I doing wrong please?

Are you sure the structure isn't declared /inside/ another class? Put
it into a separate file called "ExampleStructure.vb" and try again.
 
You didn't specify where the compiler should look for the structure.

In Class2:
dim var as Class1.ExampleStructure

Many thanks. I did try instantiating class1 in class2 and using the
object name to help point to the structure definition, but this just
gave an error. I didn't think to use the class name itself.

I guess this is a sort of generic difference of VB.Net from VB6,
where AFAICR one doesn't use the class name in other components, other
than for instantiating a new object.

JGD
 
John Dann said:
Many thanks. I did try instantiating class1 in class2 and using
the object name to help point to the structure definition, but this
just gave an error. I didn't think to use the class name itself.

You must distinguish between a class and an object here:
The place of the structure is only the place of it's declaration. This does
/not/ mean that an object of type Class1 contains a field of the type
ExampleStructure. You would have to add a field of that type:

class class1
structure ExampleStructure
'...
end structor
public Member1 as ExampleStructure
end class

Now you can write:

dim o1 as new class1
o1.member1.a = 17

I guess this is a sort of generic difference of VB.Net from VB6,
where AFAICR one doesn't use the class name in other components,
other than for instantiating a new object.

It was the same in VB6:
1. Create a new activeX dll project
2. Add two more classes
3. Within Class1 /and/ Class2 add

Public Type test
x As Integer
End Type

4. Within Class3 add

Sub a()
Dim y As test
End Sub

You get an ambiguous name error. Changing it to

Dim y As Class1.test 'or Class2.test

makes the type unambiguous.


The only difference is that, when not specifiying the class name, VB6
automatically searches for the type in all classes. The type was "global".
In VB.NET you have to specify the location because it is not "global". If
there is no reason to put the structure declaration in Class1, put it
outside the class as Herfried suggested. Alternatively (I don't recommend
it!), import Class1:

Imports BaseNamespace.Class1
Class Class2
dim var as test
end class

Now the compiler also looks for the type in the imported class Class1.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Back
Top