Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Automatically assign property based on other property values

Tags:

If I have some types e.g:

public class SomeType //Generated by some codegen i won't to change that. {     string C { get; set; }         }  public class AnotherType : SomeType {     string A { get; set; }     string B { get; set; } } 

Is it possible to assign property C automatically? For example when properties A and B get assigned or when I am Casting this type to some another type, or somehow else?

Basically for example I want to execute some logic to automatically assign property C according to values A and B at some point when properties values A and B get populated.

Is there any another ways of doing that rather than using standard properties?

I was thinking that it is possible to do some king of magic when i may cast type AnotherType to SomeType, but i cant implement implicit operator where i may put this conversion logic "from A+B to C" because compiler doesn't allow implicit operator for related types.

Now Only way i see it is remove inheritance and implement implicit operator for AnotherType to SomeType conversion, but the evil in that case i need to duplicate all properties of type SomeType within type AnotherType and i need to change type AnotherType manually every time when SomeType getting changed.

like image 579
angularrocks.com Avatar asked Aug 29 '10 08:08

angularrocks.com


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

This is possible using auto-implemented properties. You could use the setter of B to assign a value to C:

public class SomeType {     public string A { get; set; }     public string C { get; set; }      private string _b;     public string B      {          get { return _b; }          set          {              // Set B to some new value             _b = value;               // Assign C             C = string.Format("B has been set to {0}", value);         }     } } 
like image 54
Darin Dimitrov Avatar answered Sep 29 '22 22:09

Darin Dimitrov


Do you want to be able to set C, or just get it? If you don't need to be able to set the value, then I think you want this:

public class MyClass {     private string _a = "not set";     private string _b = "not set";      public string A     {         get { return _a; }         set { _a = value; }     }      public string B     {         get { return _b; }         set { _b = value; }     }      public string C     {         get         {             if (_a != "not set" && _b != "not set")                 return "not set";             else                 return "set";         }     } } 

Here's a simpler example of accessing a property that is dependent on another property:

public class MyClass {     private double[] MyArray = new double[5];      public int NumElements     {         get         {             return MyArray.Length;         }     } } 
like image 25
gkrogers Avatar answered Sep 30 '22 00:09

gkrogers