Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 18th, 2003, 04:52 PM
Authorized User
 
Join Date: Sep 2003
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing Objects by value

Hi,

I have just got used to the idea where basic values may be sent by reference, using the keyword 'ref'.... but I was wondering how to pass class instances by value so that the actual data members are not effected.....???
 
Old December 18th, 2003, 05:18 PM
Authorized User
 
Join Date: Sep 2003
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again!

I have just been reading up and I was surprised to find out that by default both value and reference types are sent by value...... ???? I thought reference types were sent by reference by default.....?

some body please explain.... much appreciated......
 
Old December 20th, 2003, 02:03 AM
Registered User
 
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I don't think you can pass an obj and mess up data members. Passing the object allows you to access to another classes methods. For example:

class Class1

int Age = 37;

public void Display()
        {
            Console.WriteLine("Name: {0}\nSSN: {1}\nYour birthyear was: {2}", myName, mySSN, BirthYear.birthYear(this));
        }

    public class BirthYear
    {

        public static int birthYear(Class1 obj1)
        {
            return (2003 - obj1.myAge);
        }
    }

the object was created in Main and used to access the Display() method. The diplay method needs to call the birthYear() method but it is in a seperate class. We then can use the "this" keyword to pass the current object to the birthYear() method with a variable from the Class1 class.

I hope this helps...

Ray B.

 
Old December 20th, 2003, 05:27 AM
Authorized User
 
Join Date: Sep 2003
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much for replying... I was beginning to loose hope....

anyway.... unfortuantely I have to say that I wasn't able to quite grasp what you were saying.. please forgive me......

I just wanted to reconfirm that the default way to pass arguments was by value.... regardless of whether the arguments being passed are value or reference types....
 
Old December 22nd, 2003, 03:52 AM
Registered User
 
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To answer your question, don't use the ref or out modifiers. The object will not affect your data.


RSteel

 
Old December 25th, 2003, 06:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

If you dont specify anything reference types will be passed as references(e.g. classes) and value types will be passes as values (e.g. structs, common data types like int,long).

Ankur Verma
.Net and C++ Specialist
Wiley Tech Support
 
Old December 27th, 2003, 07:36 AM
Authorized User
 
Join Date: Sep 2003
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Then can you simply tell me.. how do we send reference types as value types...

the answer will be much appreciated......!!

 
Old December 30th, 2003, 06:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Aziz I've been going through you queries, modifiers for static constructors, namespaces, method hiding-- it seems the book you are referring is telling you the concepts but not the whys and whats around them. Your last query is reflective of the same fact - if you know what you want with your program you can always design it in such a way that you would never run into indecision situations like this. Then there are good programming practices that if you can follow your life as a programmer would be much easier.
When you learn concepts you must also learn why they are there and why something else isn’t. Complete your knowledge so to speak.
I wouldn’t suggest a professional level book to a beginner but I think you need Professional C# (wrox) this book has what you need and would stand you in good stead.

Ankur Verma
.Net and C++ Specialist
Wiley Tech Support





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing / Referencing Objects echovue Javascript 2 September 28th, 2007 09:28 AM
Passing objects between One Page to next page muralidharan.d ASP.NET 1.0 and 1.1 Basics 2 July 3rd, 2007 07:48 PM
passing jagged array of objects nutrino Beginning VB 6 0 January 27th, 2006 11:58 AM
Passing Objects nayan_k Classic ASP Basics 1 September 16th, 2003 01:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.