Using attributes to observe a variable - c#

I am planning to make something like setting an attribute to a variable in any class and be able to observe the changes to the variable from another class is this possible. Basically what I am trying is tagging a variable with a certain name and collect all the named variables and display it in screen and be able to change the observe and consume the changes to the variable. Is this possible with attributes or any other feature in c#?
Thanks for your time!
Fl#my!

Related

Get all writeable properties of an ADLDS-Class

I'm developing an application which can deal with a MS-ADLDS-Service.
Currently it is possible to create Directory-Entries and assign values to some properties.
Not a realy exciting task until this:
Im my application it's possible (it should be) to configure which properties of a class (for instance: the CN=Person class) should be assigned with values which are evaluated at runtime in my application.
Long story short:
I want to retrieve all (writeable) properties of a class. Without creating and saving a new CN=Person-Object before.
Currently i use my schemaBinding to get the Directory-classSchema-Entry of the Person-Class (CN=Person) from where i read some property-values (like "AllowedAttributesEffective", "mayContain", "AllowedAttributes") - i get the most properties by this way - but some Properties are missing! For instance the "telephoneNumber"-Property (attributeSchema: CN=Telephone-Number)
Does anybody know how to get these properties of a class? ADSI-Edit does this: when i create a new object with adsi-edit i can assign values to all possible properties before committing the new entry.
thanks a lot for any hint!
(.net code is welcome)
I have found the solution for my task!
Some of these properties are "calculated" and not persistent at the directoryentry.
So its meant to call the RefreshCache() Method and pass the needed property names as an string array.
directoryEntry.RefreshCache(new string[] { "allowedAttributesEffective",
"allowedAttributes",
"systemMayContain",
"systemMustContain" });
After that call, the properties have values....
if (directoryEntry.Properties["systemMayContain"]).Value != null)
{
/// Success
}

DataType to store control type Windows Forms C#

Trying a simple project to create my own (very basic) data binding.
I have a class FIELD_DESCRIPTOR which stores meta information about a database column. There will be a further class FIELD which represents the actual field, which will contain a reference to its corresponding FIELD_DESCRIPTOR class.
I am getting stuck in 2 places.
First, I need to have a property in the FIELD_DESCRIPTOR class that stores what kind of Windows Forms control the field is mapped to at the front end. for e.g. I need a property, say, MAPPED_CONTROL_TYPE. And I should be able to store either TextBox, ComboBox etc in this property. Should I just use a string property and be done with it ? Or is there a better way ? Ideally, I'd like to use some kind of enumeration of control types.
Secondly, I need to store a reference / handle to the actual control the field it mapped to. (I think I can do this by passing a reference of the actual control on the form.)
How do I implement this ? What kind of dataType should be used to define this property ?
(Using .Net 3.5, No WPF)
Thanks and regards.
First, I need to have a property in the FIELD_DESCRIPTOR class that
stores what kind of Windows Forms control the field is mapped to at
the front end. for e.g. I need a property, say, MAPPED_CONTROL_TYPE.
And I should be able to store either TextBox, ComboBox etc in this
property. Should I just use a string property and be done with it ? Or
is there a better way ? Ideally, I'd like to use some kind of
enumeration of control types.
The fully qualified type name springs to mind, e.g. System.Windows.Forms.TextBox. This should be unique, and could always be used for dynamic creation if needed.
Secondly, I need to store a reference / handle to the actual control
the field it mapped to. (I think I can do this by passing a reference
of the actual control on the form.)
You could indeed store the reference using a type such as System.Windows.Forms.Control. If you do, be carefult that you don't create a memory leak. i.e. When you control is no longer needed, you should no longer hold its reference in your lookup otherwise you will stop it being disposed.

C# and ASP.NET custom property attributes and determining if properties changed

