0

I have two classes in two separate libraries (one VB, one C#):

Public Class Item
    ...
    Public Overridable ReadOnly Property TotalPrice() As String
    Get
        Return FormatCurrency(Price + SelectedOptionsTotal())
    End Get
End Property
End Class

and

public class DerivedItem : Item {
    ...
   public new Decimal TotalPrice
    {
        get { return Price + OptionsPrice; }
    }
}

As you can see, the DerivedItem.TotalPrice shadows the Item.TotalPrice property. However, when trying to retrieve the DerivedItem.TotalPrice value, I'm still getting the base object's TotalPrice value.

Why is the DerivedItem's property not being returned?

EDIT

I've actually found the problem! I am getting the wrong value in the JSON string being returned via AJAX. It turns out that the TotalPrice is being returned correctly, it's just being overwritten by the shadowed property call being made later in the JSON string. My new question, then, is how to I prevent the shadowed property from being serialized?

(This question has been rescoped here)

Community
  • 1
  • 1
Jason
  • 51,583
  • 38
  • 133
  • 185

3 Answers3

2

It may depend on how you are instantiating the object.

For example:

DerivedItem i = new DerivedItem();
i.TotalPrice();

Will call the shadowed version.

However:

Item i = new DerivedItem();
i.TotalPrice();

Will actually call the base.

Here's a nice explanation.

Of course if at all possible I would avoid shadowing.... :-)

Kevin LaBranche
  • 20,908
  • 5
  • 52
  • 76
  • i'm instantiating the object from a webmethod parameter. see my comment on mystere man's answer – Jason Apr 06 '11 at 01:19
  • How is SomeMethod using item? Looks fun. VB Library with a C# Library to extend and then a VB AJAX call. :-) – Kevin LaBranche Apr 06 '11 at 01:32
  • yeah it's a fun project for sure... but i'm still having this problem. it's only happening when serializing the object to send back as a JSON string. it's really frustrating. – Jason Apr 06 '11 at 16:54
  • @jason I would bet the serializing / JSON library(ies) is likely the culprit of what you are seeing but of course this isn't much help to fix.... – Kevin LaBranche Apr 07 '11 at 19:50
  • yeah i was thinking that too, but I'm using the built in serializer. check out the new question: http://stackoverflow.com/questions/5570658/prevent-shadowed-property-from-getting-serialized – Jason Apr 07 '11 at 19:53
0

Are you referecing TotalPrice from a reference to the base type?

Item item = new DerivedItem;
string s = item.TotalPrice;
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • no... the item is being bound via asmx web binding from an AJAX call. my sig looks like `Public SomeMethod(item As DerivedItem) As String` – Jason Apr 06 '11 at 01:09
  • How is SomeMethod using item? Looks fun. VB Library with a C# Library to extend and then a VB AJAX call. :-) – Kevin LaBranche Apr 06 '11 at 01:32
0

Does setting <NonSerialized()> attribute on the base class property work?

Zack
  • 598
  • 3
  • 8
  • 23
  • well that's the thing... i still want it to be serialized when the base class is used... – Jason Apr 15 '11 at 15:29