Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. public...() where T statement confusion??

public...() where T statement confusion??

Scheduled Pinned Locked Moved C#
csharphtmlwpfcomhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sevententh
    wrote on last edited by
    #1

    I'm trying to understand what this piece of code does from Prajeesh's Blog I've never seen a statement with any 'where' statements in the method before

    using FluentValidation;
    ....

    public static ValidationResult Validate(K entity)
    where T : IValidator, new()
    where K : class
    {
    IValidator __Validator = new T();
    return __Validator.Validate(entity);
    }

    Can you help...???

    L T 2 Replies Last reply
    0
    • S Sevententh

      I'm trying to understand what this piece of code does from Prajeesh's Blog I've never seen a statement with any 'where' statements in the method before

      using FluentValidation;
      ....

      public static ValidationResult Validate(K entity)
      where T : IValidator, new()
      where K : class
      {
      IValidator __Validator = new T();
      return __Validator.Validate(entity);
      }

      Can you help...???

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Did you ask the author of the blog?

      It's time for a new signature.

      S 1 Reply Last reply
      0
      • S Sevententh

        I'm trying to understand what this piece of code does from Prajeesh's Blog I've never seen a statement with any 'where' statements in the method before

        using FluentValidation;
        ....

        public static ValidationResult Validate(K entity)
        where T : IValidator, new()
        where K : class
        {
        IValidator __Validator = new T();
        return __Validator.Validate(entity);
        }

        Can you help...???

        T Offline
        T Offline
        Tony Richards
        wrote on last edited by
        #3

        The where statements are used to constrain which Types the generic can be used for. So:

        Sevententh wrote:

        where T : IValidator, new()

        Means that the type T must inherit from IValidator and must have an accessible constructor that takes no arguments.

        Sevententh wrote:

        where K : class

        This states that the type must be a Reference Type, not a Value Type.

        S 1 Reply Last reply
        0
        • L Lost User

          Did you ask the author of the blog?

          It's time for a new signature.

          S Offline
          S Offline
          Sevententh
          wrote on last edited by
          #4

          yes, no reply as yet, so I thought I'd ask here, seeing as I use CP loads :-D

          1 Reply Last reply
          0
          • T Tony Richards

            The where statements are used to constrain which Types the generic can be used for. So:

            Sevententh wrote:

            where T : IValidator, new()

            Means that the type T must inherit from IValidator and must have an accessible constructor that takes no arguments.

            Sevententh wrote:

            where K : class

            This states that the type must be a Reference Type, not a Value Type.

            S Offline
            S Offline
            Sevententh
            wrote on last edited by
            #5

            So is the above code wrong? It should be

            public static ValidationResult Validate<T>(K entity)
            where T : IValidator, new()
            where K : class
            {
            IValidator __Validator = new T();
            return __Validator.Validate(entity);
            }

            As I get the Error: Constraints are not allowed on non-generic declarations, if I use the code from the blog...? I think I need to wait until the author gets back to me! I run into even more problems if i use this change!! :~ Thanks for your help tho

            T 1 Reply Last reply
            0
            • S Sevententh

              So is the above code wrong? It should be

              public static ValidationResult Validate<T>(K entity)
              where T : IValidator, new()
              where K : class
              {
              IValidator __Validator = new T();
              return __Validator.Validate(entity);
              }

              As I get the Error: Constraints are not allowed on non-generic declarations, if I use the code from the blog...? I think I need to wait until the author gets back to me! I run into even more problems if i use this change!! :~ Thanks for your help tho

              T Offline
              T Offline
              Tony Richards
              wrote on last edited by
              #6

              You could try:

              public static ValidationResult Validate<T, K>(K entity)
              where T : IValidator, new()
              where K : class
              {
              IValidator __Validator = new T();
              return __Validator.Validate(entity);
              }

              But, without more context this is nothing more than an educated guess, so the blog writer will definitly know best.

              S 1 Reply Last reply
              0
              • T Tony Richards

                You could try:

                public static ValidationResult Validate<T, K>(K entity)
                where T : IValidator, new()
                where K : class
                {
                IValidator __Validator = new T();
                return __Validator.Validate(entity);
                }

                But, without more context this is nothing more than an educated guess, so the blog writer will definitly know best.

                S Offline
                S Offline
                Sevententh
                wrote on last edited by
                #7

                Fantastic! that resolved all the other problems I was having with this code :-D Thanks

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups