Avatar of David DB
David DB

asked on 

Restrict parameter to Controls and Forms in C#

Hi

I have some methods that I want to restrict the parameters to allow only Control and Form objects.

I know I can use restrictions, but only one class, and multiple interfaces.

public static void SetWidth<T>(T thisObj, int width) where T: Control
         {
            thisObj.Width=width;
         }

Open in new window


How can I extend the where T: to include Form also ?
C#

Avatar of undefined
Last Comment
AndyAinscow
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

I think this is what you require
where T: Control, Form
EXPERT CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

I'd been puzzled by the request (it is ambiguous, control and form objects - does it mean all controls including forms or the control must be a form?).  The link provided should show all possibilities for the generic and their combinations.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

I've made a quick test here.  (Guess who doesn't use the where clause that much and misunderstood the docs).

My first comment is wrong, it won't compile.
Avatar of David DB
David DB

ASKER

Thanks for all the input. You are right that Control also includes Form. I found that out just after I posted.

Regarding extension, that example was actually not relevant for my case since I use this to set one control edge (top, left, width, height) relative to another. Building a class out of all these relatives to resize controls on a form when the forms size changes in the form resize event.
Avatar of it_saige
it_saige
Flag of United States of America image

David, while I appreciate the points.  I believe that gr8gonzo deserves credit as well.

-saige-
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

I agree with it_saige, gr8gonzo was the first to point out Forms are Controls


ps.  Should anyone require completely unconnected classes in a generic then this (rather theoretical example) is a way to do it.  Provide a dummy interface and have each class implement the interface even if it doesn't actually do anything.

        private void button1_Click(object sender, EventArgs e)
        {
            foo2<X>(new X());
            foo2<Y>(new Y());
        }




        private void foo2<T>(T ob) where T:IFoo2
        {

        }


    public interface IFoo2
    {
        void Z();
    }

    public class X : IFoo2
    {
        public X() { }
        public void Z() { }
    }

    public class Y : IFoo2
    {
        public Y() { }
        public void Z() { }
    }
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo