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

  • Home
  • SEARCH
  • 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 3229258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:47:23+00:00 2026-05-17T16:47:23+00:00

I have two almost identical c# functions. Because they’re so similar I thought I’d

  • 0

I have two almost identical c# functions. Because they’re so similar I thought I’d try out generics, but I’m stumped on how to do it. Any suggestions, or am I barking up the wrong tree entirely?

    public IList<UnitTemplate> UnitTemplates { get; set; }
    public IList<QualTemplate> QualTemplates { get; set; }

    public QualTemplate FindQualTemplate(string templateID)
    {
        QualTemplate selectedQualTemplate;
        if (QualTemplates.Count == 0)
            throw new CreatioException("This user's brand has no QualTemplates. There must be at least one available.");
        if (QualTemplates.Count == 1 || String.IsNullOrEmpty(templateID))
            selectedQualTemplate = QualTemplates.First();
        else
            selectedQualTemplate = QualTemplates.Single(x => x.QualTemplateID.ToLower() == templateID.ToLower());
        if (selectedQualTemplate == null)
            throw new CreatioException(String.Format("No QualTemplate with the id {0} could be found for this user's brand.", templateID));
        return selectedQualTemplate;
    }

    public UnitTemplate FindUnitTemplates(string templateID)
    {
        UnitTemplate selectedTemplate;
        if (UnitTemplates.Count == 0)
            throw new CreatioException("This user's brand has no UnitTemplates. There must be at least one available.");
        if (UnitTemplates.Count == 1 || String.IsNullOrEmpty(templateID))
            selectedTemplate = UnitTemplates.First();
        else
            selectedTemplate = UnitTemplates.Single(x => x.UnitTemplateID.ToLower() == templateID.ToLower());
        if (selectedTemplate == null)
            throw new CreatioException(String.Format("No UnitTemplate with the id {0} could be found for this user's brand.", templateID));
        return selectedTemplate;
    }
  • 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-17T16:47:24+00:00Added an answer on May 17, 2026 at 4:47 pm

    The problem you have is that both methods use a property which the two types don’t have in common: QualTemplateID and UnitTemplateID. If you can make the following changes to the code structure:

    • Declare UnitTemplate and QualTemplate to derive from a common base type, Template

    • In that base type, declare a property TemplateID

    • Get rid of QualTemplateID and UnitTemplateID and use the inherited TemplateID property instead

    then you can write the method generically:

    public TTemplate FindTemplates<TTemplate>(
        IList<TTemplate> templates, string templateID)
        where TTemplate : Template
    {
        TTemplate selectedTemplate;
        if (templates.Count == 0)
            throw new CreatioException("This user's brand has no template. There must be at least one available.");
        if (templates.Count == 1 || String.IsNullOrEmpty(templateID))
            selectedTemplate = templates.First();
        else
            selectedTemplate = templates.Single(x => x.TemplateID.ToLower() == templateID.ToLower());
        return selectedTemplate;
    }
    

    I’ve removed the if (selectedTemplate == null) because it would never fire anyway (unless the list is likely to contain nulls, but then the predicate you pass to Single would crash…).

    The above works equally well if you make it an interface instead of a base type.

    If you cannot make the changes to the code that I described, then your only option is to pass (as a parameter) a delegate that retrieves the ID:

    public TTemplate FindTemplates<TTemplate>(
        IList<TTemplate> templates, string templateID,
        Func<TTemplate, string> templateIdGetter)
    {
        TTemplate selectedTemplate;
        if (templates.Count == 0)
            throw new CreatioException("This user's brand has no template. There must be at least one available.");
        if (templates.Count == 1 || String.IsNullOrEmpty(templateID))
            selectedTemplate = templates.First();
        else
            selectedTemplate = templates.Single(x => templateIdGetter(x).ToLower() == templateID.ToLower());
        return selectedTemplate;
    }
    
    var qTempl = FindTemplates(QualTemplates, "myTemplateId", q => q.QualTemplateID);
    var uTempl = FindTemplates(UnitTemplates, "myTemplateId", u => u.UnitTemplateID);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes. They're almost identical, except for 2 attributes. I need to
I have two almost identical Beans. Bean Item contains a unique identifier (primary key),
I have two classes that are almost identical, besides one method. The classes have
I have two almost identical classes, in fact every member function is identical, every
I currently have two almost identical SQL queries, which I use against my SQLITE3
I have two insert statements, almost exactly the same, which run in two different
I have two identical tables and need to copy rows from table to another.
Compare these two largely identical functions. In the first, the memory for buff is
I have two methods that look almost the same, except for the aggregate function
I have two applications written in Java that communicate with each other using XML

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.