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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:15:20+00:00 2026-05-25T21:15:20+00:00

I have an object with has two properties: Text and Type . To avoid

  • 0

I have an object with has two properties: Text and Type.

To avoid hard-coding the Types, I put them in the database, so they can be added to in future. At the moment the types are URL, Username and ID.

However, I now want to run a Utility method to clean up the object’s Text field based on which Type the object is (e.g. add ‘http://’ if its a URL).

Is there a way to do this in the Utilities class without hard-coding the types in a switch statement/if else block.

switch (type)
{
    case 1:
        TidyUrl();
    case 2:
        TidyUsername();
    case 3:
        TidyID();
    default:
        break;
}

In this example I’m hardcoding the IDs from the database (‘Type’ table), which can never be a good thing!

Is there a better way to do this?

  • 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-25T21:15:21+00:00Added an answer on May 25, 2026 at 9:15 pm

    Traditionally this is handled using a common interface, and dynamically constructing a concrete implementation to do the actual work.

    for example:

    public interface ITidy
    {
       string Tidy(string input);
    }
    

    then the implementations

    public class UrlTidy : ITidy
    {
        public string Tidy(string input)
        {
            // do whatever you need to the url
        }
    }
    

    And so on for the other types of tidying. Now you need a way of instantiating the right concrete class (UrlTidy, IdTidy etc) from the type you are looking at. One way to do this might be to put the class name in the database along side the type, and use reflection to instantiate the right implementation of ITidy. Another wat would be to have a Factory class which uses some other method to instantiate the right ITidy based on type.

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

Sidebar

Related Questions

I have two objects, let's call them Input and Output Input has properties Input_ID
I have a list of point object where point class has two properties X
I have an array of objects. The object has two properties a value and
I have two tables: object that has object_id column and avalues that have object_id
Suppose I have a design like this: Object GUI has two objects: object aManager
I have an object which has several properties that are set when the object
I've got a simple object post that has two NSMutableArrays as properties. One is
Assume I have a class called MyClass that has two properties (int Id and
i have an object called Person . It has properties First, Last, Age ,
I have a collection of objects (IQueryable). Each object has various properties, some string

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.