Self referencing structure in VB.NET

  • Thread starter Thread starter Chris Morse
  • Start date Start date
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
 
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
 
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
 
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).
 
Back
Top