Results 1 to 7 of 7

Thread: ByRef or ByVal? [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    ByRef or ByVal? [Resolved]

    Last question for today.

    VB Code:
    1. static void RunApp(object state)
    2.         {
    3.             Application.Run((Form)state);
    4.         }

    I am currently converting it to VB.NET, I'd like to know whether "state" is being passed ByRef or ByVal. Does C# have the concept of ByRef/ByVal?
    Last edited by mendhak; Nov 25th, 2004 at 07:29 AM.

  2. #2
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476
    I am not quite sure bout that but if I recall correctly,......

    if you pass an object it will always be passed by refrence.
    But yes you can also specify that using keywords ref (out) or val.

    heres an example:
    Code:
    		private void button2_Click(object sender, System.EventArgs e)
    		{
    			System.Xml.XmlDocument myDoc  = new System.Xml.XmlDocument();
    			testFunction(myDoc);
    			textBox2.Text = myDoc.SelectSingleNode("//test").InnerText;
    			
    		}
    		private void testFunction(System.Xml.XmlDocument myDoc)
    		{
    			myDoc.LoadXml("<test>value</test>");
    		}
    Please do corrcet me if I am wrong!

    Stephan
    Last edited by Sgt-Peppa; Nov 19th, 2004 at 07:49 AM.
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Reference types (Forms, other complex objects) will always be ByRef. Value types (primatives, structs) will always be ByVal unless the ref modifier is used.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    OK, so what I understand is that it's the same deal as in VB.NET, except it's taboo to mention it in C#.

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Yup

    Burstable Bandwidth

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Wont arrays also be essentially byref as well?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    That's what I assumed too.

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