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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:10:54+00:00 2026-06-18T08:10:54+00:00

I have this C# WinForms code in which I have several different structs ,

  • 0

I have this C# WinForms code in which I have several different structs, that all function in the same way. So instead of writing individual functions for adding or removing items, I’m trying to use Templates instead.

For example, here is one struct and the corresponding List<> I’m using to store its objects:

public struct Alias
{
    public string alias;
    public string aliasSource;

    public static bool IsValid(...); //This function exists in all the structs
};

List<Alias> aliases;

This the function used from the outside, to add Aliases:

public void AddAlias(Alias iAlias)
{
    AddGenericStructItem<Alias>(iAlias, aliases);
}

And this is the actual function doing the addition:

private void AddGenericStructItem<T>(T genericStructItem, List<T> genericList)
{
    string outputError;
    if (T.IsValid(genericStructItem, out outputError)) //< -- Problem in the 'T' being used in the far left
    {
        if (genericList.Contains(genericStructItem))
        {
            MessageBox.Show("ERROR 82ha5jb :: Item already exists");
        }
        else
        {
            genericList.Add(genericStructItem);
        }
    }
    else
    {
        MessageBox.Show(outputError);
    }
}

The problem occurs in the T.IsValid... part. The compiler gives me the following error on the T:

'T' is a 'type parameter', which is not valid in the given context

Is there any way around this? All my structs have an IsValid function in them with the same setup, so it would seem silly to repeatedly write the same code, in case I don’t use templates here…

  • 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-06-18T08:10:55+00:00Added an answer on June 18, 2026 at 8:10 am

    You can’t use constraints to tell the compiler that a static method will exist on your object. If it really needs to be static, you’ll need to use reflection to call the method:

    var methodInfo = typeof(T).GetMethod("IsValid", BindingFlags.Static|BindingFlags.Public);
    if (methodInfo != null)
    {
        object[] parameters = new object[] { genericStructItem, null };
        if ((bool)methodInfo.Invoke(null, parameters))
        {
            // It's valid!
        }
        else
        {
            string error = (string)parameters[1];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this possible? We have WinForms (CF) apps which use the EMDK, and would
I have an application (winform exe) that I run several times. Does this mean
I have this app written in VB.Net with winforms that shows some stats and
I have a function created in C# (WinForms) which saves the file as gif
I have this existing code: Private Function GetTypeFromName(ByVal FullTypeName As String, ByVal AssemblyName As
I have code that lets be drag around a borderless form in winforms that
I have been working on a C# WinForms application that registers several hot keys
I have a winforms app that installs other apps in a loop. This works
I have some simple example WinForms code which I am trying to translate into
I have this HeaderlessTabControl, which subclasses the classic TabControl. // From http://social.msdn.microsoft.com/forums/en-US/winforms/thread/c290832f-3b84-4200-aa4a-7a5dc4b8b5bb/ // Author:

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.