0

Let's say I have 3 classes:

Main class, class A, class B

and let's also say that class B has a method which needs data from class A. (the data is actually a dataview object).

objects instances of class A and class B are created in the main class regardless of the above (for them to be created in general)

what would be the best way to reach the dataview object of class A from class B?

Should I make the dataview object internal? should I make it static? should I make it internal static?

I've learned to create properties in cases like these and just "get" the object, but since it's the main form (and not Class B) which creates an object of Class A, I can't reach that property from class B.


Edit, here is a demo code:

Class Main
{

A a = new A();
B b = new B();

a.doingAClassStuff(); //Not possible unless Class A is set to public or the method is set to internal (classes are internal by default)
b.doingBClassStuff(); //Likewise

//need to get the dv object here.
//dv.rows[1].foo = bar;
}

Here above in the main class I need to get the dv object from Class B.

//each class exist in a different class file (.cs file) inside the project
Class A 
{
doingAClassStuff()
{
MessageBox.Show("Hello From Class A!");
}
//here access to the dv object is also needed.
//dv.rows[5].foo = something;
}

Class A also needs to get the dv object of Class B!

//in another file as well
Class B 
{
DataView dv = new Dataview(datatable1)

doingBClassStuff()
{
MessageBox.Show("Hello From Class B!");
}
}

My notes:

  1. Making "dv" only "Internal" will not make it visible.

  2. Making "dv" "Internal Static" will make it visible and I can work with that but I am not sure "static" has no Cons, I am not creating more than 1 instance of each class (if it matters).

  3. I tried setting properties with "get" and get it but since the properties are public and I couldn't set them to "internal" are they public to outside of the assembly as well?

Many Thanks!

Ray
  • 407
  • 1
  • 3
  • 15

3 Answers3

1

Without any additional information in regards to your requirements and restrictions, this is how I would handle it.

If you need Class A for a Class B to operate properly, you should probably provide Class B with a Class A member and either initialize it in the constructor for Class B or provide a method to set the member.

Class A should then expose through its public interface what Class B needs in a manner that makes sense for the class. i.e. don't just make the DataView public.

public class A
{
    private B _b;

    public A(B b)
    {
        _b = b;
    }

    void UseData()
    {
        _b.GetData();
    }
}

Public class B
{
    public DataView GetData();
}

elsewhere

B b = new B();
A a = new A(b);
Taekahn
  • 1,592
  • 1
  • 13
  • 16
  • Thanks, I am trying to understand what you've offered, it looks a little too complicated for me and I was thinking there is a simpler solution.. I've edited the question and added code if that helps.. Thanks again! – Ray Jul 15 '15 at 19:53
  • @Ray I updated my answer with an example of what I was talking about. It sounds more complicated than it really is. – Taekahn Jul 15 '15 at 20:02
  • Interesting, I didn't know it's possible, but I think that if I make copies of the object by using properties, I might read a copy of it in a time just before the original object has been changed by another class and therefor it will not be what it should've been. Second, I don't think you need to set the classes "public" since they suppose to be internal (by default) and mostly I am not sure about the "public" of the method in Class B, since it wont work if the class will not be public as well, and of the class would be internal, the properties will not work, I've tried before.. Thanks! – Ray Jul 15 '15 at 20:20
  • That is if I am right with everything I am saying here.. maybe setting it `internal static` is the best solution.. – Ray Jul 15 '15 at 20:25
  • @Ray I'll leave the access levels up to you to work out since It depends on your specific needs. That being said, in my experience, most of your classes are fine being public unless you happen to making libraries for other people to use. I can't remember the last time I marked a class as internal. As to making a copy, its not making a copy, its a reference to the other instance, so if that gets updated, `a` will see any changes to `b`. – Taekahn Jul 15 '15 at 20:34
  • Thanks a lot, I've learned something new! I know that if you set the classes to `pubic` then an outsider could see your code etc', when you don't set it to anything it's `internal` (you don't have to set it to internal it is internal by default so it's not accessible from outside of your own assembly but visible to other classes in your project. as for the reference of the object, very good to know! thanks a lot again. – Ray Jul 15 '15 at 20:41
0

I can't add a comment yet but this seems similar to the following post which asks for an equivalent to friend. The top answer says to use InternalsVisibleTo to achieve somewhat similar functionality.

What is the C# equivalent of friend?

Community
  • 1
  • 1
Dan
  • 383
  • 1
  • 4
  • I don't think it's the solution I am looking for.. specifying an 'Internal' access modifier to a variable in a class is sufficient. that exposes that variable or field to all classes in the assembly and blocks outside assemblies from out assembly. If I'm not wrong 'InternalsVisibleTo' means that you can specify if you'd like to make your assembly visible to another specific external assembly. – Ray Jul 15 '15 at 17:54
  • Sorry about that. I would have added this as a comment but I lack the required reputation. Goodluck! – Dan Jul 15 '15 at 17:58
  • No Prob.. Thanks for trying :) – Ray Jul 15 '15 at 19:04
0

what would be the best way to reach the dataview object of class A from class B?

Some options:

  • Pass an instance of A to a method of an instance of B
  • Add a property to B of type A (if the instance would be used for multiple operations

Should I make the dataview object internal? should I make it static? should I make it internal static?

  • Make it internal if you want it accessible to all types within the same assembly but not outside of the assembly (basically private to the assembly

  • Make it public if you want it available to all types inside and outside the assembly

  • Make it static if the data would apply to all instances of A

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • @D Stanley Thanks! I see what you say, I am not sure I know how to do the first two things you've offered, I'll sit on it.. I've also edited the question and added code if that helps to help me more.. Thanks again! – Ray Jul 15 '15 at 19:55