|
public (static) fields EVIL!!!!!!
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
|
Arjan Einbu wrote:
The readonly keyword makes it work exactly the same way as the property getter.
If all you do in the property getter is return the member variable (field) then there isn't much difference.
As an absolute rule I never ever under any circumstances reveal the member variables of a class as public. Readonly or not. It breaks the OO rule on encapsulation.
What if you change the design of the class later on? You've publicly exposed the inner workings of the class! Using properties you can hide the inner workings, you can implement lazy lookup (my second suggestion employed a form of lazy lookup - the singleton is not created until needed), the innards of the class can change and the code for the property can coerce the expected value out so that existing code doesn't notice the difference.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
Colin Angus Mackay wrote:
If all you do in the property getter is return the member variable (field) then there isn't much difference.
You did in your example... ;)
I know the rules of OO, but i believe rules can be broken sometimes if it serves a greater purpose. (Less code, fewer sources of error, greater maintainability.)
You're absolutely right that you might want to change your code later on, and if other assemblies access my field, they will need to be recompiled if that field later on is changed to a property. So if some other assemblies are going to use that singleton, one should consider wrapping it up in a property, as you say, Colin.
(Also, public fields cannot be made part of an interface definition, so there can be several reasons to make it a property.)
However, if the field is accessed only from within the same assembly (or assemblies that are compiled together with the containing assembly) it will make no difference if you change the field to a property. This is very often the case. (Though one should perhaps mark the property as internal instead of public in that case.)
Encapsulation is preserved i this construct. By making the instance constructor private, we make sure there will be made no other instances of the singleton.
Revealing the inner workings on this construct wont do any harm. The knowledge gained from it can't be abused to access inner data in other ways than the way we planned for.
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
|
Arjan Einbu wrote:
I know the rules of OO, but i believe rules can be broken sometimes if it serves a greater purpose. (Less code, fewer sources of error, greater maintainability.)
Many years ago I worked with a programmer that would take this statement and abuse it to the extent that everything would be public! He'd interpret it as: Less code == fewer sources of error == greater maintainability
You make a very good argument in this case for allowing the public readonly field. However by the time I thought through all the possible senarios for each member variable I create and decide whether to fully encapsulate it or decide that it is safe to expose I could have written a default compliment of getters/setters for them.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
Colin Angus Mackay wrote:
Many years ago I worked with a programmer that would take this statement and abuse it to the extent that everything would be public!
Well, he didn't get it... neither about OO or maintainability.
I hope you understood it the way i meant it...
And yes; Creating the getter for this one probably is better, and isn't that much work. (Virtually none with the right tools[^] )
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
|
Desmond,
If you elected to receive response notifications via email the email you got will contain slightly incorrect code. I have corrected the code, but you need to view it on the forum.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
By the way, is there a way to "shorten the patch" to the singleton ? I mean, I have to write right now MainMameSpace.MyClasses.PropertiesClass.Instance.SomeFunction, but it would be much easier if it could be just MainNameSpace.PropertiesInstancel.SomeFunction.
Isn't it possible to access the ProperitesClass.Instance through a pointer or something ?
|
|
|
|
|
At the top of the file you can add using statements so that you can short circuit the path to the classes. If there is any ambiguity, for example two classes with the same name but in different namespaces, you will still have to use the full path.
For example:
using MainNameSpace;
using MainNameSpace.MyClasses;
then later in your code you can just put:
PropertiesClass.Instance.SomeFunction()
C# only permits pointers in unsafe methods. However if you are going to use the instance a lot then you can just assign it to a variable as normal. e.g.
PropertiesClass pci = PropertiesClass.Instance;
...
pci.SomeFunction();
I hope this points you in the right direction.
Regards,
Colin.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
hi, im using a datagrid generated from a XML file using the VS XSD builder. now i want to add a "virtual" colum that add's some extra information, i only need this information on the runtime so i don't wanna add it to the XML file.
i have read some examples with "BindingContext" and "Expression" but this examples are mostly DataGridBoolColumn and no TextColums.
dose anybody knows a tuturial or have some example code?
|
|
|
|
|
|
hi
i am try to do like this ....
i have a win form textbox and i am trying to write something like "jacal99" using threading with five seconds interval .... the value of thread was thread.sleep(5000);
but when the next charactor try to print in the textbox the first value gone.
i want to pring "jacal99" with five seconds interval .... every charactor...
thx.;)
Mazhar Hussain
|
|
|
|
|
If you post your code it might be easier to help.
Perhaps your assigning the value of Text property everytime and not adding to it:
yourTextBox.Text = "whatever"; //no
yourTextBox.Text += "whatever"; //yes
|
|
|
|
|
Hi again. Still learning C#. This time I managed to create 2 similar controls. I also have a class PropertiesClass that should change some properties (like color and text) of the controls. But after I have changed a property in PropertiesClass, how can I let the other (two) controls know that I did it ? I remind You that I have several controls that must be updated, not just one. I guess the only chance would be using events somehow ?
From my Borland Delphi days I remember using messages (if property is changed I broadcast the message and the controls respond to it) for things like this, but unfortunately (or luckily) .NET doesn't support them...
Best regards, Desmond
|
|
|
|
|
Yes, Raise an event is good solution. Search this site for Delegate and EventHandler keywords and you will find some articles about it. Also take a look at C#/Control part too.
Mazy
No sig. available now.
|
|
|
|
|
I'm very new to C# and I have some questions about writing my own controls (I thought I can learn it best that way). I have two classes - one containing a procedure that paints a area (draws a custom 3D border and background) and another class is a control, witch must call the painting method (that is in the first class) in OnPaint method.
What I don't understand here is how can I pass the area of control as a parameter of the method?
Like in Borland Delphi I would have done it like this:
procedure PaintControl(AreaToPaint: Rect)
Or with two points (one x1, y1 and another x2,x2).
How can I do this in C# ?
If you understood even a little what I'm talking about, please reply....
|
|
|
|
|
On of parameter of your OnPaint is PaintEventArgs (usually named e), and one if its memeber is e.Graphics , pass it to your painting class , you can do painting with it there.
Mazy
No sig. available now.
|
|
|
|
|
desmond5 wrote:
What I don't understand here is how can I pass the area of control as a parameter of the method?
You can call get a rectangle object referencing your control like this:
Rectangle rect = this.Bounds;
SomeMethod(rect);
public void SomeMethod(Rectangle rect)
{
}
- Nick Parker My Blog
|
|
|
|
|
where is "Project Settings (ALT+F7)" of VC++6.0 in VS.NET.
where i could set exe file name, compiler macros etc
|
|
|
|
|
VS.NET IDE -> Solution Explorer -> <right click on root of the tree > properties .
There you are !!
___________________________________________________________
greatest thing is to do wot others think you cant suhredayan@omniquad.com
|
|
|
|
|
Go to Tool menu
Customize---->Option Tab----->Keyboard----->Keyboard Mapping Schema
Select VC++.6 there.
Mazy
No sig. available now.
|
|
|
|
|
Hi,
I am an ex VB programmer learning C# by converting a VB app into C#. In VB I was able to add properties (get and set) to a form so that you could set some properties, display the form so the user could do their bit and the hide the form to read the properties and then close the form.
Is it possible to add properties this way in C#?
I would need to call the form and set the form properties from a button click procedure.
Thanks for any help.
Stephen
|
|
|
|
|
Here's a stub of some code to get you creating get/set properties in C#
public string MyPropertyName
{
get
{
return this.myTextBox.Text
}
set
{
this.myTextBox.Text = value;
}
}
In this example the property is a string , but it can be any type you like.
The keyword value is special and it contains the value the caller want to set the property to.
To use the property you can just assign and use it like any variable.
Does this help?
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
It looked pretty close and so I placed it into the form but I was unable to access the property from my buttom click event. How can I make the property so I can access it from the property click event? What I want to achieve ( using the example given) is frmMyform.MyPropertyName = value;
Thanks.
Stephen
|
|
|
|
|
Sorry I don't understand what you are asking.
Properties don't have events. Controls (or any kind of class) can have events. A property is a special kind of a method on a class that you can use to implement get and/or set behaviour.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|