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. Is it possible to write such a methode?

Is it possible to write such a methode?

Scheduled Pinned Locked Moved C#
question
6 Posts 6 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.
  • M Offline
    M Offline
    MarkPhB
    wrote on last edited by
    #1

    I want to write a methode where i can deliver named parameter like this

    private void MethodeName( aVar = 10, bVar = "20", cVar = 30,  = , ... ){}
    

    where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)

    L T S E R 5 Replies Last reply
    0
    • M MarkPhB

      I want to write a methode where i can deliver named parameter like this

      private void MethodeName( aVar = 10, bVar = "20", cVar = 30,  = , ... ){}
      

      where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Standard CLR languages do not support this, but nothing can stop you from creating a new language and a compiler for it.

      Luc Pattyn [Forum Guidelines] [My Articles]


      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


      1 Reply Last reply
      0
      • M MarkPhB

        I want to write a methode where i can deliver named parameter like this

        private void MethodeName( aVar = 10, bVar = "20", cVar = 30,  = , ... ){}
        

        where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)

        T Offline
        T Offline
        Talal Sultan
        wrote on last edited by
        #3

        I don't think you can write that in C#. Attributes are a special case I suppose. Why would you need such a method anyway? :confused:

        MarkPhB wrote:

        where aVar, bVar and cVar are properties of the class.

        Do you mean properties as in get-set or just fields. In the first case, you could just use the properties as they were designed to be used: myClass.aVar = 10, etc...It's a good practice in this case to capitalize the first letter of the property name. In the second case, you could encapsulate the field in a property get-set and use it as described in the first case.

        -- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

        1 Reply Last reply
        0
        • M MarkPhB

          I want to write a methode where i can deliver named parameter like this

          private void MethodeName( aVar = 10, bVar = "20", cVar = 30,  = , ... ){}
          

          where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)

          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          Based on your example code, you are showing that you want to provide default values to the function parameters when you define the method. To do this you need to provide different overloads to the function. Based on your explanation, you want to call a method and pass property initializers to it. You can do something close, but you will need to wait until the .NET Framework v3.5 is released (or use the Beta 2 release). The reason I say "close" is that you can only do this on object creation. For example, if you have a class

          public class UserProfile
          {
          public int UserId { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
          pubic UserProfile() { }
          }

          You can create a new instance like this

          UserProfile profile = new UserProfile
          {
          UserId = 123
          FirstName = "John"
          LastName = "Smith"
          };

          Scott.


          —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

          1 Reply Last reply
          0
          • M MarkPhB

            I want to write a methode where i can deliver named parameter like this

            private void MethodeName( aVar = 10, bVar = "20", cVar = 30,  = , ... ){}
            

            where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)

            E Offline
            E Offline
            Eduard Keilholz
            wrote on last edited by
            #5

            However you can create a method with an undefined number of arguments : private void MyBike(string one, params object[] BikeParts) { foreach (Object BikePart in BikeParts) { } } however you cannot assign names to them and they must be of the same type

            .: I love it when a plan comes together :. http://www.zonderpunt.nl

            1 Reply Last reply
            0
            • M MarkPhB

              I want to write a methode where i can deliver named parameter like this

              private void MethodeName( aVar = 10, bVar = "20", cVar = 30,  = , ... ){}
              

              where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)

              R Offline
              R Offline
              Rudolf Jan
              wrote on last edited by
              #6

              This does not look like acceptable syntax. In C# you always should specify the parameter type in the declaration. C# does not support default parameters. If you intend your sample to mean a method call, private void are irrelevant qualifiers. I don't thing C# supports method calls with this syntax.

              Rudolf Heijink

              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