I am working on a project where we want to keep a history of a particular object. On save I want a method on the object that will determine if it has changed so that I can call a method to save its current state to history. E.g. I populate a form from an object the user makes changes (or possibly not) and submits the from. I want to take my original object and a copy of that object that has been updated from the form and determine if it has changed at all. Additionally I may decide at some point that certain properties don't matter (e.g. if Name changes I won't track it).
I'm thinking the easiest/most flexible way to accomplish this would be if I could give the properties I care about a custom attribute [ChangeTracked] and then I could use reflection to get a list of all properties with that attribute and loop through them comparing A.property == B.property to determine if any have changed.
Would this work? Is there a significantly better/easier way to handle this, like some sort of built in method you can add to an object to determine if the values of any properties have changed? Whatever the solution some psudo code would be appreciated. Just as a point of clarification the solution needs to determine if the value I care about has actually changed not just if it has been assigned since it was created i.e. if I set Name="bob" and it was already "bob" before my assignment this does not count as a change.
It ain't fancy, but this is the tried and true brute force method. Just add a private property to the object named IsDirty. For properties that you want to track, just add IsDirty=True to the property Set routine. For more complicated "do I care" rules, just code them into the property set.
The page button's click event can fire a Save event that writes all the values from the textboxes and dropdowns into the object properties, then calls the object Save method, which tests the IsDirty property before doing anything.
One possible method would be to add a deep copy of the object as a private property of the object when it is loaded. (One method of deep copy)
On save you can compare the copy object to your "live" object to see if any changes have occurred.

C# Changing a constant

I have inherited a small windows form based program written in C# that uses a 'constant' (See below), I need to modify this program so 'PROPERTY_NAME' can be "jobs" and "careers".
private const string PROPERTY_NAME = "jobs";
I'm guessing a constant isn't designed to change so should I need to change this. The line above is set once at the top of a class file and then PROPERTY_NAME is used throughout that file.
On the main form I would like to add two radio buttons 1 called 'jobs' and one called 'careers' and then change the PROPERTY_NAME in the class file based on which is selected. Would I need to pass the radio button status to the method in the class file? I recall reading that I can't simply read the radio button value from the class file.
Many thanks for your advice.
Jane
My best (and simplest) guess (I could elaborate into cleaner things but this is just for speed) without seeing any other part of the code would be to remove the const and add readonly so PROPERTY_NAME is just a plain old class member variable that cannot change outside of the constructor.
In the class's constructor, take in a string parameter, and have the code that creates an instance of this class pass in either "jobs" or "careers" (coming from the selected radio button probably) and set the PROPERTY_NAME variable.
EDIT:
Like Sasha says, another way would be using an enum but it depends what exactly is being done with PROPERTY_NAME as to whether this is appropriate for your application.
If the quantity you are representing changes ever throughout the history of the universe then do not make it a constant. Constants are things like the number of eggs in a dozen or the atomic weight of lead. Things like version numbers or the current price of gold change over time and therefore are not constant. Only make actually constant values into constant fields. The compiler will treat constant fields as constant for all time, which can introduce semantic errors if they change.
make an enum (my preferred way) and make it a readonly property. Set this property in the constructor. It isn't changeable after creation and should do what you need.
-sa
You cannot make constant having two values. It looks like that you need to make a field storing current property name and use it allover your form. And you will be able to init such field from the radio button.

How can I update and get values in Windows Forms while moving one form to other form (like cookies)?

How can I update and get values in a Windows Forms application while moving one form to other form (like cookies)?
I need to update the values to some variable and again I am going to refer stored values and need to do some calculations.
I have used cookies in ASP.NET but I am not able to find out the same concept in .NET Windows Forms (C#).
How can these issues be resolves?
You can use object references.
You can decalre a read/write Property for each variable you want to be available in another form and the use them for sharing your data.
One way to do this is to declare variables to be public, either in a global module or in any form.
public x as double
If it is declared in a module, you can access it with the variable name only. To access data declared in another form, use that form name with the variable: form1.x = 7
Another way is to declare a property in a form or other class.
A really simple way of getting cookie-like functionality would be to declare a static string dictionary in Program (Program.cs)
public static System.Collections.Specialized.StringDictionary SortOfLikeCookies = new System.Collections.Specialized.StringDictionary(); and read/write string values using Program.SortOfLikeCookies["Name"] = "Value";

Categories