C# Set from another class and Get from another class - c#

This's Class A
Class A
{
public string uname { get; set; }
public string fname { get; set; }
}
I set values by Class B
Class B
{
private void Main(){
A aGetSet = new A();
aGetSet.uname = "James";
aGetSet.fname = "Blunt";
}
}
But when I get values in Class C, it's always return null
Class C
{
private void Main() {
A aGetSet = new A();
string username = aGetSet.uname;
string fistname = aGetSet.fname;
}
}
Does anyone has solution for this problem?

The aGetSet declared in B is an object of A. The aGetSet declared in C is another object of A. They are completely independent of each other. Changing the values of one of the objects does not affect the values of the other.
To fix this problem, you need to make it so that you are accessing the same instance in B and C.
There are lots of ways to do this. I will show you how to use the singleton pattern.
class A
{
public string uname { get; set; }
public string fname { get; set; }
private A() {} // mark this private so that no other instances of A can be created
public static readonly A Instance = new A();
}
class B
{
public void Main(){
// here we are setting A.Instance, which is the only instance there is
A.Instance.uname = "James";
A.Instance.fname = "Blunt";
}
}
class C
{
public void Main() {
B b = new B();
b.Main();
string username = A.Instance.uname;
string fistname = A.Instance.fname;
}
}
Now you just need to call C.Main to make this work!

Your have 2 different objects in 2 classes. When you are using '= new A() ' it creates new instance.
The reason why you are getting null here:
string username = aGetSet.uname;
is default value for string type (as any reference type) is null.
To pass 'the same' object from class B into class C Main method change method in class C to public Main(ref A obj). That will not create a copy and use the same instance.
Call from class B:
A aObj = new A();
aGetSet.uname = "James";
aGetSet.fname = "Blunt";
C c = new C();
c.Main(ref aObj);

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;
}

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;
}
}

Assign Properties from Method

I have two class, base and inherited class like these
public class A
{
public string Prop1 { get; set; }
}
public class B : A
{
public string Prop2 { get; set; }
}
Now i have a method which return class A after populating properties values
public A GetPopulatedValues()
{
//code to populate values here
}
Now my question is, how to assign all the property values at once?
This works
public void Init()
{
var obj = new B();
obj.Prop1 = GetPopulatedValues().Prop1;
}
But i want something like this
public void Init()
{
var obj = new B();
obj = GetPopulatedValues();
}
Any ideas?
Create a constructor:
public Foo(string prop1, string prop2)
{
Prop1 = prop1;
Prop2 = prop2;
}
Or a copy constructor:
public Foo(Foo other)
{
Prop1 = other.Prop1;
Prop2 = other.Prop2;
}
Or use an object initializer:
var foo = new Foo { Prop1 = "Hello", Prop2 = "World" };
But as #RudiVisser says, you'll actually want to fix this in the GetPopulatedValues() method, by supplying a reference to the object you want to populate:
static class FooPopulator
{
public static void PopulateValues(Foo foo)
{
foo.Prop1 = "Hello";
foo.Prop2 = "World";
}
}
var foo = new Foo();
FooPopulator.PopulateValues(foo);
Now your foo object is usable and populatable, without having to alter the Foo class. Added bonus: when you add a property to populate (say Prop3), you will only have to alter the PopulateValues() method.
In your code you also have to do that, but then you'd need to also add the Prop3 assignment, which, if forgotten, will cause bugs.
I agree with #CodeCaster that this should probably be done with a constructor modification, but just to provide another alternative - why not make your GetPopulatedValues method accept an instance of A, and populate the values directly.
public void GetPopulatedValues(A instance)
{
instance.Prop1 = (whatever);
}
public void Init()
{
var obj = new B();
GetPopulatedValues(obj);
}

Categories