Click here to Skip to main content
15,913,264 members
Home / Discussions / C#
   

C#

 
AnswerRe: word search puzzle [modified] Pin
DaveyM695-Dec-09 0:17
professionalDaveyM695-Dec-09 0:17 
QuestionSending packet to a specified IP address & predefined the contents of it... Pin
3bood.ghzawi4-Dec-09 22:07
3bood.ghzawi4-Dec-09 22:07 
AnswerMessage Closed Pin
4-Dec-09 22:46
stancrm4-Dec-09 22:46 
GeneralRe: Sending packet to a specified IP address & predefined the contents of it... Pin
3bood.ghzawi5-Dec-09 2:51
3bood.ghzawi5-Dec-09 2:51 
QuestionA Question About Class Properties Pin
Roger Wright4-Dec-09 17:15
professionalRoger Wright4-Dec-09 17:15 
AnswerRe: A Question About Class Properties Pin
PIEBALDconsult4-Dec-09 17:44
mvePIEBALDconsult4-Dec-09 17:44 
GeneralRe: A Question About Class Properties Pin
Roger Wright4-Dec-09 18:02
professionalRoger Wright4-Dec-09 18:02 
GeneralRe: A Question About Class Properties Pin
DaveyM694-Dec-09 21:06
professionalDaveyM694-Dec-09 21:06 
There are also other useful 'properties' of properties. A couple that spring to mind are...

Properties can be serialized by the designer, fields cannot.
Because property getters and setters are methods, we can do other stuff such as raising events if the underlying value changes, as well as validation. Consider this component's code:
C#
using System;
using System.ComponentModel;

public class PercentageControl : Component
{
    private const int DefaultPercentage = 50;
    public const int MaxPercentage = 100;
    public const int MinPercentage = 0;

    public event EventHandler PercentageChanged;

    private int percentage;

    public PercentageControl()
    {
        percentage = DefaultPercentage;
    }

    [DefaultValue(50)]
    public int Percentage
    {
        get { return percentage; }
        set
        {
            if (value >= MinPercentage && value <= MaxPercentage)
            {
                if (value != percentage)
                {
                    percentage = value;
                    OnPercentageChanged(EventArgs.Empty);
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException(
                    "Percentage",
                    string.Format("Percentage must be between {0} and {1}",
                        MinPercentage, MaxPercentage));
            }
        }
    }

    protected virtual void OnPercentageChanged(EventArgs e)
    {
        EventHandler eh = PercentageChanged;
        if (eh != null)
            eh(this, e);
    }
}
If you create a winforms project, add this code, build and then drop a PercentageControl from the toolbox onto the form, whenever the Percentage property isn't 50, the form's .Designer.cs will change and store the new property value for you so it can be set to the design time value at runtime.

If you subscribe to the PercentageChanged event, you will see it is raised whenever the Percentage property's value is changed.

Dave

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)

GeneralRe: A Question About Class Properties Pin
Roger Wright5-Dec-09 2:59
professionalRoger Wright5-Dec-09 2:59 
GeneralRe: A Question About Class Properties Pin
PIEBALDconsult5-Dec-09 4:17
mvePIEBALDconsult5-Dec-09 4:17 
AnswerRe: A Question About Class Properties Pin
Richard Blythe4-Dec-09 18:08
Richard Blythe4-Dec-09 18:08 
GeneralRe: A Question About Class Properties Pin
Roger Wright4-Dec-09 18:24
professionalRoger Wright4-Dec-09 18:24 
GeneralRe: A Question About Class Properties Pin
Richard Blythe4-Dec-09 18:39
Richard Blythe4-Dec-09 18:39 
GeneralRe: A Question About Class Properties Pin
Roger Wright4-Dec-09 18:58
professionalRoger Wright4-Dec-09 18:58 
AnswerRe: A Question About Class Properties Pin
Luc Pattyn5-Dec-09 3:06
sitebuilderLuc Pattyn5-Dec-09 3:06 
QuestionReading x64 registry keys from a x86 computer remotely Pin
Jacob Dixon4-Dec-09 14:53
Jacob Dixon4-Dec-09 14:53 
AnswerRe: Reading x64 registry keys from a x86 computer remotely Pin
Dave Kreskowiak4-Dec-09 17:05
mveDave Kreskowiak4-Dec-09 17:05 
GeneralRe: Reading x64 registry keys from a x86 computer remotely Pin
Jacob Dixon4-Dec-09 18:13
Jacob Dixon4-Dec-09 18:13 
GeneralRe: Reading x64 registry keys from a x86 computer remotely Pin
Dave Kreskowiak5-Dec-09 3:41
mveDave Kreskowiak5-Dec-09 3:41 
GeneralRe: Reading x64 registry keys from a x86 computer remotely Pin
Jacob Dixon5-Dec-09 4:04
Jacob Dixon5-Dec-09 4:04 
QuestionActivating a child form Pin
electriac4-Dec-09 13:20
electriac4-Dec-09 13:20 
AnswerRe: Activating a child form Pin
Luc Pattyn4-Dec-09 13:27
sitebuilderLuc Pattyn4-Dec-09 13:27 
GeneralRe: Activating a child form Pin
electriac4-Dec-09 15:06
electriac4-Dec-09 15:06 
GeneralRe: Activating a child form Pin
Luc Pattyn4-Dec-09 15:45
sitebuilderLuc Pattyn4-Dec-09 15:45 
GeneralRe: Activating a child form Pin
electriac5-Dec-09 0:11
electriac5-Dec-09 0:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.