access for that protected variables out side the assembly

Posted by krrishbiju-15589 under C# on 9/27/2013 | Points: 10 | Views : 992 | Status : [Member] | Replies : 3
Hi ,
I i declare a variable with protected access modifiers.
Can we get access for that protected variables out side the assembly.
For Eg:
//ClassLibrary1.DLL
public class Class2
{
protected int Addprotected(int id)
{
return id + 1;
}
}

//refering ClassLibrary.DLL dll in ClassLibrary2 dll a
public class ClassB:ClassLibrary1.Class2
{
public int Addmeth()
{
this.Addprotected(1);// Can this work ?

}
}

Regards
krrish




Responses

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Protected members will be accessible in derived classes...

You will need to either create a public setter for the protected field or inherit from the class.

public class A
{
protected int x;

public int X { set { x = value; } }
}

public static A CreateClassA()
{
A x = new A();
x.X = 5;
return x;
}
Or:

public class B : A
{
public static A CreateClassA()
{
this.x = 5;
return x;
}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
namespace Foo {
public class Base {
protected internal void M() { ... }
}
}

Many people believe that this means " M is accessible to all derived classes that are in this assembly." It does not. It actually means "M is accessible to all derived classes and to all classes in this assembly". That is, it is the less restrictive combination, not the more restrictive combination.



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Krrishbiju,

You code is perfectly current but one thing you have missed is return statement.

return  this.Addprotected(1);


You can see the below is you complete code:-

namespace ClassLibrary1.DLL
{
public class Class2
{
protected int Addprotected(int id)
{
return id + 1;
}
}
}

namespace ClassLibrary2.DLL
{
public class ClassB : ClassLibrary1.DLL.Class2
{
public int Addmeth()
{
return this.Addprotected(1);
}
}
}

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
ClassLibrary2.DLL.ClassB obj = new ClassLibrary2.DLL.ClassB();
Console.WriteLine(obj.Addmeth());
Console.ReadLine();
}
}
}



Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response