Access base class(list) properties from derived class c# - c#

I need to access base class properties which is a list from derieved class. Below is the example.I need this for unit testing.
class child : List<Parent>
{
//this class is empty.
}
class Parent
{
public List<Seller> Seller
{
get;
set;
}
public string Id
{
get;
set;
}
}
Am not able to access any properties of parent class . Please help.
unit test code
[Test]
public class test()
{
child a = new child();
a. // not showing any properties of parent
}

if you want to get a.ID , a.Seller you have to declare:
class child :Parent
{
//this class is empty.
}

If you derive from a List<T> you will inherit its abilities, not the abilities from T. What you can do is access one T in the list to access its properties.
public class Parent
{
public bool IAmAParent { get; set; }
}
public class Child : List<Parent>
{ }
var c = new Child();
c[0].IAmAParent = true;
From what I'm seeing I have the feeling you are confused about the inheritance you need. If you need to access the Parent's properties in the Child then it should inherit from the Parent, then put into a list, not the opposite.
public class Parent
{
public bool IAmAParent { get; set; }
}
public class Child : Parent
{ }
var c = new List<Child>();
c[0].IAmAParent = true;

Related

How to call a method with different objects as parameter types

I have multiple objects with similar properties. The objects are stored in dictionaries. Now I want one single Method to work with all dictionaries.
I tried making them child elements of another object. Using the parent object as parameter and calling the function with their child objects was not working for me.
ArticleItem.cs:
public class ArticleItem
{
public int ID { get; set; }
public string Text { get; set; }
... constructor and so on...
}
ArticleItemB.cs:
public class ArticleItemB
{
public int ID { get; set; }
public string Text { get; set; }
public string AnotherText { get; set; }
... constructor and so on...
}
Programm.cs:
public Dictionary<string, ArticleItem> newArticle_Catalog = new
Dictionary<string, ArticleItem>();
public Dictionary<string, ArticleItemB> newArticleB_Catalog = new
Dictionary<string, ArticleItemB>();
newArticle_Catalog.Add("1", new ArticleItem(1,"ABC"));
newArticle_Catalog.Add("2", new ArticleItem(2,"DEF"));
newArticleB_Catalog.Add("1", new ArticleItemB(1,"ABC","DEF"));
newArticleB_Catalog.Add("2", new ArticleItemB(2,"GHI","JKL"));
public void PrintCatalog(Dictionary<string, OBJECTTYPE> catalog)
{
foreach (var item in newCatalog)
{
Console.WriteLine(item.value.ToString());
}
}
I want to call PrintCatalog() either like:
PrintCatalog(newArticle_Catalog);
or like:
PrintCatalog(newArticleB_Catalog);
And get their .ToString() printed out
Create a common base class as:
public abstract class ArticleItemBase
{ }
and let your classes inherit from it as:
public class ArticleItem : ArticleItemBase
and
public class ArticleItemB : ArticleItemBase
You can then make your PrintCatalog accept a Dictionary with a generic value type and restrict it to only objects that are of your base class:
public static void PrintCatalog<T>(Dictionary<string, T> catalog) where T : ArticleItemBase
{
foreach (var item in catalog)
{
Console.WriteLine(item.Value.ToString());
}
}
Create a base Class that has these properties and inherit the base class from all Item classes.
Then in PrintCatalog just take parameter of type base class rather than each item

Set default properties in base class from derived class

Ok so, I've run into an interested and probably simple problem. I have a base class that is inherited by another class (child). I have the same parameterless constructor in the base and the child. I would like to set defaults in the child that propagate into the base properties. I would like to do something like this:
public partial class baseclass
{
public baseclass() {
//never called if instantiated from baseclass(string newp1)
p1 = "";
p2 = "google";
}
public baseclass(string newp1) {
p1 = newp1; //p2 will be "" and p1 will be newP1
}
public string p1 { get; set; }
public string p2 { get; set; }
}
public partial class childclass : baseclass
{
public childclass() {
//How can I call this to set some default values for the child?
p2 = "facebook";
}
public childclass(string newp1) : base(newp1) {
p1 = newp1; //p2 needs to be "facebook"
}
}
Use constructors chaining if you have duplicated code in several constructors:
public class baseclass
{
public baseclass() : this("google") { }
public baseclass(string newp1)
{
p1 = newp1; // the only place in your code where you init properties
p2 = "";
}
public string p1 { get; set; }
public string p2 { get; set; }
}
Child class should inherit baseClass
public class childclass : baseclass
{
public childclass() : this("facebook") { } // you can also call base here
public childclass(string newp1) : base(newp1) { }
}
Also keep in mind that parital just allows you split class/method definiton in several parts (e.g. keep it in different files). It is useful when you are generating classes (e.g. from database tables) but still want to add/customize something in generated classes. If you will put customized code directly into generated files, then it will be lost after classes re-generation. Read more
You can create a protected constructor in base class and call it in child class:
public class Base
{
public Base(int value1, int value2) { ... }
protected Base(string value1) { ... } // only for child class
}
public class Child : Base
{
public Child() : Base("Value") { ... }
}

