c# Get value of static property from the instance of a class - c#

How do I get the value of a static property from an instance of a class? see example below...
abstract class A {
public static double Foo {get; protected set;}
}
class B : A {
static B(){
Foo = 1;
}
}
class C : A {
static C(){
Food = 2;
}
}
class Test {
A test = new B();
//How do I get test.Foo ??
}

Static members (fields, properties, methods, etc.) are accessed via class name.
var x = A.Foo;
I think you are getting statics wrong.
Did you mean something like this?
abstract class A {
public double Foo {get; protected set;}
}
class B : A {
public B(){
Foo = 1;
}
}
class C : A {
public C(){
Foo = 2;
}
}
class Test {
A test = new B();
var foo = test.Foo;
}

Related

Instanciate class A with values from instantiated class B when A and B inherit from same class (C#)

Say I have the following
public class A{
public string foo {get;set;}
public string bar {get;set;}
public int baz {get;set;}
}
public class B:A
{
public string wuz {get;set;}
}
public class C:A
{
public int yuh {get;set;}
}
If I then make an instance of B
var b = new B(){wuz="wow",foo="hello",bar="wordl",baz=18};
and later on I want to use those values of B in C, then I have to manually write
c = new C(){yuh=20, foo = b.foo, bar = b.bar, baz=b.baz};
isn't there a way to make c take the already instantiated values from B in a dynamic way, and parse them onto C? In this example it is easy to to, but say I have 500 properties then it's not feasible
(I'm fairly new to .NET thus there might be a typo/syntax error in the codeexample above, but I think the problem should be rather clear)
You can add a constructor to C that takes an instance of A (or if you really want to do that B). Would look something like this:
public class C:A
{
public C() {}
public C(A other)
{
this.foo = other.foo;
...
}
public int yuh {get;set;}
}
If you also want the same capability in B, you might want to add this kind of constructor to A, and call it from C:
public class A{
public A(){}
public A(A other)
{
this.foo = other.foo;
...
}
public string foo {get;set;}
public string bar {get;set;}
public int baz {get;set;}
}
public class C:A
{
public C(){}
public C(A other) : base(other) {}
public int yuh {get;set;}
}
In either case you can now create a new instance of Clike this:
c = new C(b){ yuh = 20};
Yes you can. You need to write method that will take B instance and sets all properties with reflection.
public C CreateCFromB(B bInstance)
{
var cInstance = new C();
foreach (var property in typeof(B).GetProperties())
{
var propertyValue = property.GetValue(bInstance, null);
property.SetValue(cInstance, propertyValue);
}
return cInstance;
}
You can take more generalized approach with generic types.
public TWhat CreateFrom<TFrom, TWhat>(TFrom tFromInstance)
where TWhat : TFrom, new()
{
var tWhatInstance = new TWhat();
foreach (var property in typeof(TFrom).GetProperties())
{
var propertyValue = property.GetValue(tFromInstance, null);
property.SetValue(tWhatInstance, propertyValue);
}
return tWhatInstance;
}

How to set all properties of base class through derived class

public BaseClass
{
prop abc;
prop pqr;
prop xyz;
}
public DerivedClass : BaseClass
{
prop New1;
prop New2;
//constructor
public DerivedClass(BaseClass baseObj,someMore params)
{
this.abc = baseObj.abc;
this.pqr = baseObj.pqr;
this.xyz = baseObj.xyz;
/* I do not want to do this for each and every property as I have more than 40 properties */
}
}
Here in above code How I can set all properties of derived class which are same in derived class as base class.
In my derived class
Can I do somethign of following type to achieve above thing without using Automapper or Reflection
public DerivedClass(BaseClass baseObj,someMore params):base(baseObj) //or something similar
{
}
You could initialize the properties in your base class instead of the derived class (since both the parameter and the base classes share the same type of class).
To copy the properties, besides AutoMapper (which I find really slow) and reflection (which is hard to implement and maintain), an easy, very efficient way to do this is using the open source library Omu.ValueInjecter:
Install the library from NuGet:
Install-Package ValueInjecter -Version 3.1.3
Then, use it in your constructor as follows:
using Omu.ValueInjecter;
public abstract class BaseClass
{
protected BaseClass(BaseClass baseObj)
{
this.InjectFrom(baseObj);
}
public string Abc { get; set; }
public int Pqr { get; set; }
public object Xyz { get; set; }
}
public class DerivedClass : BaseClass
{
public DerivedClass(BaseClass baseObj, int new1, object new2) : base(baseObj)
{
New1 = new1;
New2 = new2;
}
public int New1 { get; set; }
public object New2 { get; set; }
}
The first time you run this code, it will create a mapper and store it in memory so the following times you perform the same operation will be much faster.
If you don't want to use value injecter or any other library, simply set the properties manually in your base class.
public abstract class BaseClass
{
protected BaseClass(BaseClass baseObj)
{
Abc = baseObj.Abc;
Pqr = baseObj.Pqr;
Xyz = baseObj.Xyz;
}
[...]
}
You can use prototype pattern. If you use C#, Please refer code as below:
public abstract class BaseClass
{
public int a;
public int b;
public int c;
public abstract BaseClass Clone();
}
public class DerivedClass : BaseClass
{
public int new1;
public int new2;
public override BaseClass Clone()
{
return this.MemberwiseClone() as BaseClass;
}
public override string ToString()
{
return string.Format("{0}{1}{2}{3}{4}", a, b, c, new1, new2);
}
}
class Program
{
static void Main(string[] args)
{
DerivedClass AClass = new DerivedClass();
AClass.a = 1;
AClass.b = 2;
AClass.c = 3;
DerivedClass BClass = AClass.Clone() as DerivedClass;
BClass.new1 = 4;
BClass.new2 = 5;
Console.WriteLine(BClass.ToString());
}
}
It comes from https://mobilechos.blogspot.com/2019/04/prototype-pattern-with-csharp.html.

C# multiple inheritance, using protected member

I have little problem using inheritance. I can neither change the value of first and second in class C nor first in class B when they are protected. If these variables are public everything works fine, but in this case what's the point of using protected?
class A
{
protected int first { get; set; }
}
class B : A
{
protected int second { get; set; }
public Show()
{
A a = new A();
a.first = 5;
}
}
class C : B
{
private int third { get; set; }
static void Main()
{
B b = new B();
b.first = 1;
b.second = 2;
}
}
The main problem is simply caused by you putting your program's entry point inside the class you want to test. Because Main() is static, you can't access C's (inherited) instance members.
So separate it:
class Program
{
static void Main()
{
C c = new C();
c.Test();
}
}
Your class C inherits from B, so C can access B's protected members like so:
class C : B
{
private int third { get; set; }
public void Test()
{
first = 1; // from A
second = 2; // from B
third = 3; // from C
}
}
By newing a B inside C, there's no relation between those instances of B and C, so all you can access there are B's public and internal members.
You're allowed to access the protected members when you're dealing with instances of your own class:
class B :A
{
protected int second { get; set; }
public show() {
this.first = 5; //This is valid
}
}
If you were allowed arbitrary access to your base class' protected members, on any instance of the base class, this would be allowed:
class DDefinitelyNotB : A
{
}
class B :A
{
protected int second { get; set; }
public show() {
A a = new DDefinitelyNotB ();
a.first = 5;
}
}
And that could be bad for DDefinitelyNotB, which isn't expecting other classes that just happen to derive from A to be able to interfere with the protected members it's inherited from A.

How to set class-property and re-use it on another class?

I got a class with inputs where I set values trough Class A. How can I access those property-values in Class B?
E.g.
namespace Example{
public class Inputs {
public string Something { get; set; }
}
Class A:
Inputs test = new Inputs();
test.Something = txtSomething.Text;
Class B:
//How do I access values I declared in class A, or did I do something wrong?
public class B{
public Inputs Test { get; set; }
}
var b = new B();
b.Test = new Inputs();
b.Test.Something = txtSomething.Text;
public class B{
public B(Inputs myB)
{ this.MyB = myB; }
public Inputs MyB { get; set; }
}
Inputs test = new Inputs();
test.Something = txtSomething.Text;
var b = new B(test );
Just give B a copy of test in its constructor.
public class A
{
private Inputs _test;
public A()
{
_test = new Inputs();
new B(_test);
}
}
public class B
{
private Inputs _input;
public B(Inputs input)
{
_input= input;
}
}
any changes to A._test.Somthing will also show up in B._input.Somthing.

How to access the instance variable value from the diffrrent class in C#

Creating a class with property a1
class A
{
public int a1 { get; set; }
}
Creating object for Class A in B and assigning value to it
class B
{
A a=new A();
a.a1=45;
}
How to get the the assigned value in different class.
class C
{
//How to access the 45 value from the class B instance variable here
//without using static keyword.
}
you could try
class A
{
public int a1 { get; set; }
}
class B
{
public A a = new A();
public B()
{
a.a1 = 45; //you need to put that in a method..
}
}
class C
{
B b = new B(); // instance of B in C
int aValue = b.a.a1; // access b's instance of A
}
better solution:
class A
{
public int a1 { get; set; }
}
class B
{
A a = new A();
public int A_Value
{
get { return a.a1; }
set { a.a1 = value; }
}
}
class C
{
B b = new B(); // instance of B in C
public C()
{
b.A_Value = 45;
}
}
Accept either an A or B as a parameter either in the method or constructor. In the case below, A and B are interchangeable.
public class C
{
A _a;
public C(A a)
{
_a = a;
}
void Do() // Using constructor parameter.
{
Console.WriteLine(_a.a1); // Should print 45, so long as your other code has already ran.
}
void Do(B b) // Using method parameter.
{
Console.WriteLine(b.A.a1); // will write 45
}
}
With respect, these answers just seem to be muddying the water. At risk of feeding a troll here, because it appears this question has no practical application, I'll submit my suggestion. Aside from some basic conventions, I'd added public constructors for each class, applied a public getter for the instance of A stored in B, and then provided a method on C that returns the "A1" property from the instance of A within the instance of B. Having written that last sentence punctuates how convoluted this task has been.
public class A
{
public A() { }
public int A1 { get; set; }
}
public class B
{
public B()
{
this._a = new A() { A1 = 42 };
}
private A _a;
public A A
{
get { return _a; }
}
}
public class C
{
public C() { }
public int GetA1FromA()
{
return new B().A.A1;
}
}

Categories