How to do something like this (without creating intermediate classes):
abstract class A
{
public abstract string Text { get; }
}
class B : A
{
string text = "";
public override string Text
{
get { return text; }
}
new public string Text
{
get { return (this as A).Text; }
set { text = value; }
}
}
Compiler say: The type 'B' already contains a definition for 'Text'.
Clarification: How to do so(but without class "Intermediate"):
abstract class A
{
public abstract string Text { get; }
}
class Intermediate : A
{
protected string text = "";
public override string Text
{
get { return text; }
}
}
class B : Intermediate
{
new public string Text
{
get { return (this as A).Text; }
set { text = value; }
}
}
If you want the property to be read-write in derived class, then it is impossible.
The property is a syntax sugar for PropertyType get_PropertyName() (when the property is readable) and void set_PropertyName(PropertyType value) (when the property is writable) methods. This line:
public abstract string Text { get; }
means:
public abstract string get_Text();
And this:
public override string Text{ get; set;}
means:
public override string get_Text()
{
// ...
}
public override void set_Text(string value)
{
// ...
}
Since there's no abstract set_Text method in base class, you can't override it.
You are defining the property Text twice in the same class. You are overriding it and using the keyword new. Delete the second copy of Text.
class B : A
{
private string text;
public override string Text{ get; set;}
}
You can sort of do this with an interface:
interface A
{
// The interface requires that Text is *at least* read only
string Text { get; }
}
class B : A
{
string text = "";
// Implement Text as read-write
public string Text
{
get { return text; }
set { text = value; }
}
}
If that's not going to work for you, you can simply add a TextEx property that is read-write:
public string TextEx
{
get { return text; }
set { text = value;
}
public string Text
{
get { return text; }
}
Related
So my dilemma is that in order to access IntThing's or StringThing's MyProperty from UtilityThing<T>, I'm defining an interface with MyProperty and using it as the generic constraint on T in UtilityThing<T>. This is working, but seems redundant given that the same property is already defined in the abstract base. Am I missing a facet of design here, or is this actually the way it needs to be done in this instance?
public interface IThing {
string MyProperty { get; set; }
}
public abstract class Thing<T> {
protected string _MyProperty;
public abstract string MyProperty { get; set; }
public T OtherProperty { get; set; }
public string CommonMethod() {
return MyProperty + "foobar";
}
}
public class IntThing : Thing<int?>, IThing {
public override string MyProperty {
get { return _MyProperty; }
set { _MyProperty = value + OtherProperty.ToString(); }
}
}
public class StringThing: Thing<string>, IThing {
public override string MyProperty {
get { return _MyProperty; }
set { _MyProperty = OtherProperty + value; }
}
}
public class UtilityThing<T> where T: IThing, new() {
public T DoIt(SomeContext someContext, string name) {
string contextVal = someContext.GetValue(name);
var thing = new T { MyProperty = contextVal }
return thing;
}
}
You'll need to introduce a new generic type. Once the new type is introduced you can eliminate the need of the interface.
public class UtilityThing<T, I> where T : Thing<I>, new()
{
public T DoIt(SomeContext someContext, string name)
{
string contextVal = someContext.GetValue(name);
var thing = new T { MyProperty = contextVal };
return thing;
}
}
And you can use it like this:
var utility = new UtilityThing<IntThing, int?>();
So I have got these 2 instance types "FirstType" and "SecondType" which inherit from the mother class "ContaBancaria". They both return text from different textboxes. Basically, they do the same thing, but I need 2 instances for 2 different list types (I probably don't think the list has anything to do with my question, so I'll proceed not to go in detail)
Here are the instances:
private FirstType AddTypeFirst()
{
return new FirstType(textBoxNumber.Text,
textBoxBalance.Text,
textBoxName.Text,
textBoxAddress.Text,
textBoxBirth.Text);
}
private SecondType AddTypeSecond()
{
return new SecondType(textBoxNumber.Text,
textBoxBalance.Text,
textBoxName.Text,
textBoxAddress.Text,
textBoxBirth.Text);
}
Is there a way to return these 2 instances with the same method type?
EDIT:
What I meant was to return these 2 different types of instances with 1 single method, for example:
private [type?] AddInstance()
{
return new [type*] textBoxNumber.Text, //* the type could be FirstType or SecondType
textBoxBalance.Text,
textBoxName.Text,
textBoxAddress.Text,
textBoxBirth.Text);
}
EDIT 2:
ContaBancaria looks like this:
abstract class ContaBancaria
{
public string number { get; set; }
public string balance { get; set; }
public Client data { get; set; }
}
And, since there's Client...
class Client
{
public string name;
public string address;
public string birth;
}
Hope you get me.
You can use generic method and derrived classes I think.
For example, you have two classes and you want to receive one of them. Those classes are named "FirstSon" and "SecondSon" and both of them are derrived from class "Father".
class Father
{
string myName;
public string MyName
{
get { return myName; }
set { myName = value; }
}
public Father()
{
myName = "John";
}
}
class FirstSon : Father
{
string mySecondName;
public string MySecondName
{
get { return mySecondName; }
set { mySecondName = value; }
}
public FirstSon()
{
mySecondName = "Bill";
}
}
class SecondSon : Father
{
int age;
public int Age
{
get { return age; }
set { age = value; }
}
string mySecondName;
public string MySecondName
{
get { return mySecondName; }
set { mySecondName = value; }
}
public SecondSon()
{
mySecondName = "Drake";
age = 21;
}
}
And you have method GetObject(). This method is generic. It receives type of class, then checks what type it has received and returnes the new object with the same type.
public static T GetObject<T>() where T: Father
{
var firstSon = new FirstSon();
var secondSon = new SecondSon();
if (firstSon.GetType() == typeof(T))
return (T)Convert.ChangeType(firstSon, typeof(T));
return (T)Convert.ChangeType(secondSon, typeof(T));
}
It uses method Convert.ChangeType(object value, Type conversonType) and allows you to convert your object to your type.
But I am not convinced that this is a good idea according to How do I make the return type of a method generic?
Assuming you want to return the proper type based on the list being added to, you will need to write your own generic Add function, and use Reflection to figure out the type:
public static class Ext {
public static void AddInstancia<T>(this List<T> aList) where T : class {
if (typeof(T) == typeof(FirstType))
aList.Add(AddTypeFirst() as T);
else
aList.Add(AddTypeSecond() as T);
}
}
I see no good reason to do this - after all, you know the type of the list, just call the correct function for that list...
Instead of using Reflection, you could also use dynamic if you add some functions to each sub-type:
public class FirstType : Parent {
public FirstType MakeChild() {
return new FirstType();
}
}
public class SecondType : Parent {
public SecondType MakeChild() {
return new SecondType();
}
}
public static class Static<T> where T : new() {
public static dynamic Value = new T();
}
public static class Ext {
public static void AddInstance<T>(this List<T> aList) where T : new() {
aList.Add(Static<T>.Value.MakeChild());
}
}
Which you can call like
var list1 = new List<FirstType>();
list1.AddInstance();
I have a base class
public class ParentSystemInfo
{
private string _Version = "R8.1";
public SystemInfo()
{
this.Version = _Version;
}
}
public string Version {
get { return _Version; }
set { _Version = value; }
}
}
I am inheriting it in another class
public class ChildSystemInfo : ParentSystemInfo
{
public ChildSystemInfo () :base()
{
this.MYInfo= new MYInfo();
}
public string Name { get; set; }
public MYInfo MYInfo{ get; set; }
}
When i serialize this class i get parent property in end of json not at start. why is this happening and how can i prevent it.
You can use the Json property attribute to control the order in which attributes are being serizlied. See documentation here: http://www.newtonsoft.com/json/help/html/JsonPropertyOrder.htm
So you would have:
public class ParentSystemInfo
{
private string _Version = "R8.1";
public SystemInfo()
{
this.Version = _Version;
}
// Setting the JsonProperty to be -1 will ensure it appears before
// all properties for which this attribute was not set.
[JsonProperty(Order = -1)]
public string Version {
get { return _Version; }
set { _Version = value; }
}
}
I'm trying to inherit a method that returns a Generic BindingList of type ServerType. For example, let's say I have the following:
public interface IServer
{
string IpAddress { get; set; }
string Name { get; set; }
string HostName { get; set; }
string OsVersion { get; set; }
}
public class BaseServer : IServer
{
private string _IpAddress;
private string _Name;
private string _HostName;
private string _OsVersion;
public string IpAddress
{
get { return _IpAddress; }
set { _IpAddress = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public string HostName
{
get { return _HostName; }
set { _HostName = value; }
}
public string OsVersion
{
get { return _OsVersion; }
set { _OsVersion = value; }
}
}
public class ServerTypeA : BaseServer { }
public class ServerTypeB : BaseServer { }
public class ServerTypeC : BaseServer { }
public class ServerTypeList : List<ServerTypeA>
{
public BindingList<ServerTypeA> ToBindingList()
{
BindingList<ServerTypeA> myBindingList = new BindingList<ServerTypeA>();
foreach (ServerTypeA item in this.ToList<ServerTypeA>())
{
_bl.Add(item);
}
return _bl;
}
}
Is there any way I can do the "ToBindingList" method without having to repeat it in each derived server class and have it use the correct generic type.
First offf donĀ“t derive from List<T>. Instead use it (favor composition over inheritance).
Then make your Repositories-class generic:
public class Repository : Server
{
}
public class Repositories<T> where T: Server
{
private List<T> theList = new List<T>();
public Repositories<T>(List<T> theList) this.theList = theList; }
public BindingList<T> ToBindingList()
{
BindingList<T> myBindingList = new BindingList<T>();
foreach (Titem in this.theList)
{
_bl.Add(item);
}
return _bl;
}
}
Now you can have Repositories-instances of arbitrary classes deriving from Server.
First, create a base list for all your collections:
public class MyListBase<T> : List<T>
where T: Server
{
public BindingList<T> ToBindingList()
{
BindingList<T> myBindingList = new BindingList<T>();
foreach (T item in this.ToList<T>())
myBindingList.Add(item);
return myBindingList;
}
}
Then use this one to inherit from:
public class Repositories : MyListBase<Repository>
{
}
please tell me best way to implement many duplicate INotifyPropertyChanged.
I have a MainClass that has 10 children, every child has six field and every field must fired property change when own value changed.
this my code but not work:
public class BaseModel
{
public string S1 { get; set; }
public string S2 { get; set; }
public string S3 { get; set; }
public string S4 { get; set; }
public string S5 { get; set; }
public string S6 { get; set; }
}
and I use a class named ViewModelBase to implement INotifyPropertyChanged.
in second step use a class to implement duplicate INotifyPropertyChanged:
public class ImplementBaseModel : ViewModelBase
{
private readonly BaseModel _baseModel;
public ImplementBaseModel()
{
_baseModel = new BaseModel();
}
public string S1
{
get { return _baseModel.S1; }
set
{
if (_baseModel.S1 == value)
return;
_baseModel.S1 = value;
base.OnPropertyChanged("S1");
}
}
public string S2
{
get { return _baseModel.S2; }
set
{
if (_baseModel.S2 == value)
return;
_baseModel.S1 = value;
base.OnPropertyChanged("S2");
}
}
// other code...
}
then a model has 10 of this class:
public class MidClass
{
public ImplementBaseModel ImplementBaseModel1 { get; set; }
public ImplementBaseModel ImplementBaseModel2 { get; set; }
// other field
public ImplementBaseModel ImplementBaseModel10 { get; set; }
public MidClass()
{
ImplementBaseModel1 = new ImplementBaseModel();
ImplementBaseModel2 = new ImplementBaseModel();
// ....
ImplementBaseModel10 = new ImplementBaseModel();
}
}
OK finish code! now please tell me why some property not fired when value change? is a best way to implement this code?
In your setters, you never actually set the value. Use:
public string S1
{
get { return _baseModel.S1; }
set
{
if (_baseModel.S1 == value)
return;
baseModel.S1 = value;
OnPropertyChanged("S1");
}
}
Note that I removed the base from OnPropertyChanged. It isn't normal to invoke the PropertyChanged event in this way.
All NotifyPropertyChanged does is cause every binding to perform a "get" on their bound property. If the backing field is never updated, they will just get the same data.
as a shortcut, you could also create a local method like
bool UpdateAndRaiseIfNecessary( ref string baseValue, string newValue, [CallerMemberName] string propertyName = null)
{
if (baseValue != newValue)
{
baseValue = newValue;
OnPropertyChanged( propertyName );
return true;
}
return false;
}
and then all of the setters would be like this:
set
{
this.UpdateAndRaiseIfNecessary( ref _baseModel.S1, value );
}