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. getting the name of a passed parameter

getting the name of a passed parameter

Scheduled Pinned Locked Moved C#
questionhelp
6 Posts 4 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.
  • R Offline
    R Offline
    Rbledwards
    wrote on last edited by
    #1

    I have a reporting interface that generates 100 or so reports. Each report uses a varying combination of 11 total parameters (from 0 - 4 at most). So I could have: Report report = new Report(StartDate, EndDate, NameID, LocID) //or just Report(StartDate, NameID) //or Report() My problem is: how do I get my constructor to accept the parameters and know which ones were passed without overloading it a gillion times. Is there a way to a generic type parameter and get the actual name of the parameter passed? If I have the name of the parameter I can cast to the right type. I could even create a hashtable with all my parameter names, types and build my AddParameter method off of the parameter name. Actually If I can get the name of the parameter, and I know the most that will be passed is four, five constructors and be good to go...I think. Is this possible? Robert

    A D J 3 Replies Last reply
    0
    • R Rbledwards

      I have a reporting interface that generates 100 or so reports. Each report uses a varying combination of 11 total parameters (from 0 - 4 at most). So I could have: Report report = new Report(StartDate, EndDate, NameID, LocID) //or just Report(StartDate, NameID) //or Report() My problem is: how do I get my constructor to accept the parameters and know which ones were passed without overloading it a gillion times. Is there a way to a generic type parameter and get the actual name of the parameter passed? If I have the name of the parameter I can cast to the right type. I could even create a hashtable with all my parameter names, types and build my AddParameter method off of the parameter name. Actually If I can get the name of the parameter, and I know the most that will be passed is four, five constructors and be good to go...I think. Is this possible? Robert

      A Offline
      A Offline
      albean
      wrote on last edited by
      #2

      Why not just pass it a collection or array that contains your "parameters".

      R 1 Reply Last reply
      0
      • R Rbledwards

        I have a reporting interface that generates 100 or so reports. Each report uses a varying combination of 11 total parameters (from 0 - 4 at most). So I could have: Report report = new Report(StartDate, EndDate, NameID, LocID) //or just Report(StartDate, NameID) //or Report() My problem is: how do I get my constructor to accept the parameters and know which ones were passed without overloading it a gillion times. Is there a way to a generic type parameter and get the actual name of the parameter passed? If I have the name of the parameter I can cast to the right type. I could even create a hashtable with all my parameter names, types and build my AddParameter method off of the parameter name. Actually If I can get the name of the parameter, and I know the most that will be passed is four, five constructors and be good to go...I think. Is this possible? Robert

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        Besides albean suggestion, which makes it very flexible for extending parameters, you can always set properties of a class and pass this class as a parameter to your Report constructor. Acting as a substitute for God, he becomes a dispenser of justice. - Alexandre Dumas

        1 Reply Last reply
        0
        • R Rbledwards

          I have a reporting interface that generates 100 or so reports. Each report uses a varying combination of 11 total parameters (from 0 - 4 at most). So I could have: Report report = new Report(StartDate, EndDate, NameID, LocID) //or just Report(StartDate, NameID) //or Report() My problem is: how do I get my constructor to accept the parameters and know which ones were passed without overloading it a gillion times. Is there a way to a generic type parameter and get the actual name of the parameter passed? If I have the name of the parameter I can cast to the right type. I could even create a hashtable with all my parameter names, types and build my AddParameter method off of the parameter name. Actually If I can get the name of the parameter, and I know the most that will be passed is four, five constructors and be good to go...I think. Is this possible? Robert

          J Offline
          J Offline
          Jim Stewart
          wrote on last edited by
          #4

          Martin Fowler had a solution for this type of problem in Refactoring with Introduce Parameter Object (p. 295). The concept is to "Create a new class to represent the group of parameters you are replacing."

          ReportParams rp = new ReportParams (StartDate, EndDate, NameID, LocID);
          Report report = new Report (rp);

          Your new parameter object could have all the permutations of parameters and remove those details from the Report class.

          α.γεεκ

          Fortune passes everywhere.
          Duke Leto Atreides

          R 1 Reply Last reply
          0
          • A albean

            Why not just pass it a collection or array that contains your "parameters".

            R Offline
            R Offline
            Rbledwards
            wrote on last edited by
            #5

            Excellent idea. Obviously never thought of it.

            1 Reply Last reply
            0
            • J Jim Stewart

              Martin Fowler had a solution for this type of problem in Refactoring with Introduce Parameter Object (p. 295). The concept is to "Create a new class to represent the group of parameters you are replacing."

              ReportParams rp = new ReportParams (StartDate, EndDate, NameID, LocID);
              Report report = new Report (rp);

              Your new parameter object could have all the permutations of parameters and remove those details from the Report class.

              α.γεεκ

              Fortune passes everywhere.
              Duke Leto Atreides

              R Offline
              R Offline
              Rbledwards
              wrote on last edited by
              #6

              Off to Amazon.com I guess. Thanks for the tip.

              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