473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Accessing Protected Variable From Outside Class

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
5 5442

"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
<=?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
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
<=?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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
3
by: Vivek Sharma | last post by:
Hi, I have created a dropdownlist as a web user control. I am using its multiple instances on the webpage. How do I access the selectedValue of each instance? All the instances have different...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
7
by: hummh | last post by:
Hello out there, I´m making my first steps with ASP.NET 2.0 and have he following problem: I´ve implemented a Web User Control that sits in the root of my ASP.NET Website. I want to use the...
5
by: Siva | last post by:
Hello I have a dropdownlist inside the gridview as a template column defined as follows: <asp:TemplateField HeaderText="Choose Location"> <ItemTemplate> <asp:DropDownList ID="ddlChooseLoc"...
5
by: MrJim | last post by:
How should variables be declared and referenced in both the base and derived form so they can be accessed?
4
by: Joseph Paterson | last post by:
Hi all, I'm having some trouble with the following code (simplified to show the problem) class Counter { protected: int m_counter; }
2
by: Licheng Fang | last post by:
On Apr 14 2003, 10:30 pm, Alex Martelli <al...@aleax.itwrote: Sorry to dig up this old thread, but I would like to know what's the rationale is. Why can't a nested function rebind a variable of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.