Sunday, 15 February 2015

c# - Inheriting from a class and adding an additional property -



c# - Inheriting from a class and adding an additional property -

i have class in 1 application - that cannot change (legacy) - within of assembly (dll file):

public class shippingmethod { public string shipmethodcode { get; set; } public string shipmethodname { get; set; } public decimal shippingcost { get; set; } public list<shippingmethod> getallshippingmethods() { ...... } }

i have sec application referencing assembly (dll file) , needs populate drop-down shipping methods. ex: "ups - $3.25"

the issue needs using right format different currencies. ex: $3.25 or 3.25€ depending on parameter called countryid.

i have written function string displaymoney(decimal amount, integer countryid) homecoming right format of amount.

now need apply function every shipping method , save new list. best way this?

i can create class called localizedshippingmethods follows:

public class localizedshippingmethod { public shippingmethod shipmethod { get; set; } public string localizedshippingcost { get; set; } }

is best way accomplish this? there improve way using inheritance? , if utilize inheritance, how values first list new list?

that indeed method of doing it. can utilize pretty quick linq query pull old list new one:

list<localizedshippingmethod> translate(list<shippingmethod> oldlist) { homecoming oldlist.select(a => new localizedshippingmethod { // initialize properties according translate them }).tolist(); }

additionally, create more streamlined , obvious, of next aid in translation:

create constructor localizedshippingmethod takes in shippingmethod , sets properties create static method on localizedshippingmethod takes in shippingmethod , returns initialized localizedshippingmethod create operator on localizedshippingmethod converts shippingmethod create extension method on shippingmethod, phone call tolocalized() returns localizedshippingmethod

c# class inheritance

No comments:

Post a Comment