Accessing Protected Variable From Outside Class

G

Guest

Hello

I have a protected variable in a class (Class A) that I need to call from
another class (Class B) to modify. I thought what I had to do was create a
public method in the class (Class A) containing the protected variable so
that the modification(s) can be done.

However, when I try this, I cannot see the public method from the second
class (Class B) unless it is declared static. If the method is static, then
using this.variable.attribute does not work.

Any suggestsions? I can change it from protected to private if need be.

Thanks for any and all help.
Ben

Sample of what i need done

Public Class A
{
protected int a;

public void main() {}

//method here to access and change the properties of a from outside
class(es)
public static void change(int b) {this.a = b;}

}

Public Class b
{
public void main()
{
namespace.A.change(5);
}

}
 
M

Mythran

Ben said:
Hello

I have a protected variable in a class (Class A) that I need to call from
another class (Class B) to modify. I thought what I had to do was create
a
public method in the class (Class A) containing the protected variable so
that the modification(s) can be done.

However, when I try this, I cannot see the public method from the second
class (Class B) unless it is declared static. If the method is static,
then
using this.variable.attribute does not work.

Any suggestsions? I can change it from protected to private if need be.

Thanks for any and all help.
Ben

Sample of what i need done

Public Class A
{
protected int a;

public void main() {}

//method here to access and change the properties of a from outside
class(es)
public static void change(int b) {this.a = b;}

}

Public Class b
{
public void main()
{
namespace.A.change(5);
}

}

Looks like what you want is a property, not a method... 2nd, why do you
have 2 entry points defined? I believe only 1 should be defined per
application....otherwise, when you run the assembly, how will it know which
to run?

public class A
{
private int mMyValue;
public int MyValue {
get { return mMyValue; }
set { mMyValue = value; }
}
}

public class B
{
public void DoIt()
{
A myClass = new A();
myClass.MyValue = 1234;
}
}

Note: You need an instance of class A in order to access any of it's
non-static members, properties, or methods.

HTH,
Mythran
 
J

Jon Skeet [C# MVP]

I have a protected variable in a class (Class A) that I need to call from
another class (Class B) to modify. I thought what I had to do was create a
public method in the class (Class A) containing the protected variable so
that the modification(s) can be done.

However, when I try this, I cannot see the public method from the second
class (Class B) unless it is declared static. If the method is static, then
using this.variable.attribute does not work.

Any suggestsions? I can change it from protected to private if need be.

You're not specifying (in class B) which instance of class A you want
to change the value for. Which instance did you mean to call Change()
on?
 
G

Guest

Thanks for the replies.

I tried to simplify the propblem for simplicity. I will try to give some
more information.

I have a class that builds a treeview control for an asp.net page. I have
another control that handles another frame on the pages. There should only
be one instance occuring with each class (per session).

If a user selects a control in the second frame, i need it to update the
first frame. (ie, i need to highlight a specific node in the treeview based
on what the users has selected).

The treeview control is protected in its own class, and i need to be able to
call a method that is able to access the current instance of the treeview
(only 1 exists in the session) and set the highlight property.

Does this help any with the problem?

Thanks for any help.
Ben
 
J

Jon Skeet [C# MVP]

I tried to simplify the propblem for simplicity. I will try to give some
more information.

I have a class that builds a treeview control for an asp.net page. I have
another control that handles another frame on the pages. There should only
be one instance occuring with each class (per session).

If a user selects a control in the second frame, i need it to update the
first frame. (ie, i need to highlight a specific node in the treeview based
on what the users has selected).

The treeview control is protected in its own class, and i need to be able to
call a method that is able to access the current instance of the treeview
(only 1 exists in the session) and set the highlight property.

Does this help any with the problem?

It sounds like the bit that you're missing is where you work out how to
get to the "current instance" from your other class. You could either
store a reference in the session, or pass the instance to the object
which needs it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top