Results 1 to 9 of 9

Thread: [RESOLVED] Confirmation: Are objects always passed as ByRef?

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Resolved [RESOLVED] Confirmation: Are objects always passed as ByRef?

    Just want confirmations since this is to be the case...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Confirmation: Are objects always passed as ByRef?

    In C# you have to declare the argument using the "ref" keyword, otherwise parameters will be passed by value. Note that the implications of passing by reference and by value are slightly different for value types and reference types. It is the variable that you are passing by value or by reference. With value types the variable contains the object while with reference types the variable contains a reference to the object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Confirmation: Are objects always passed as ByRef?

    Why is it tha when I passed a custom collection of mine as a parameter to a method then added values to that collection then the added values appears to the passed collection?

    It is the same scenario as I described here...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Confirmation: Are objects always passed as ByRef?

    Um... that's a VB6 question. Why would that have anything to do with C# or .Net?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Confirmation: Are objects always passed as ByRef?

    Quote Originally Posted by dee-u
    Why is it tha when I passed a custom collection of mine as a parameter to a method then added values to that collection then the added values appears to the passed collection?
    As already mentioned by JM, when you pass reference types you are actually passing the reference of the object unlike value types where the type holds the values too. So as collection is a reference type when you pass it to another method and do some changes to it, you are actually changing the same object because both contain the reference to the same object.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Confirmation: Are objects always passed as ByRef?

    That's exactly why I specified that you are passing the VARIABLE by reference or by value, not the object. When you pass a reference type object by value you are passing a copy of the variable. That means you have two variables that refer to the same object. If you make changes to the object via that reference then that change will be reflected in the original. If you assign a new object to that variable then that change will not be reflected, because the original variable still refers to the original object. If you actually want to make changes to a reference type object past as a parameter without those changes being reflected in the original then you need to create a copy of the actual object and pass a reference to that. Why you would want to do this though I don't know.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Confirmation: Are objects always passed as ByRef?

    Hmmmnnn... Does it mean my business object is also of reference type?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Confirmation: Are objects always passed as ByRef?

    Every class is a reference type and every structure is a value type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Confirmation: Are objects always passed as ByRef?

    Ahhh... Thanks again!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width