Inheritance and Array's

D

Derek

Hello...Maybe I am not understanding how object arrays are created - but
this problem has bothered me for some time now - Take a quick look at the
code and then my 'problem' below:

#CODE#
Public Class CustomerInfo
Dim _billto() As BillTo

Public Overloads ReadOnly Property Billing() As BillTo()
Get
Return _billto
End Get
End Property

Public Overloads ReadOnly Property Billing(ByVal index As Integer) As
BillTo
Get
Return _billto(index)
End Get
End Property

Public Sub AddBillto(ByVal bill As BillTo)
If _billto Is Nothing Then
ReDim _billto(0)
_billto(0) = bill
Else
ReDim Preserve _billto(UBound(_billto) + 1)
_billto(UBound(_billto)) = bill
End If
End Sub
End Class


Public Class customer
Private _name As String

Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property

Public Sub Clear()
_name = ""
End Sub
End Class

Public Class BillTo
Inherits customer

End Class

#END CODE#

This is over-simplified code of course but I just wanted it to explain this
behavior - In a form I have the following code:

Private _custinfo As New CustomerInfo

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim bleh As New BillTo
bleh.Name = "test string"
_custinfo.AddBillto(bleh)
bleh.Clear()
MsgBox(_custinfo.Billing(0).Name)
End Sub

What I don't understand is why the bleh.Clear event would also be clearing
the _custinfo.Billing(0).Name 's value....Any explanations? Workarounds?

Thanks!
 
J

Jon Skeet [C# MVP]

What I don't understand is why the bleh.Clear event would also be clearing
the _custinfo.Billing(0).Name 's value....Any explanations? Workarounds?

You need to understand the difference between reference types and value
types. You're only actually creating one BillTo object - if you clear
its name, that name will be gone however you access the object, whether
it's via the reference in the bleh variable, or the reference in the
array.
 
D

Derek

You need to understand the difference between reference types and value
types. You're only actually creating one BillTo object - if you clear
its name, that name will be gone however you access the object, whether
it's via the reference in the bleh variable, or the reference in the
array.

Maybe I'm not understanding the difference at all; I was under the
impression that everything is created with New was all ByVal...

Is there any way I can change this around somewhat to make it all ByVal
instead of ByRef?
Sorry if I'm digressing in any way...1240am
 
J

Jon Skeet [C# MVP]

Derek said:
Maybe I'm not understanding the difference at all; I was under the
impression that everything is created with New was all ByVal...

Is there any way I can change this around somewhat to make it all ByVal
instead of ByRef?
Sorry if I'm digressing in any way...1240am

Passing things ByVal and ByRef is a completely separate issue from
reference types vs value types.

See http://www.pobox.com/~skeet/csharp/parameters.html - it's C# based,
but not too hard to understand. It gives a brief overview of reference
types vs value types, and then moves on to the difference between
passing things by reference and by value.
 
D

Derek

Passing things ByVal and ByRef is a completely separate issue from
reference types vs value types.

See http://www.pobox.com/~skeet/csharp/parameters.html - it's C# based,
but not too hard to understand. It gives a brief overview of reference
types vs value types, and then moves on to the difference between
passing things by reference and by value.

Thanks a lot Jon!!! - the website was very informative and helped me
understand exactly what is going on with the code above.
 
J

Jon Skeet [C# MVP]

Thanks a lot Jon!!! - the website was very informative and helped me
understand exactly what is going on with the code above.

Cool. It would be very valuable to me if in your "newly enlightened"
state you could think of anything else which would help to make the
site clearer. It's one of those issues that's absolutely key to
understanding .NET, and it's rarely treated well in books or tutorials
:(
 
D

Derek

Cool. It would be very valuable to me if in your "newly enlightened"
state you could think of anything else which would help to make the
site clearer. It's one of those issues that's absolutely key to
understanding .NET, and it's rarely treated well in books or tutorials
:(
Actually Jon, the site is very clear the way it is. The only thing (for me)
that was slightly confusing about the code I pasted before was that I was
creating multiple references in that array (which is what really threw me
off at first - how can my _billto() array be modified by this other variable
that I thought was completely out of 'its scope' so to speak).

You are absolutely right that this is one more key elements to understanding
..Net - I'm going to see what other people say about your page to see if they
can offer any suggestions.
 

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