Self referencing structure in VB.NET

C

Chris Morse

I'm working to convert some C code to VB.NET and one of the issues I'm
faced with is that of pointers to structures.

Consider this C structure:

typedef struct vertex_type {
double x;
double y;
vertex_type* next;
} vertex;

Converting this to VB.NET doesn't seem really possible. Structures
are value types, and so cannot contain an instance of itself.

Public Structure vertex
Public x as Double
Public y as Double
Public [next] As ???
End Structure

My guess is that I must promote this to a class:

Public Class vertex
Public x as Double
Public y as Double
Public [next] As vertex
End Class

Of course, this changes the semantics of the vertex type - it's no
longer a value type.

Is there a better solution? Is there a way to create a pointer to the
structure so it can be implemented as a structure and not a class?

Any help or pointers (pun intended!) apprecaited!

// CHRIS
 
B

Brian Finn

Chris Morse said:
I'm working to convert some C code to VB.NET and one of the issues I'm
faced with is that of pointers to structures.

Consider this C structure:

typedef struct vertex_type {
double x;
double y;
vertex_type* next;
} vertex;

Converting this to VB.NET doesn't seem really possible. Structures
are value types, and so cannot contain an instance of itself.

Public Structure vertex
Public x as Double
Public y as Double
Public [next] As ???
End Structure

My guess is that I must promote this to a class:

Public Class vertex
Public x as Double
Public y as Double
Public [next] As vertex
End Class

Of course, this changes the semantics of the vertex type - it's no
longer a value type.

Is there a better solution? Is there a way to create a pointer to the
structure so it can be implemented as a structure and not a class?

Any help or pointers (pun intended!) apprecaited!

// CHRIS

Just deklare next as Object, why dont you do that?

Like this:

Public Structure vertex
Public x As Integer
Public y As Integer
Private _next As Object
Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal [next] As
vertex)
Me.x = x
Me.y = y
Me._next = [next]
End Sub
Public Property [next]() As vertex
Set(ByVal value As vertex)
Me._next = value
End Set
Get
Return CType(Me._next, vertex)
End Get
End Property
End Structure

Brian
 
C

Chris Morse

I'm working to convert some C code to VB.NET and one of the issues I'm
faced with is that of pointers to structures.
Consider this C structure:
typedef struct vertex_type {
   double x;
   double y;
   vertex_type* next;
} vertex;
Converting this to VB.NET doesn't seem really possible.  Structures
are value types, and so cannot contain an instance of itself.
Public Structure vertex
   Public x as Double
   Public y as Double
   Public [next] As ???
End Structure
My guess is that I must promote this to a class:
Public Class vertex
   Public x as Double
   Public y as Double
   Public [next] As vertex
End Class
Of course, this changes the semantics of the vertex type - it's no
longer a value type.
Is there a better solution?  Is there a way to create a pointer to the
structure so it can be implemented as a structure and not a class?
Any help or pointers (pun intended!) apprecaited!

Just deklare next as Object, why dont you do that?

Like this:

Public Structure vertex
  Public x As Integer
  Public y As Integer
  Private _next As Object
  Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal [next] As
vertex)
    Me.x = x
    Me.y = y
    Me._next = [next]
  End Sub
  Public Property [next]() As vertex
    Set(ByVal value As vertex)
      Me._next = value
    End Set
    Get
      Return CType(Me._next, vertex)
    End Get
  End Property
End Structure

Brian- Hide quoted text -

- Show quoted text -

Thanks Brian, looks like that's the way to keep the structure a
structure. It's not strongly typed, but it works.

// CHRIS
 
J

J.B. Moreno

Chris Morse said:
I'm working to convert some C code to VB.NET and one of the issues I'm
faced with is that of pointers to structures.

Consider this C structure:

typedef struct vertex_type {
double x;
double y;
vertex_type* next;
} vertex;

Converting this to VB.NET doesn't seem really possible. Structures
are value types, and so cannot contain an instance of itself.
Yep.

Public Structure vertex
Public x as Double
Public y as Double
Public [next] As ???
End Structure

My guess is that I must promote this to a class:
Yep.

Public Class vertex
Public x as Double
Public y as Double
Public [next] As vertex
End Class

Looks good.
Of course, this changes the semantics of the vertex type - it's no
longer a value type.

Is there a better solution? Is there a way to create a pointer to the
structure so it can be implemented as a structure and not a class?

Any help or pointers (pun intended!) apprecaited!

Is there any reason why you must have this as a value type? There are
a few good reasons, but generally it's better to go with a class
(reference type).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top