tags:

views:

170

answers:

4

how do i acces the property value from an internal class , see below?

namespace N1
{
    public class ClassA
    {
       string var1 = null;
       private ClassB  b;
       public ClassA()
       {
          var1 = "one";
          b = new ClassB();
       }
       //property 
       public string Var1
       {
          get{ return var1; }
       }
}

namespace N1
{
   internal class ClassB  
   {
     private void method()
     {
      // I need to access the value of Var1( property) from here, how to do this?
     }
   }
}
A: 

You can't. Change access modifiers of either class A or B. Purpose of internal class is to contain some internal logic implementation, and if you have need to access public classes' fields from it, probabli something wrong with app design

Gopher
I think comments on down-votes must be made mandatory - but I digress. I did not down-vote though but I am wondering if you meant nested classes
Asher
A: 

Well there are a couple of ways:

  1. Change the access modifier of ClassB.Method to be public and make it take a string parameter.
  2. Update the constructor of ClassB to take a string parameter and store it in a private field.
  3. Add a public string property to ClassB.

Making a class internal just means the class is only available with files inside the same assembly.

James
My Idea of making class B internal is that the client tht uses assebmbly Should only access the properties and methods of Class A. Client should not be allowed to access anything in Class B
@starz26: To make `ClassB` only accessible to `ClassA` internally, make it a private class in `ClassA`. To make it accessible to any other classes in the assembly, but not accessible outside it just mark it as `internal`.
James
+1  A: 

Pass an instance of ClassA into ClassB's constructor:

namespace N1
{
   internal class ClassB  
   {
     private ClassA _classAInstance;

     public void ClassB(ClassA classAInstance)
     {
         _classAInstance = classAInstance;
     }

     private void method()
     {
       // You can access _classAInstance properties here
     }

   }
}

Update: I missed that a ClassB instance b was a private member on ClassA. Using my previous answer, you can just instantiate b in ClassA's constructor:

   public ClassA()
   {
      var1 = "one";
      b = new ClassB(this);
   }
Jamie Ide
If its to access the value of 1 property I would probably advise against taking in the full class.
James
@Jamie: I was thinking in the lines of instantiating the class inside the method. Cool implementation.
Raja
@James -- That works too but doesn't provide a context. You could pass any string into the constructor; my answer limits ClassB's access to ClassA's properties and methods.
Jamie Ide
@Jamie: Valid point, however, ClassB is an internal field of ClassA so it can only really be allowed to input data from that class anyway (in this particular example).
James
@James -- That's not correct; internal means the class is accessible to all other files in the same assembly. See http://msdn.microsoft.com/en-us/library/7c5ka91b%28VS.80%29.aspx
Jamie Ide
@Jamie think you misunderstood my comment....ClassB *is* a private field of ClassA. I understand what the keyword **internal** means as I was the first on the post to actually point that out.
James
@James -- I apologize, I missed that ClassB was a private member in ClassA and you did indeed correctly first point out the meaning of internal. However, I think my point that passing a string to the constructor would allow any class in the same assembly to new up a ClassB with any string is still valid.
Jamie Ide
@Jamie: I did say you had a valid point in my previous comment. However, what I am trying to say is, *in this particular example* I feel passing the entire class in is unnecessary. Whose to say that `ClassB` is 100% on `ClassA.Property`?. It may be used in multiple classes (the OP hasn't detailed this). Which is why I feel a more generic solution i.e. accepting the property rather than the full class, is more appropriate. However, it is one of those things we could argue about all day, when it comes down to it its a matter of preference :)
James
thanks for the solution, It works for me.
A: 

You need a reference to an instance of Class A.

So either change Class B constructor to accept a reference to class A

namespace N1
{
    public class ClassA
    {
        string var1 = null;
        private ClassB b;
        public ClassA()
        {
            var1 = "one";
            b = new ClassB(this);
        }
        //property 
        public string Var1
        {
            get { return var1; }
        }
    }
}

namespace N1
{
   internal class ClassB  
   {
       ClassA classA;
       public ClassB(ClassA classARef)
       {
           classA = classARef;
       }

     private void method()
     {
      // I need to access the value of Var1( property) from here, how to do this?
         string myString = classA.Var1;
     }
   }
}

or make ClassB's private method() take in a string? private void method(string classAVar1)

or make ClassA static (haha)

Asher