Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:12:25+00:00 2026-05-29T07:12:25+00:00

I have a user control with some public properties. A particular property is an

  • 0

I have a user control with some public properties. A particular property is an integer, but must only accept positive values that are less than a const max value. At present I do the following:

private int markerwidth = 2;
[DefaultValue(2), Category("Appearance"), Description("Size of position marker")]
public int MarkerWidth
{
    get
    {
        return this.markerwidth;
    }
    set
    {
        if (value > 0 && value <= MAXMARKERWIDTH)
        {
            this.markerwidth = value;
        }
    }
}

This does the job, but fails silently. I guess I could add logic to use 0 for negative values and the max value for those that exceed it, but it’s still not ideal.

By way of contrast, the TabValue property (inherited from UserControl) complains if I try to set a negative value at design time (and presumably at run time).

If this achieved with a normal exception? Or is there a better way? An attribute maybe?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:12:27+00:00Added an answer on May 29, 2026 at 7:12 am

    The most optimal way is to achieve via exception. Just continue your code

        if (value > 0 && value <= MAXMARKERWIDTH)
        {
            this.markerwidth = value;
        }
        else 
        {
            throw new ArgumentOutOfRangeException("Invalid value. Value must be between 0 and " + MAXMARKERWIDTH.ToString());
        }
    

    EDIT

    Yes, Wiktor Zychla is absolutely right! I corrected the answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control which uses objects as inner properties (some code is
I have to create Silverlight User Control with public properties that should be used
I am working on a user control that has some dependency properties, namely; public
I have a user control which does some validation in the ValidateChildren method which
I have made a user control in asp.net c#. Becuase of some data I
I have a two dependency properties(both List of strings) in a custom user control.The
I have the task to write a user control to display some results by
I have a user control inside a webpart inside sharepoint that I add some
I have a UserControl and I put some public Methods/Properties in there. I would
I have a user control that displays objects of a client-declared type with some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.