Member initialisation in c# - c#

I have two classes, class A and class B.
public class A
{
public int someNumber;
public A(int a)
{
someNumber = a;
}
}
Now class B has a field that is an object of class A. In C++ it is possible to do this:
public class B
{
public A foo;
public B(int a) : foo(a) { }
}
But this doesnt work in C#. So how can one solve this problem in C# without using a default constructor in class A. To be more precise, how is it possible to write a constructor for class B that takes as parameter the someNumber value of foo?

You could try something like this:
public class B
{
public A thing;
public B(int a)
{
thing = new A(a);
}
}

Related

Initialize parent class array member in child class in C#

Is it possible in C# to write something like this that actually works (this compiles and runs, but doesn't work properly)?
public abstract class Foo
{
public int[] a;
}
public class Bar : Foo
{
public int[] a = new int[123];
}
Or do I have to use this (works fine, but doesn't seem as nice as the above code)?
public abstract class Foo
{
public int[] a;
public void Init(int size)
{
a = new int[size];
}
}
public class Bar : Foo
{
public Bar()
{
Init(123);
}
}
Based on your comment above:
The intention is to have the child class set the array size.
It sounds like what you're looking for is called a constructor. You would use it to define the required values for creating an instance of an object, and use those values to build the object.
Your base class would require the value, something like this:
public abstract class Foo
{
public int[] a;
public Foo(int size)
{
a = new int[size];
}
}
So now one needs to supply the value for the constructor. The child class can supply the value from its own constructor easily using constructor chaining:
public class Bar : Foo
{
public Bar(int size) : base(size) { }
}
The Bar.a hides inherited base member (Foo.a). To make this works properly, firstly, you need to change your base member from field to property and secondly declare it as abstract and then override it in your derived class. Something like this:
public abstract class Foo
{
public abstract int[] a { get; set; }
}
public class Bar : Foo
{
public override int[] a { get; set; }
}
Or if you only want to have the child class set the array size use constructor in your derived class like this:
public abstract class Foo
{
public int[] a;
}
public class Bar : Foo
{
public Bar()
{
a = new int[123];
}
}

c# get only child properties without parent class properties

Is there a way, and not using reflection, of elegant get only child propeties of an object?
For example:
class A
{
public string PropA;
}
class B : A
{
public string PropB;
}
class C
{
var classB_instance = new B();
/* Only class B properties without parent so B.PropB; but no B.PropA;
}
I know it would be possible with reflection, but if this can be avoided?
You could create a specific interface for your inherited class like say
interface ISpecificB {
string PropB;
}
and then Create your class like
public class A {
public string PropA;
}
public class B: A, ISpecificB {
public string PropB;
}
and only make the variable as specific as ISpecificB when creating it or returning it from a function
ISpecificB classB = new B();
classB.PropA // shouldn't be available
However, classB could still be casted as B or A which would give access to the propA and it might increase complexity in your solution
Whether you can do this way ?
class A
{
private string PropA;
}
class B : A
{
public string PropB;
}
class C
{
var classB_instance = new B();
}
You could mark PropA as private, look at https://msdn.microsoft.com/en-us/library/ms173121.aspx:
private
The type or member can be accessed only by code in the same class or struct.
just a short note: most of the time, I use reflection to do exactly the opposite: access things I am not allowed, for example, because they are private... ;-) reflection is not a "tool" to hide something, AFAIK. it opens every door which is usually locked ;-)
You can use the protected accessibility modifier:
The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
public class A
{
protected string PropA { get; set; }
}
public class B : A
{
public string PropB { get; set; }
}
public class C
{
var classB_instance = new B();
//You can't access classB_instance.PropA
}
Declare variable PropA of Class A as private variable(as show in below code):
class A
{
private string PropA;
}

Call inner class in c#?

I have an outer class with an inner class like this :
class A
{
public class B
{
public int number;
}
}
I think inner class is useful because I can call my "number" field like this :
A.B.number = X; but I can't call it this shape !
I create an instance from A, for example => A a = new A();
I want to access B by a instance directly => a.B.number;
but I can't.
I know if I create a new from B; I can access it, But I want to know how I can call my field in this shape => A.B.number NOT b.number
in brief, how I can access B class by call A class. (not directly B)
Instead of putting the B class description in A you can just put a property in A that is of type B.
public class B
{
public int number;
}
public class A
{
public A()
{
MyB = new B();
}
public B MyB { get; private set; }
}
Then you can do the following
A myA = new A();
int num = myA.MyB.number;
Though I would suggest also making number in B a property as well.
An inner class is just a class, and if you want to refer to its properties you need to have an object of that.
I think,you should make an object of B.
class A
{
public B b = new B();
public class B
{
public int number;
}
}
I wrote this answer to take experts opinion on it.
class A
{
public B b{ get; private set; }
public A()
{
b= new B();
}
public class B
{
public int number;
}
}
A a = new A();
Now you can access
a.b.number;
Class A will not automatically instantiate an instance of class B. You would need to create a property that returned an instance of B.
For example
public class A {
public A() {
this.MyB = new B()
}
public B MyB {get; set;}
}
You could then delegate to B if you wanted to get a property of b directly.
public int BNumber
{
get
{
return MyB.number;
}
}
With that said, why do you want a nested class here? Class B doesn't need to be nested in A for A to have a property of type B. I think you may be conflating class definition with property definition.

Derived member object in a derived class

I have the following classes:
public class A {
public C mObj:
}
public class B : A {
}
public class C {
public int val1;
}
public class D : C {
public int val2;
}
I initialize all the class B instances in a way that inst.mObj = new D();. When I have an instance of class B I would ideally like to access all the members of class D by using mObj, but due to inheritance I can not do it without casting into D first.
I would like class B to have a member object of class D, but I automatically inherit a member from class C. Is there a way to achieve something like that? If not then how is it usually done when a similar structure is required?
It is difficult to determine your exact requirements, but you could try to use generics with type constraints:
public class A<T>
where T : C
{
public T mObj:
}
public class B : A<D>
{
}
public class C
{
public int val1;
}
public class D : C
{
public int val2;
}
In this case the mObj in B will be of type D, so no conversion will be required.

In a private class : access to a member of the "outer class"?

Here is my code (just a snippet to expose the problem) :
public class A
{
class B
{
//private class
}
public int nb;
}
Im tired but why can't I access to "nb" in my private class ?
You're gonna need an instance of A in order to access the instance member nb:
public class A
{
class B
{
public B()
{
A a = new A();
int nb = a.nb;
}
}
public int nb;
}
It's possible in java but not in C#.
You need to pass an instance of A to B.
In C# an 'outer' class is just a 'namespace' to the inner class. So the outer class is not being instantiated.
You need to pass an instance of A to B, like so:
public class A
{
class B
{
private A _outerClass;
public B(A outerClass)
{
_outerClass = outerClass;
// Then you can access nb thus:
_outerClass.nb;
}
}
public int nb;
}

Categories