Beginner programming question
-
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!
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 -
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, StringYou'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 )
-
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 )
-
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.
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 )
-
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, StringJordanwb 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 -
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 )
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
-
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, StringYou 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
-
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
-
Okay next time someone needs help I won't help him and tell him to stop being a mooch. There you happy?
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