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. Beginner programming question

Beginner programming question

Scheduled Pinned Locked Moved C#
csharphelpquestionlearning
10 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.
  • C Offline
    C Offline
    cmh623
    wrote on last edited by
    #1

    I need to write a C# method that will double the values of 2 variables A and B in the calling program by passing these parameters by reference. I'm not really sure where to begin on this, any help would be greatly appreciated!

    J 1 Reply Last reply
    0
    • C cmh623

      I need to write a C# method that will double the values of 2 variables A and B in the calling program by passing these parameters by reference. I'm not really sure where to begin on this, any help would be greatly appreciated!

      J Offline
      J Offline
      Jordanwb
      wrote on last edited by
      #2

      Function: public void doub(ref int a, ref int b) { a *= 2; b *= 2; } and you would call it like this: doub (ref var_name1, ref var_name2); In my opinion using a function to do that is overkill. Keep in mind that some variable types pass by reference and not value, an example would be String (capital S). If the data type is dark blue it's a primitive (pass by value), if it is light blue it is an object (passes by reference). Example of primitive types: boolean, int, char Example of Objects: Form, String

      C D P 3 Replies Last reply
      0
      • J Jordanwb

        Function: public void doub(ref int a, ref int b) { a *= 2; b *= 2; } and you would call it like this: doub (ref var_name1, ref var_name2); In my opinion using a function to do that is overkill. Keep in mind that some variable types pass by reference and not value, an example would be String (capital S). If the data type is dark blue it's a primitive (pass by value), if it is light blue it is an object (passes by reference). Example of primitive types: boolean, int, char Example of Objects: Form, String

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        You've obviously just done this guy's homework for him, I can't see any real world reason to be asking for what he did.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        J 1 Reply Last reply
        0
        • C Christian Graus

          You've obviously just done this guy's homework for him, I can't see any real world reason to be asking for what he did.

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          J Offline
          J Offline
          Jordanwb
          wrote on last edited by
          #4

          Christian Graus wrote:

          You've obviously just done this guy's homework for him, I can't see any real world reason to be asking for what he did.

          Ahem:

          jordanwb wrote:

          In my opinion using a function to do that is overkill.

          C 1 Reply Last reply
          0
          • J Jordanwb

            Christian Graus wrote:

            You've obviously just done this guy's homework for him, I can't see any real world reason to be asking for what he did.

            Ahem:

            jordanwb wrote:

            In my opinion using a function to do that is overkill.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            Precisely. I agree, it's overkill, and therefore, odds are 99% that you did his homework.

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            P 1 Reply Last reply
            0
            • J Jordanwb

              Function: public void doub(ref int a, ref int b) { a *= 2; b *= 2; } and you would call it like this: doub (ref var_name1, ref var_name2); In my opinion using a function to do that is overkill. Keep in mind that some variable types pass by reference and not value, an example would be String (capital S). If the data type is dark blue it's a primitive (pass by value), if it is light blue it is an object (passes by reference). Example of primitive types: boolean, int, char Example of Objects: Form, String

              D Offline
              D Offline
              DavidNohejl
              wrote on last edited by
              #6

              Jordanwb wrote:

              In my opinion using a function to do that is overkill.

              In this case I agree, I'd use even extremely simple function if it increased readability, which is not the case. Performance-wise it's equal, this is going to be inlined.

              Jordanwb wrote:

              Keep in mind that some variable types pass by reference and not value, an example would be String (capital S). If the data type is dark blue it's a primitive (pass by value), if it is light blue it is an object (passes by reference).

              First, .NET System.String type and C# string type are exactly THE SAME type. And it's passed by reference. There happen to be two primitive types that are not value types - string and object. Whenever you make your own type that derives (even indirectly) from ValueType, its passed by value. And guess what, it appears light blue in editor. So wrong, wrong and wrong. :sigh:


              [My Blog]
              "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - RĂ¼diger Klaehn
              "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

              1 Reply Last reply
              0
              • C Christian Graus

                Precisely. I agree, it's overkill, and therefore, odds are 99% that you did his homework.

                Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                P Offline
                P Offline
                Paul Conrad
                wrote on last edited by
                #7

                Christian Graus wrote:

                odds are 99% that you did his homework.

                I agree and he probably also helped him not be able to solve a problem on his own either.

                "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                1 Reply Last reply
                0
                • J Jordanwb

                  Function: public void doub(ref int a, ref int b) { a *= 2; b *= 2; } and you would call it like this: doub (ref var_name1, ref var_name2); In my opinion using a function to do that is overkill. Keep in mind that some variable types pass by reference and not value, an example would be String (capital S). If the data type is dark blue it's a primitive (pass by value), if it is light blue it is an object (passes by reference). Example of primitive types: boolean, int, char Example of Objects: Form, String

                  P Offline
                  P Offline
                  Paul Conrad
                  wrote on last edited by
                  #8

                  You probably just did his homework assignment for him and enabled himn to learn how to mooch off of other people when he can't solve his own problem.

                  "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                  J 1 Reply Last reply
                  0
                  • P Paul Conrad

                    You probably just did his homework assignment for him and enabled himn to learn how to mooch off of other people when he can't solve his own problem.

                    "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                    J Offline
                    J Offline
                    Jordanwb
                    wrote on last edited by
                    #9

                    Okay next time someone needs help I won't help him and tell him to stop being a mooch. There you happy?

                    P 1 Reply Last reply
                    0
                    • J Jordanwb

                      Okay next time someone needs help I won't help him and tell him to stop being a mooch. There you happy?

                      P Offline
                      P Offline
                      Paul Conrad
                      wrote on last edited by
                      #10

                      No, help them, but don't do it for them.

                      "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                      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