86452

Setting Base class attribute from outside

Question:

I want use a base class property in my derived classes. Also, I have to pass the property value from outside. I can think of the following two approaches:

Approach 1:

class Mydata { } class Converter1:Converter { void Converter1(Mydata data1) { data = data1; } public void Convert() { // USING "data" here } } class Converter2 : Converter { void Converter2(Mydata data2) { data = data1; } public void Convert() { //USING "data" here } }

When creating the objects I can create like following:

Mydata data1 =new Mydata() Mydata data2 =new Mydata() Coverter cObj= Converter1(data1 ) cObj.Convert() Coverter cObj1= Converter2(data2 ) cObj1.Convert();

Approach 2:

class Mydata { } class Covnerter1:Converter { public void Convert() { // USING "data" here } } class Converter2 : Converter { public void Convert() { // USING "data" here } }

When creating the objects I can create like following and the set the property value in the base class.

Mydata data1 =new Mydata() Mydata data2 =new Mydata() Coverter cObj= Converter1() cObj.data = data1; cObj.Convert() Coverter cObj1= Converter2() cObj1.data =data2 cObj1.Convert()

Which approach is better? Or any other better approach?

Answer1:

Ignoring the "Converter" strangeness...

In terms of "better as sharing more code and encapsulating data": Often if you need to set property of base class you can do it via base class constructor:

class Base { public MyData { get; private set; } public Base(MyData data) { MyData = data; } } class Derived : Base { public Derived(MyData data):base(data) {} // Methods that use MyData here... }

Recommend

  • iconv library on Mac OS X: strange behavior
  • How can I pass some string containing [$index] into my directive?
  • Merge multiple dataframe pandas
  • In PHP, are objects methods code duplicated or shared between instances?
  • Spark sorting of delimited data
  • How can I prevent a double submit($.post) with jQuery
  • Merge Command in R
  • Python Multiple file writing question
  • Interpret strings as packed binary data in C++
  • FAILURE: Gradle Build failed with an exception
  • Insert records into two table at once
  • Return null in boolean to checkbox state converter in XAML
  • Gstreamer1.0 : link a decodebin to videoconvert
  • EntLib Way to Bind “Null” Value to Parameter
  • is there a way to update filter with async data
  • DateTime.Today for a specific time zone
  • Doctrine/Symfony entity generator and generating entity from one table
  • Casting between Interfaces and Classes
  • jwtBearer bearer token with rc-1 update to ASP.Net 5
  • Enabling DTD support in Sql Server
  • Convert SQLite database to XML
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • Conversion from string “a” to type 'Boolean' is not valid
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Fetching methods from BroadcastReceiver to update UI
  • sending/ receiving email in Java
  • Arrays break string types in Julia
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • embed rChart in Markdown
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?