Retrieve property from class through baseclass C# WPF

I have a class called ModelBase:
public abstract class ModelBase : ViewModelBase
{
public ModelBase
{
ProcessObjects.Instance.AddProcessObject(name, this);
}
public abstract void Dispose();
public String Name { get; set; }
....
....
}
public class SomeModel1: ModelBase
{
public String customprop1 { get; set; }
}
public class SomeModel2: ModelBase
{
public String customprop2 { get; set; }
}
I keep a list of all object instances in an application singleton. Now somewhere else in the application I want to retrieve customprop2 from SomeModel2 using this singleton list. I can retrieve the object as modelbase object and cast it:
SomeMethod()
{
if(_obj.Name == "SomeModel2"){
var _obj = obj as SomeModel2;
var _customProp2 = obj.customprop2 ;
}
}
But ideally i want to just try to retrieve the value straight from the object instance by knowing it is there in the parent of the basemodel.
try{
//Some code to automatically cast the object as parent.
var _customProp2 = _obj.customProp2;
}catch{
//Notify user that his request failed
}
The reason for this is that the user can write into a textbox and start a logging function for that particular property.
Can you create a virtual property/method in the base class and override it in your derived class. This way you can retrieve the values from the object with base class reference, whenever you want.

How to allow nested class instances modification in PropertyGrid not having access to inspected classes code?

Say we have a simple class model with classes as feilds (inside compiled, not modifiable Dll):
public class SubSubClassTest {
public int Data { get; set; }
}
public class SubClassTest {
public string InnerStr { get; set; }
public int InnerInteger { get; set; }
public SubSubClassTest InnerLoad { get; set; }
public SubClassTest() {
InnerLoad = new SubSubClassTest();
}
}
public class Test {
public string Str { get; set; }
public int Integer { get; set; }
public SubClassTest Load { get; set; }
public Test() {
Load = new SubClassTest();
}
}
And we want to edit it using PropertyGrid.
public partial class ApplicationForm : Form {
public ApplicationForm() {
InitializeComponent();
var test = new Test();
propertyGrid.SelectedObject = test;
}
}
And I do not have abilety to change classes (as I get them from Dll) and they have no [TypeConverter(typeof(ExpandableObjectConverter))] attribute on all members that are classes I get sush picture:
And members that are from my namespace class type are not editable.
If all such members havd [TypeConverter(typeof(ExpandableObjectConverter))] attribute I would have another picture and all would be fine:
I wonder how to make PropertyGrid use PropertyGrid for all nested classes?
You could try changing the TypeConverterAttribute value using PropertyDescriptor and Reflection. I wouldn't recommend to do this but to show that its possible I have added the sample code. I verified with your example and it works. But I cannot assure that it would work in all scenarios. Food for thought...
var test = new Test();
SetTypeConverterAttribute(test);
propertyGrid.SelectedObject = test;
private void SetTypeConverterAttribute(Test test)
{
foreach (PropertyDescriptor item in TypeDescriptor.GetProperties(test))
{
TypeConverterAttribute attribute = item.Attributes[typeof(TypeConverterAttribute)] as TypeConverterAttribute;
if (attribute != null && item.PropertyType == typeof(SubClassTest))
{
FieldInfo field = attribute.GetType().GetField("typeName", BindingFlags.NonPublic | BindingFlags.Instance);
if (field != null)
{
field.SetValue(attribute, typeof(ExpandableObjectConverter).FullName);
}
}
}
}
If you have control over the classes, you can create a common base class and decorate this base class with the TypeConverterAttribute. In that case, any property that will reference any instance of this type will use the ExpandableObjectConverter, unless this behavior is overridden by the property (using another TypeConverterAttribute).

XMLSerialization of base class not derived class

I have a base class that implements a number of properties I want to store, and a derived class that contains additional properties that I don't want to store (they can be quite large). I have a container class that contains a list of the base class, which in turn contains a mixture of instances of the base class and its derived class. I create an XMLSerializer for the container class, but on serialization it complains that the derived class isn't included.
Is there any way of forcing the serializer to output base class XML only, irrespective of the instance type?
Note, I don't want to use XMLInclude, as I specifically don't want any of the properties in the derived class to be stored.
(Simplified example of the code)
public class MyBase {
public String Title { get; set; }
}
public class MyDerived : MyBase {
public String Details { get; set; }
}
public class Container {
private static XmlSerializer sSerializer = new XmlSerializer(typeof(Container));
public List<MyBase> mBases { get; set; }
public void MyProblem() {
mBases = new List<MyBase>();
mBases.Add(new MyBase { Title = "One" });
mBases.Add(new MyDerived { Title = "Two", Details = "An incredibly long string" });
using (var lWriter = XmlWriter.Create("C:\\Temp\\output.xml")) {
sSerializer.Serialize(lWriter, this);
}
}
}

Categories