How to reorganize this?

I tried to reorganize this

class AClass { string Property1 { get; set; } string Property2 { get; set; } string Property3 { get; set; } void AMethod(AClass other) { if(String.IsNullOrEmpty(this.Property1)) { this.Property1 = other.Property1; } if(String.IsNullOrEmpty(this.Property2)) { this.Property2 = other.Property2; } if(String.IsNullOrEmpty(this.Property3)) { this.Property3 = other.Property3; } } } 

And the only thing I could come up with was

  private string GetFirstNotNullOrEmpty(string first, string second) { if (String.IsNullOrEmpty(first)) { return second; } return first; } 

and

  this.Property1 = GetFirstNotNullOrEmpty(this.Property1, other.Property1); 

This is not entirely equivalent, but will do the job. Is there a better way to reorganize this?

+4
source share
6 answers

If you are going to do this for the string N properties of this class, you must implement this using Reflection .

Update

It's all about the "code", right? Here it is:

 class SomeClass { public string Property0 { get; set; } public string Property1 { get; set; } public string Property2 { get; set; } public string Property3 { get; set; } public string Property4 { get; set; } public string Property5 { get; set; } public string Property6 { get; set; } public string Property7 { get; set; } public string Property8 { get; set; } public string Property9 { get; set; } public override string ToString() { //just to print out all properties and values foreach (PropertyInfo prop in typeof(SomeClass).GetProperties()) { Console.WriteLine(prop.Name + "," + prop.PropertyType + " = " + prop.GetValue(this, null)); } return base.ToString(); } public void CopyStringPropertiesIfEmptyFrom(SomeClass SourceInstance) { foreach (PropertyInfo prop in typeof(SomeClass).GetProperties()) { if (prop.PropertyType == typeof(System.String) && String.IsNullOrEmpty((string)prop.GetValue(this, null))) { prop.SetValue(this, prop.GetValue(SourceInstance, null), null); } } } } 
+4
source

Instead of using the method, you can collapse ifs into triple operators:

 this.Property1 = String.IsNullOrEmpty(this.Property1)? other.Property1 : this.Property1; 
+2
source

implement verification of the properties themselves.

 public class AClass { string Property1 { get { return _Property1; } set { if (String.IsNullOrEmpty(_Property1)) { _Property1 = value } } } private string _Property1; void AMethod(AClass other) { this.Property1 = other.Property1;// Property can only be set once. } } 
+1
source

I'm not a fan of using Reflection when I can avoid it, so I really like your suggested option in the question, but mix it a bit with Tesserex:

 private string GetFirstNotNullOrEmpty(string first, string second) { return String.IsNullOrEmpty(first)) ? second : first; } 
0
source

I think the best solution would be something like

 private void SetFirstNotNullOrEmpty(string first, string second, Action<T> setter) { if (String.IsNullOrEmpty(first)) { setter(second); } } 

and it will be called like this:

 this.Property1 = GetFirstNotNullOrEmpty(this.Property1, other.Property1, i => this.Property1 = i); 

That would be better if it weren't for C # properties. With public fields, I could pass the link and have both getter and setter in one parameter.

0
source

The first thing to reorganize here is non-intuitive names such as Property1 and AClass. Use meaningful names for class and property names so that they clearly reflect intent.

Perhaps the OP wants us to focus on the problem, not on this aspect.

0
source

Source: https://habr.com/ru/post/1347964/


All Articles