Click here to Skip to main content
14,971,215 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
GeneralRe: Class data members Pin
led mike20-Aug-08 11:38
Memberled mike20-Aug-08 11:38 
AnswerRe: Class data members Pin
led mike20-Aug-08 11:41
Memberled mike20-Aug-08 11:41 
AnswerRe: Class data members Pin
Brady Kelly20-Aug-08 23:32
MemberBrady Kelly20-Aug-08 23:32 
GeneralRe: Class data members Pin
stavinski21-Aug-08 2:13
Memberstavinski21-Aug-08 2:13 
GeneralRe: Class data members Pin
Robert.C.Cartaino21-Aug-08 6:03
MemberRobert.C.Cartaino21-Aug-08 6:03 
AnswerRe: Class data members Pin
Paul Conrad21-Aug-08 17:00
professionalPaul Conrad21-Aug-08 17:00 
Questionsimple Source code managment software (free?) Pin
David Hovey19-Aug-08 12:48
MemberDavid Hovey19-Aug-08 12:48 
AnswerRe: simple Source code managment software (free?) Pin
Robert.C.Cartaino19-Aug-08 13:05
MemberRobert.C.Cartaino19-Aug-08 13:05 
GeneralRe: simple Source code managment software (free?) Pin
Paul Conrad23-Aug-08 20:08
professionalPaul Conrad23-Aug-08 20:08 
AnswerRe: simple Source code managment software (free?) Pin
ChandraRam21-Aug-08 3:35
MemberChandraRam21-Aug-08 3:35 
GeneralRe: simple Source code managment software (free?) Pin
David Hovey21-Aug-08 13:10
MemberDavid Hovey21-Aug-08 13:10 
AnswerRe: simple Source code managment software (free?) Pin
Netblue22-Aug-08 8:55
MemberNetblue22-Aug-08 8:55 
GeneralRe: simple Source code managment software (free?) Pin
Paul Conrad23-Aug-08 20:09
professionalPaul Conrad23-Aug-08 20:09 
GeneralRe: simple Source code managment software (free?) Pin
Mark Churchill24-Aug-08 17:28
MemberMark Churchill24-Aug-08 17:28 
AnswerRe: simple Source code managment software (free?) Pin
Steven A. Lowe13-Sep-08 16:54
MemberSteven A. Lowe13-Sep-08 16:54 
QuestionArguments Pin
CodingYoshi18-Aug-08 10:53
MemberCodingYoshi18-Aug-08 10:53 
AnswerRe: Arguments Pin
Robert.C.Cartaino19-Aug-08 7:54
MemberRobert.C.Cartaino19-Aug-08 7:54 
CodingYoshi wrote:
However, I also know that all objects are passed by reference.


That is not correct. By default, objects references are passed by value.

Do not confuse variable data types (value types vs. reference types) with argument passing mechanisms (pass-by-value vs pass-by-reference).

For example, arrays are reference types. When you create an array, a block of memory is allocated and your array variable contains a "reference" the to the memory area (typically a four-byte address). For example:

In Visual Basic:
Dim values(10) As Integer
In C#:
int[] values = new int[10];

The variable, values, contains a reference to a block of ten integers.

If you pass your array to a method as an argument, the reference to the array is passed by value (important). You can modify the elements of the underlying array but you cannot assign a new array to it.

To illustrate what I am talking about, take a look at this C# code:
using System;
class Program
{
    static void Main()
    {
        int[] values = { 1, 2, 3 };
        TestMethod(values);
        Console.WriteLine("{0}, {1}, {2}", values[0], values[1], values[2]);
    }

    static void TestMethod(int[] values)
    {
        int[] newArray = new int[] { 4, 5, 6 };
        values = newArray;
    }
}

This code will print "1, 2, 3". Why? First, an array containing { 1, 2, 3 } is created. The TestMethod() creates a new array with { 4, 5, 6 } and attempts to point your array reference to it. But when TestMethod() returns, the reference is not changed because the reference was passed by value.

However, you can change the underlying data pointed to by the reference. If TestMethod() did this:
static void TestMethod(int[] values)
{
    values[0] = 4;
    values[1] = 5;
    values[2] = 6;
}
... the values would be changed (i.e., it prints "4, 5, 6") because you passed a reference to the data contained in values, which TestMethod() is free to change.
CodingYoshi wrote:
when arguments are passed byVal it hinders performance since a copy needs to be made


Nah. Performance differences are usually insignificant. Maybe if you need to pass really large value types (like large structures) and you really need to tune for performance, you could pass those value types ByRef. But, more often than not, it is much more important to communicate your intentions to the programmer (to protect the parameter's value or not) rather than prematurely optimizing your code.
CodingYoshi wrote:
If a method only needs to read an argument but does not need to modify it, does it make sense to send it byref?


No. Passing arguments ByRef or ByVal should declare your intentions to protect (or not protect) the value of the parameters you are passing. It will improve the readability and safety of your code.

Enjoy,

Robert C. Cartaino
QuestionIndex Server search algorithms - or something like it... Pin
Michael Yeaney16-Aug-08 17:09
MemberMichael Yeaney16-Aug-08 17:09 
AnswerRe: Index Server search algorithms - or something like it... Pin
Pete O'Hanlon17-Aug-08 9:13
mvePete O'Hanlon17-Aug-08 9:13 
QuestionMethod Parameters Pin
CodingYoshi15-Aug-08 4:06
MemberCodingYoshi15-Aug-08 4:06 
AnswerRe: Method Parameters Pin
Pete O'Hanlon15-Aug-08 4:12
mvePete O'Hanlon15-Aug-08 4:12 
QuestionInterfaces Pin
RichardGrimmer13-Aug-08 0:55
MemberRichardGrimmer13-Aug-08 0:55 
AnswerRe: Interfaces Pin
Pete O'Hanlon13-Aug-08 1:02
mvePete O'Hanlon13-Aug-08 1:02 
GeneralRe: Interfaces Pin
RichardGrimmer13-Aug-08 3:15
MemberRichardGrimmer13-Aug-08 3:15 
GeneralRe: Interfaces Pin
Pete O'Hanlon13-Aug-08 10:37
mvePete O'Hanlon13-Aug-08 10:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.