|
Uwe Keim wrote:
You missed the "call" word
Corrected now.
Uwe Keim wrote:
but surely it is quite inefficient if you have large number of controls to update.
Yes, but I don't think any other way exists in C#.
Uwe Keim wrote:
I think MFC's version only call the apropriate handlers that are really needed (e.g. only the contents of ONE dropdown menu).
Its a long time that I haven't use MFC , but as muuch as I remeber it was not act like this too.
Mazy
"A useless but amusing fact is that if all the DNA in all the cells in a single human being were stretched out it would reach the moon and back eight thousand times." - Steve Jones
|
|
|
|
|
Has anyone ran into this before? When I try to load a xfdf into the ocx the control stops responding. The reader.exe has full support for xfdf, so I have not idea what to think.
|
|
|
|
|
I have a small library witch contains a few controls and one simple class. The class holds a vew variables and functions that the controls need and also manipulates with the controls itself (changes their text and color for example).
MyOwnPropertiesClass <---- Ctrl1/2/3 connect to this class.
| | |
Ctrl1 Ctrl2 Ctrl3
Because all controls in library exchange information through that one class, there must be only one instance of that class. It should be created just when the library is initialized or something like that. How is it possible ?
Best regards, Desmond
|
|
|
|
|
Create a singleton. Here's how:
Create a private static member variable of your class of the same type as your class and call it singleInstance or similar.
Create a static initialiser which is like a normal constructor but with the keyword static in front of it. In this method create a new instance of the class and assign it to the singleInstance member variable.
Modify your constructor so that it is private . This ensures that no one else can create your class.
Create a new static public get property called Instance or similar and return the singleInstance member variable.
The remainder of the class is as you would normally have it.
You have now created a singleton. Every time you want to use the class you access it through the Instance property. This version of a singleton will be created at application start up.
If you want to delay the initialisation until first use then you can remove the static initialiser and have the code for the Instance property check singleInstance == null in which case a new instance of the class is created and assigned to singleInstance first. Then it returns singleInstance as it did before.
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
|
|
|
|
|
Thanks, this seems to be exacly what I need. However, could You please show me a little (code) example of a static initializer ?
And -- Where could I find more info on static initializers ? I did a quick search, but didn't result much...
Regards, Desmond
|
|
|
|
|
desmond5 wrote:
Where could I find more info on static initializers ?
Sorry, my fault - I was using Java terminology. In C# they are actually called static constructors. See this MSDN document[^]
[Edit]Ooops! I fixed some bugs[/Edit]
public MySingletonClass
{
private static MySingletonClass onlyInstance;
static MySingletonClass()
{
onlyInstance = new MySingletonClass();
}
public MySingletonClass()
{
}
public MySingletonClass Instance
{
get
{
return onlyInstance;
}
}
}
--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
|
|
|
|
|
Thanks. I also learned that these singletons has something to do with thread-safety (has something to do with multithreading) ... is this code compatible with that ?
|
|
|
|
|
desmond5 wrote:
singletons has something to do with thread-safety
I don't see why it cannot be used in a thead safety mechanism. However I've never used a singleton for that purpose.
For instance if you have a singleton that controls access to a shared resource then in each of you instance methods and properties (not the static ones) you can lock(this) to ensure that not other thread has access to your singleton while some sensitive operation is being carried out. See MSDN[^] for more information.
--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
|
|
|
|
|
Or a shorter version:
public MySingletonClass{
public static readonly MySingletonClass Instance = new MySingletonClass();
private MySingletonClass()
{
}
}
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
|
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....
|
|
|
|