Solved

Finding the property setter class

Posted on 2003-11-19
3
235 Views
Last Modified: 2010-04-16
Hi

Is it possible to find which is object is setting a particular property?

For ex: I have a class in which TestValue is a property that sets its int m_iTestValue. And many other may classes set this TestValue property. In the class which holds this property, I want to find the class which is setting the value to this property.

Is this possible?

Regards
Murali
0
Comment
Question by:bmmohan
  • 2
3 Comments
 
LVL 6

Expert Comment

by:purpleblob
ID: 9786072
Can I just try and get this clear in my head. You have a class with a property (TestValue) - let's say it's ina class called MyClass for clarity.

Now you have code such as

MyClass obj = new MyClass();
obj.TextValue = 10;

And you wish to find what class called this method ?

If this is the case I very much doubt it's possible/simple as the instance of MyClass (in this example) would need to have a reference to the object that called the property and really this could only be achieved by passing a reference of the object to your property/method. i.e.

MyClass obj = new MyClass();
obj.SetTextValue(this, 10);

Now I suppose one could attempt the trace through the stack to discover who's called this property but I doubt it'd be worth the effort.

If this isn't what you mean't then sorry about the rambling, please can you clarify, maybe with a snippet of code, what you're after :-)
0
 

Author Comment

by:bmmohan
ID: 9786526
Hi purpleblob

Thanks for the reply. I should have been more clear on my query.

Actually I am creating a user Control which is a Scrollable Control. This inturn will have a Graph Control. This Graph Control will be increasing gradually in its size. So when the size of the graph control is more than the Scrollable Control, the scroll bars have to be shown.

Here I am manually doing some calculations to find out whether the scroll bars are required or not. If the scroll bars are required, then I set the Autoscroll property to True, and also set the AutoscrollPosition.

For this, when this control is added to a form, the Autoscroll and AutoscrollPosition property of the Scrollable Control should not be allowed to be set. So I want t restrict the form to set these properties. Only the scrollable control must be able to these properties, no other control or form.

I was trying out lot of options and thought of overriding these properties. At that I thought, if I can find any other class is setting these peoperties, I can just reset back to their default properties. But I was unable to restrict the form from setting these peoperties OR unabl to find whether the property was set by the control or form.

Can you please tell me how to achieve this? Can I do this using ShouldSerialize and Reset ?

Please help me as this is little urgent for me.

Regards
Murali
0
 
LVL 6

Accepted Solution

by:
purpleblob earned 65 total points
ID: 9786740
You cannot restrict a property to a specific class (again unless you pass a reference to that class to your object and then you could use the is operator to see whether it's a certain type of class, but this would stop someone creating a new class derived from the object you allow access... anyway I think I'm going of track :-) )

You can change the visibility of a property to only available to classes within the same assembly (i.e. DLL or EXE) by using the internal keyword, i.e.

instead of

public int TestValue
{
}

have

internal int TestValue
{
}

Now only objects which are within your assembly can interact with this property.

Anyway short answer - you cannot restrict access to your properties to a single instance of a single class without doing something like I mentioned previously, i.e. passing a reference to the object altering the property and then checking to see whether this is an instance or a type that is allowed to alter the property.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calling stored proc in EDMX 11 23
How to iterate over object and get property name and value 4 26
Anyway to make "All" the default in the dropdown? 6 34
C# Single Form 8 27
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now