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 6799559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:51:46+00:00 2026-05-26T18:51:46+00:00

This is probably a long shot, but I’m trying to minimize the repition in

  • 0

This is probably a long shot, but I’m trying to minimize the repition in the program I’m working on, and have run into a snag. As can be seen in the ClearTextBoxes() method below, I have a very repetitive bit of code that I would prefer to place inside a foreach loop for succinctness. (Originally the foreach (object box in customBoxes) loop was not there). I tried to do this with the following List, but to no avail. I’m not sure if this is just not possible to do, or if I’m simply doing it wrong. I would appreciate any help you could give, and if this can’t be done, then how can I shrink this code block?

Thanks!

List<object> customBoxes = new List<object>();

customBoxes.AddRange(new[] { "TextBox", "DateBox", "DigitBox", "PhoneBox", "WaterTextBox" });



public void ClearTextBoxes()
    {
        ChildControls ccChildren = new ChildControls();

        foreach (object o in ccChildren.GetChildren(rvraDockPanel, 2))
        {
            foreach (object box in customBoxes)
            {
                if (o.GetType() == typeof(TextBox))
                {
                    TextBox txt = (TextBox)o;
                    txt.Text = "";
                }

                if (o.GetType() == typeof(DigitBox))
                {
                    DigitBox digit = (DigitBox)o;
                    digit.Text = "";
                }

                if (o.GetType() == typeof(PhoneBox))
                {
                    PhoneBox phone = (PhoneBox)o;
                    phone.Text = "";
                }

                if (o.GetType() == typeof(DateBox))
                {
                    DateBox date = (DateBox)o;
                    date.Text = "";
                }

                if (o.GetType() == typeof(WatermarkTextBox))
                {
                    WatermarkTextBox water = (WatermarkTextBox)o;
                    water.Text = "";
                }
            }
        }
    }
  • 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-26T18:51:47+00:00Added an answer on May 26, 2026 at 6:51 pm

    I would create an interface with a ClearText() method.

    interface IClearable
    {
      public void ClearText();
    }
    

    Then you can inherit from each control and apply that interface:

    class ClearableDigitBox : DigitBox, IClearable
    {
      public void ClearText() {
        Text = String.Empty;
      }
    }
    // etc...
    

    So it’s just:

    var list = new List<IClearable>;
    // ...
    foreach (IClearable control in list) control.ClearText();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a long shot, but I have vague sense I ran across
This is probably a long shot but I asked a question about converting one
this is probably trivial, but i have a quite long sql query and echo
Its been a long day and this is probably extemely simple but how do
This is probably easy, but I'm trying to convert from a source which provides
It's probably a long-shot that anyone can answer this without seeing all the source
It's probably a long shot, but is there an add-in for VS2010 that can
This is a long shot, but does anyone know of an algorithm for estimating
This is probably a n00b question, but nontheless... I have a bit of a
This is probably trivial, and I do have a solution but I'm not happy

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.