how to declare a structure before defining it in vb.net

892 views Asked by At

I'm doing a project in which I have to use an algorithm to see if given structures are structurally equivalent. For this an example was given in class like this:

T1 = struct {a: int, p: pointer to T2}
T2 = struct {c: int, q: pointer to T3}
T3 = struct {a: float, p: pointer to T1}

The algorithm says that none of them are structurally equivalent to one other. Our part is to implement the algorithm in c++ taking an input (language from our choice... mine is VB.net) file and output which structures are structurally equivalent.

First of all, pointers are not defined in vb.net. So that leaves that part out, but is it possible that a variable be declared instead of a pointer? For example,

T1 = struct {a: int, p: T2}
T2 = struct {c: int, q: T3}
T3 = struct {a: float, p: T1}

Coding these structures in vb.net (yea, we write the input file too) requires that the structures be already predefined. So is it possible? If so how?

1

There are 1 answers

0
Olivier Jacot-Descombes On

Is your task really to parse the input language? This is quite a complex task, while in .NET there is a technique called Reflection allowing you to analyze a compiled type at runtime without having to do any parsing. By using C++ for .NET and Reflection you could save you a lot of work. You would have to load the VB assembly dynamically. See: Programming with Reflection in the .NET Framework Using Managed C++

VB (and C# as well) are not working with pointers but with references. References are just a high-level abstraction of pointers. While you think of pointers as references a location in memory, references are just variables that point to objects. The physical address of an object can change during its lifetime when the GC (garbage collector) reclaims and re-organizes memory, but the reference is still the same.

Structs are value types and therefore in T1 = struct {a: int, p: T2} the field p is not a reference. Instead T2 (which is a struct) is embedded into T1! If you want to have references, use classes instead.

In VB, a variable of a reference type is automatically a reference. There is no special syntax to follow this reference (like * or ->), just the dot syntax.

Dim p As Person ' p is a reference.
p = New Person()
Console.WriteLine(p.LastName);

See: