Main Page | Report this Page
.NET DotNet Forum Index  »  VB.NET Forum (Visual Basic .NET)  »  Self referencing structure in VB.NET...
Page 1 of 1    

Self referencing structure in VB.NET...

Author Message
Chris Morse...
Posted: Mon Oct 26, 2009 6:08 pm
Guest
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...
Posted: Tue Oct 27, 2009 2:54 am
Guest
On Oct 27, 5:14 am, "Brian Finn" <bazh... at (no spam) gmx.net> wrote:
Quote:
"Chris Morse" <win32... at (no spam) gmail.com> schrieb im Newsbeitragnews:01aa93d9-dd77-4ad8-8f04-a867e077a656 at (no spam) l13g2000yqb.googlegroups.com...





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- 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.B. Moreno...
Posted: Tue Oct 27, 2009 9:38 am
Guest
Chris Morse <win32mfc at (no spam) gmail.com> wrote:

Quote:
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.

Quote:
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.

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

Looks good.

Quote:
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).

--
J.B. Moreno
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Wed Dec 09, 2009 12:57 pm