Salve a tutti,
da qualche settimana stò studiando il vb.net, utilizzando la guida scritta da Totem .
Arrivato a pagina 82 dove spiega con un esempio le property di tipo reference .
Da quanto ho capito il sorgente che utilizza serve per creare un oggetto "temporaneo" in una variabile di un altra classe, senza modificarne il valore(ditemi se sbaglio).
il codice completo del sorgente con cui lui fà l'esempio è
Il problema è che sull'istruzione:codice:Class Cube Private _SideLength As Single Private _Density As Single Public Property SideLength() As Single Get Return _SideLength End Get Set(ByVal value As Single) If value > 0 Then _SideLength = value Else _SideLength = 1 End If End Set End Property Public Property Density() As Single Get Return _Density End Get Set(ByVal value As Single) If value > 0 Then _Density = value Else _Density = 1 End If End Set End Property Public ReadOnly Property SurfaceArea() As Single Get Return (SideLength ^ 2) End Get End Property Public ReadOnly Property Volume() As Single Get Return (SideLength ^ 3) End Get End Property Public ReadOnly Property Mass() As Single Get Return (Volume * Density) End Get End Property End Class Class ASystem Private _Box As Cube Public ReadOnly Property Box() As Cube Get Return _Box End Get End Property '... End Class '... Sub Main() Dim S As New ASystem() S.Box.SideLength = 4 End Sub
Allora ho provato a fare un po' di testa mia ed ho aggiunto un costruttore nella dichiarazione del campo che puntava alla classe cubecodice:S.Boc.SideLenght = 4 'Errore:Riferimento a un oggetto non impostato su un'istanza di oggetto.
facendo così il programma si esegue senza problemi.codice:Private _Box As New Cube
Il dubbio è ho sbagliato io a interpretare quello che voleva spiegare, oppure era l'esempio che era sbagliato?
P.S Se notate qualche termine tecnico errato, fatemelo notare che sto imparando, e voglio imparare dai miei errori
Grazie


Rispondi quotando