C # Assigning default property for class and operator =

Problem 1:

I have a simple winforms application and I want to assign a DataBind property to my Person.Name TextBox. The name is of type StringField. I originally defined the Name property as String. Data binding works great with value types like String. I would like the StringField.Value property to be the standard StringField property. I want to see the value of StringField.Value in a text field, and not the text "FieldApp.StringField".

Problem 2:

I would like to be able to assign a StringField string using the = operator. This assignment will set the StringField.Value element.

Can this be done?

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace FieldApp { public class StringField { public string Value { get; set; } } public class Person { //private String _Name; //public String Name //{ // get { return _Name; } // set { _Name = value; } //} //public Person(string name) //{ // Name = name; //} private StringField _Name; public StringField Name { get { return _Name; } set { _Name = value; } } public Person(string name) { Name = new StringField(); Name.Value = name; } } public partial class FieldAppForm : Form { Person person = new Person("steve"); public FieldAppForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //our form contains a button1 and textBox1 //this compiles person.Name.Value = "steve"; //this does not. Is there anyway to accomplish this? person.Name = "steve"; //steve appears in the textbox textBox1.DataBindings.Add("Text", person, "Name.Value"); //FieldApp.StringField appears in the textbox textBox1.DataBindings.Add("Text", person, "Name"); } } } 
+6
c # properties default
source share
5 answers

You can create an implisit operator overload. Then you can create a Stringfield from strings as follows:

 StringField field = "value of new object"; string value=(string)field; 

Be aware that this creates a new StringField object. I would not advise you to do this.

 [System.Diagnostics.DebuggerDisplay("{Value}")] public class StringField { public string Value { get; set; } public static implicit operator StringField(string s) { return new StringField { Value = s }; } public static explicit operator string(StringField f) { return f.Value; } public override string ToString() { return Value; } } 
+10
source share

Re data binding for some target bindings ( PropertyGrid , DataGridView , etc.), you can do this using TypeConverter (see below). Unfortunately, this does not work with TextBox , so I think your best option is to simply add a padding property (as already suggested):

 string NameString { get { return Name.Value; } set { Name.Value = value; } // or new blah... } 

(and binding to NameString )

In the past, I have used custom implementations of PropertyDescriptor for this, but it is not worth it.

Anyway, an example of TypeConverter (works with PropertyGrid and DataGridView ):

 [TypeConverter(typeof(StringFieldConverter))] public class StringField { public StringField() : this("") { } public StringField(string value) { Value = value; } public string Value { get; private set; } } class StringFieldConverter : TypeConverter { public override bool CanConvertFrom( ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } public override object ConvertFrom( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { string s = value as string; if (s != null) return new StringField(s); return base.ConvertFrom(context, culture, value); } public override bool CanConvertTo( ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(string) || base.CanConvertTo(context, destinationType); } public override object ConvertTo( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value != null && value is StringField) { return ((StringField)value).Value; } return base.ConvertTo(context, culture, value, destinationType); } } 
+2
source share

You can implement the assignment by providing a conversion operator. Given the nature of your class, you should also override Object methods:

 public class StringField { public string Value { get; set; } public static implicit operator StringField(string value) { StringField sf = new StringField(); sf.Value = value; return sf; } public override string ToString() { return Value; } public override bool Equals(object obj) { if (obj == null || !(obj is StringField)) return false; return 0 == string.Compare(Value, (obj as StringField).Value); } public override int GetHashCode() { return Value.GetHashCode(); } } 
+1
source share

You can have a StringField by matching the Name property in the Name.Value field inside your class.

so that you can define the Name property as follows:

 string Name { get { return _name.Value; } set { _name.Value = value; } } 

Here _name is your StringField variable.

0
source share

The assignment operator cannot be overridden in C #. However, you may have a property for type conversion for you and set that class

0
source share

All Articles