By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,204 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,204 IT Pros & Developers. It's quick & easy.

Accessing Protected Variable From Outside Class

P: n/a
Ben
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);
}

}
Mar 30 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Ben" <ben_1_ AT hotmail DOT com> wrote in message
news:77**********************************@microsof t.com...
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

Mar 30 '06 #2

P: n/a
<=?Utf-8?B?QmVu?= <ben_1_ AT hotmail DOT com>> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 30 '06 #3

P: n/a
Ben
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

Mar 30 '06 #4

P: n/a
<=?Utf-8?B?QmVu?= <ben_1_ AT hotmail DOT com>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 30 '06 #5

P: n/a
Ben
thanks. I'll pass that on to the dev. team to see what we can figure out.

"Jon Skeet [C# MVP]" wrote:
<=?Utf-8?B?QmVu?= <ben_1_ AT hotmail DOT com>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.