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. Passing a Class as a method parameter?

Passing a Class as a method parameter?

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

    I mean be an idiot for asking this question too but is there a way to pass a class as a parameter of a method i.e. I have a repeated block of code like this: class pTermsEditor; pTermsEditor = new class(); pnlViews.Controls.Add(pTermsEditor); pTermsEditor.Dock = DockStyle.Fill; This Block of code is repeated many times but the class type may and almose always does change, so I want to control it more by putting it into a method something like this: myMethod(class) { class x; x = new class(); //blah blah blah } Any help or even a better solution what be greatly appreciated

    T J J 3 Replies Last reply
    0
    • J joshp1217

      I mean be an idiot for asking this question too but is there a way to pass a class as a parameter of a method i.e. I have a repeated block of code like this: class pTermsEditor; pTermsEditor = new class(); pnlViews.Controls.Add(pTermsEditor); pTermsEditor.Dock = DockStyle.Fill; This Block of code is repeated many times but the class type may and almose always does change, so I want to control it more by putting it into a method something like this: myMethod(class) { class x; x = new class(); //blah blah blah } Any help or even a better solution what be greatly appreciated

      T Offline
      T Offline
      ToddHileHoffer
      wrote on last edited by
      #2

      You can use the object type, and it is a valid question. for example myMethod(object obj) { if (obj.GetType() == typeof(classA)) { ClassA objA; objA = (ClassA)obj; return; } if (obj.GetType() == typeof(classB)) { ClassB objB; objB = (ClassB)obj; return; } } how vital enterprise application are for proactive organizations leveraging collective synergy to think outside the box and formulate their key objectives into a win-win game plan with a quality-driven approach that focuses on empowering key players to drive-up their core competencies and increase expectations with an all-around initiative to drive up the bottom-line. But of course, that's all a "high level" overview of things --thedailywtf 3/21/06 -- modified at 14:58 Thursday 6th July, 2006

      1 Reply Last reply
      0
      • J joshp1217

        I mean be an idiot for asking this question too but is there a way to pass a class as a parameter of a method i.e. I have a repeated block of code like this: class pTermsEditor; pTermsEditor = new class(); pnlViews.Controls.Add(pTermsEditor); pTermsEditor.Dock = DockStyle.Fill; This Block of code is repeated many times but the class type may and almose always does change, so I want to control it more by putting it into a method something like this: myMethod(class) { class x; x = new class(); //blah blah blah } Any help or even a better solution what be greatly appreciated

        J Offline
        J Offline
        Josh Smith
        wrote on last edited by
        #3

        You can pass a Type object and then use Activator.CreateInstance to create an instance via the default (parameterless) constructor. Or, if you're using .NET 2, you can make a generic method whose type parameter is the type that you want to create. Then just do T obj = new T(); where T is your type parameter name. If all else fails, you can always do the magical type parameter dance and everything should work out fine. ;) :josh: My WPF Blog[^]

        J 1 Reply Last reply
        0
        • J joshp1217

          I mean be an idiot for asking this question too but is there a way to pass a class as a parameter of a method i.e. I have a repeated block of code like this: class pTermsEditor; pTermsEditor = new class(); pnlViews.Controls.Add(pTermsEditor); pTermsEditor.Dock = DockStyle.Fill; This Block of code is repeated many times but the class type may and almose always does change, so I want to control it more by putting it into a method something like this: myMethod(class) { class x; x = new class(); //blah blah blah } Any help or even a better solution what be greatly appreciated

          J Offline
          J Offline
          Jun Du
          wrote on last edited by
          #4

          C++/C# hasn't invented that feature yet :-D Best, Jun

          1 Reply Last reply
          0
          • J Josh Smith

            You can pass a Type object and then use Activator.CreateInstance to create an instance via the default (parameterless) constructor. Or, if you're using .NET 2, you can make a generic method whose type parameter is the type that you want to create. Then just do T obj = new T(); where T is your type parameter name. If all else fails, you can always do the magical type parameter dance and everything should work out fine. ;) :josh: My WPF Blog[^]

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            That'll work, so long as you put a new() (that is, default constructor) constraint on the method:

            void CreateNew<T>() where T : new()
            {
            T instance = new T();
            ...
            }

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            J 1 Reply Last reply
            0
            • J Judah Gabriel Himango

              That'll work, so long as you put a new() (that is, default constructor) constraint on the method:

              void CreateNew<T>() where T : new()
              {
              T instance = new T();
              ...
              }

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              J Offline
              J Offline
              Jun Du
              wrote on last edited by
              #6

              That's cool. Thought it wasn't doable :-O Best, Jun

              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