C# Access a method from one class, and not another - c#

and sorry in advance if this question has been solved previously.
I am creating a small library in C# and am hoping to have class A be able to modify the data members of class B, where class A and B exist in the same namespace, C.D. This is not a problem to accomplish though I would like class E, in namespace C, to not be able to access the data members of B.
namespace C.D
{
class B
{
modifier int
a,
b;
public B()
{
}
}
class A
{
public A() {}
public B DoStuff()
{
B b = new B();
b.a = 1; b.b = 2;
return b;
}
}
}
namespace C
{
class E
{
static void Main(String[] args)
{
A a = new A();
B b = a.DoStuff();
}
}
}
In my main method above I would like every class in the namespace C.D to be able to alter the data members of a class B object though nothing outside of the C.D namespace to be able to modify class B object data members.
Is there any way to do this by changing the namespace structure, modifiers, or implementing specific design patterns?
Thank you all in advance. : )

You can use the "internal" access modifier if you're willing to move the relevant classes in C.D to a separate assembly.
namespace C.D
{
class B
{
internal int a;
internal int b;
public B()
{
}
}
}

One way to achieve this is by:
Moving Class A's definition into Class B (nesting it within class B { ... })
Making Class B's fields (a and b) private.
i.e.
namespace C.D
{
class B
{
private int a, b;
public B()
{
}
class A
{
public A() { }
public B DoStuff()
{
B b = new B();
b.a = 1; b.b = 2;
return b;
}
}
}
}
namespace C
{
class E
{
static void Main(String[] args)
{
A a = new A();
B b = a.DoStuff();
}
}
}
Another way to achieve this is to make Class B's fields protected, then make all classes that should have access to Class B's fields derive from Class B. This way you won't need to nest Class A inside Class B. i.e.
namespace C.D
{
class B
{
protected int a, b;
public B()
{
}
}
class A : B
{
public A() {}
public B DoStuff()
{
B b = new B();
b.a = 1; b.b = 2;
return b;
}
}
}
namespace C
{
class E
{
static void Main(String[] args)
{
A a = new A();
B b = a.DoStuff();
}
}
}

Related

c# Accessing Creation Class Variables From Instance

Just a basic programming question.
public class ClassA
{
int i = 10;
void Start()
{
ClassB b = new ClassB(this);
b.DoSomething();
}
}
public class ClassB
{
ClassA a;
public ClassB(ClassA a)
{
this.a = a;
}
void DoSomething()
{
Console.WriteLine(a.i);
}
}
I would really like to omit the a:
Console.WriteLine(a.i);
->
Console.WriteLine(i);
What is the most reasonable method of achieving this?
(Note: ClassB must not inherit from ClassA, as ClassA inherits from something ClassB cannot. And I suppose I should say I don't want to pass parameters to the functions, so DoSomething(i) is not applicable.)
You can create a property. Please note that a.i still needs to be public for both your example and mine.
public class ClassB
{
private ClassA a;
public ClassB(ClassA a)
{
this.a = a;
}
public int i { get { return a.i; } }
void DoSomething()
{
Console.WriteLine(i);
}
}

Callback function in c# - WPF

I have class A which define class B:
B b=new B()
I would like to call to a function in class A from B.when I tried to make this function- static- i got an error cause i have in that function-
Dispatcher.BeginInvoke...
Is there another way to do it?
Why not just pass a reference of A to B.
Something like
public class A
{
public A()
{
B b = new B(this);
}
}
public class B
{
public B(A a)
{
}
}
Or you could make it a property of B.
Something like
public class A
{
public A()
{
B b = new B
{
MyA = this
};
}
}
public class B
{
public A MyA;
}

How inheritance will work with different class objects

Here is an Inheritance question. I was trying to understand TAG1 to TAG3 Process. what exactly going happen and hold which class reference. Looking forward to your suggestion.
static void Main(string[] args)
{
B b = new B(); **// What Happens here TAG1**
A a = b; **//What Happens here TAG2**
B x = new A() as B; **//what happens here TAG3**
a.F();
a.G();
a.H();
a.Z();
b.F();
b.G();
b.H();
b.Z();
x.F();
Console.ReadLine();
}
public class A
{
public void F() { Console.WriteLine("A.F"); }
public virtual void G() { Console.WriteLine("A.G"); }
public virtual void H() { Console.WriteLine("A.H"); }
public void Z() { Console.WriteLine("A.Z"); }
}
public class B : A
{
new public void F() { Console.WriteLine("B.F"); }
public override void G() { Console.WriteLine("B.G"); }
new public void H() { Console.WriteLine("B.H"); }
}
B b = new B(); **// What Happens here TAG1**
An instance of B is created here, and b holds a reference to it.
A a = b; **//What Happens here TAG2**
a is assigned that b instance, which means you can access the A part of the previously crated B object.
B x = new A() as B; **//what happens here TAG3**
You create an A object and cast it to B, but the cast is gonna fail and return null. Therefore x will not reference the instance of an object.

Class nesting and access modifiers

I have following piece of code:
class A
{
public C GetC()
{
return new C();
}
}
class B
{
//has access to A but can not create C. Must ask A to create C.
private void method()
{
A a = new A();
C c = a.GetC();//Ok!
C c2 = new C();//Not allowed.
}
}
class C
{
}
What access modifiers should be used on C so it is ONLY accessible through A? (only class A knows how to properly initialize class C)
Or is there a better solution?
If you make A a nested class in C it should work.
public class B
{
//has access to A but can not create C. Must ask A to create C.
private void method()
{
var a = new C.A();
var c = a.GetC();//Ok!
var c2 = new C();//Not allowed.
}
}
public class C
{
private C()
{
}
public class A
{
public C GetC()
{
return new C();
}
}
}
Inherit A from C, and make Constructor of C protected
EDIT -- "as protected member cannot be accessed via qualifier ", error is coming, as a work around, static member is introduced which will return the instance. This protected member is accessible from derived.
class A : C
{
private C GetC()
{
C c = C.GetC();
return c;
}
}
class C
{
protected C()
{
}
protected static C GetC()
{
return new C();
}
}
Proposed method Inherit A from C, and make Constructor of C protected code doesn't work because in it are some errors. Adjusted this method code is below:
class B
{
static void Main(string[] args)
{
A a = new A();
C c = a.GetC();
C c2 = C(); //Non-invocable member 'C' cannot be used like a method
}
}
class A : C
{
public new C GetC()
{
C c = C.GetC();
return c;
}
}
class C
{
protected C()
{
}
protected static C GetC()
{
return new C();
}
}

OpCodes.Castclass. Is it necessary?

Is it necessary to emit OpCode.CastClass(typeof(A)) when you having a reference to instance of (B) on top of stack, where B is class, derived from A, when preparing for a call to method with argument of type A?
Addition:
interface IFoo
{
void IFoo();
}
public class A:IFoo
{
public void IFoo()
{
}
}
public class B:A,IFoo
{
new public void IFoo()
{
}
}
var b = new B();
(b as IFoo).Foo();
((b as A) as IFoo).Foo();
I guess you have something like this:
class A
{
public void Foo() { }
}
class B : A
{
}
and need to decide between:
B b = new B();
b.Foo();
and
B b = new B();
((A)b).Foo();
Both work. But the cast is not necessary, because B inherits all members from A.

Categories