11 Replies - 808 Views - Last Post: 02 March 2012 - 07:25 PM

#1 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

method parameter as 'ref'

Posted 28 February 2012 - 07:06 AM

another question I wish to ask:

what does the 'ref' means in the following syntax:

private void MoveForward(ref Vector3 position, Quaternion rotationQuat, float speed)
{
}
Is This A Good Question/Topic? 0
  • +

Replies To: method parameter as 'ref'

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: method parameter as 'ref'

Posted 28 February 2012 - 07:28 AM

Here's an MSDN link that goes into detail and could explain it a lot better than I can.
Was This Post Helpful? 0
  • +
  • -

#3 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: method parameter as 'ref'

Posted 28 February 2012 - 07:44 AM

it's either me .. or else MSDN is the worst teacher in the world!
Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: method parameter as 'ref'

Posted 28 February 2012 - 07:49 AM

Okay I'll try to explain it. The ref keyword simply tells the compiler that you're going to pass a variable to the method by reference as opposed to passing it by value. Passing it by value exposes the variable to the method with it's current value. Inside that method, you can use the variable any way you see fit, but the value of the variable that you passed in will not change once the method is exited. Passing by reference allows you to pass the variable to the method, do whatever needs to be done to it, and keep the changes made to that variable when the method is exited. Here's another MSDN link that explains passing parameters.
Was This Post Helpful? 1
  • +
  • -

#5 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: method parameter as 'ref'

Posted 28 February 2012 - 08:03 AM

I think I got what ur saying ...

when using ref, the global variable will be changed when the same variable's value is changed in a method.

if not using ref, a return type method is needed to change the global variable.

am i right? :)

can you specify similarities and differences between ref and out?
Was This Post Helpful? 0
  • +
  • -

#6 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: method parameter as 'ref'

Posted 28 February 2012 - 08:12 AM

The biggest difference between ref and out is that ref requires a variable that has already been initialized before it is passed in. They both cause the variable to be passed be reference, though. The out keyword allows you to have multiple values returned from a single method. Instead of just having a return statement at the exit of a method to return one value, you can pass in a parameter using the out keyword that will tell the method that it will be outputting a value to the variable that is passed by reference.

I'm curious as to why you've come here with these questions. Don't get me wrong I'm happy to help, but there are countless resources online that offer this information and I want to make sure that this isn't just a homework assignment that you don't want to do any research for and you'd rather us give you the answers. If you're just trying to increase your general knowledge of the C# language, then I'd suggest some Google searches to find information on the things you wish to know more about. If you run into a specific problem with code that you write, then come here looking for answers or explanations on why a certain piece of code is performing a certain way.
Was This Post Helpful? 1
  • +
  • -

#7 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: method parameter as 'ref'

Posted 28 February 2012 - 08:23 AM

rest assure it not homework .. i'm trying to learn xna from Riemers as we don't do this at school. We only learn C# as applications and web (ASP).

But you are right, I may have asked questions that some of which I might have found by googling. But theory is difficult to explained and learnt just by reading. I'm sry for the hassle!

Thanks never the less!
Was This Post Helpful? 0
  • +
  • -

#8 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: method parameter as 'ref'

Posted 28 February 2012 - 08:27 AM

It's no hassle, I just want to make sure that any help I offer isn't taking away from what someone could learn on their own, and I definitely do not want to do anyone's homework for them. Riemer definitely has an awesome site with tons of useful information and tutorials, so you're taking a step in the right direction by wanting to learn from him and his examples. Is there anything specific from his site that you might be stuck on and need a little help with? Or are you just gathering some deeper knowledge about C# and game development with XNA?
Was This Post Helpful? 0
  • +
  • -

#9 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: method parameter as 'ref'

Posted 28 February 2012 - 08:38 AM

your right on the latter one. It's just for my interest to be more confident with C# and learn something new at the same time - XNA.

No problems up untill now because as you said Riemers has a great site and I really doubt if anyone can get stuck since he explains it all. My questions are more specific because i don't accept the idea to have code without knowing what it is for.

I really wish there are sites like this about other subjects! Especially about WPF and MVVM as I will be trying to get a know how about these as well and hopefully build a small app or website with using these.

but of course thanks to dreamincode community as it connects everything that's left out unexplained from the net - from my view.
Was This Post Helpful? 1
  • +
  • -

#10 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: method parameter as 'ref'

Posted 28 February 2012 - 08:42 AM

Having a broader knowledge of C# is exactly what you will need as you start to learn more advanced topics of game development with XNA. Without a good foundation of understanding the underlying language, you're going to become frustrated when you get to things that you don't understand, so the more you learn at the beginning, the better. And of course we're always here to help point people in the right direction when/if they run into problems or just have questions, so feel free to become an avid member of the community and one day I'm sure you'll be offering advice and help to others as well.
Was This Post Helpful? 0
  • +
  • -

#11 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2528
  • View blog
  • Posts: 4,630
  • Joined: 24-September 10

Re: method parameter as 'ref'

Posted 28 February 2012 - 08:56 AM

I just figued I go a bit more in depth about 'ref'...



big use for a 'ref' is for value types. In .Net you have Structures and Classes, structures are considered value types. When you pass a structure value around it is copied every time you pass it, where as a class instance is passed around like an object and you have to explicitly state if you want to make a new copy.

Vector3 v1 = new Vector3(0,0,1);
Vector3 v2 = v1;
v2.Z = 2;

Console.WriteLine(v1.Z);//still 1
Console.WriteLine(v2.Z);//is 2

MyClassType obj1 = new MyClassType();
obj1.Z = 1;
MyClassType obj2 = obj1;
obj2.Z = 2;

Console.WriteLine(v1.Z);//changed to 2, it's the same object
Console.WriteLine(v2.Z);//is 2



Thing is there is a way to make a copy of a class instance (new, clone, etc), but what if you want a reference of a value type (a reference to an int, vector, etc). Well you create a 'ref' to it.

'ref' acts like a pointer to the variable you passed in as opposed to the value itself. This is like pointers (*) in C++. In C++ everything is passed by value unless explicitly told otherwise (via pointers), this meant structures and classes had little differences with each other on a technical level (though developers developed standards for each just for standards sake).

Anyways, it's useful if you want to alter what a variable points at, as opposed to the object it's pointing at. For value types this gives the illusion of editing the value passed... but actually you're editing the variable passed to be a new value. But with ref types like objects, well it's already a reference, so the point is moot... this means you seldom would pass in a class instance 'by ref', unless that is you wanted to change the object the variable was referencing. But then again, this is considered bad design by some.

void Entry()
{
    int value = 1;
    Foo(value);
    Console.WriteLine(value);//output: 1

    Bar(value);
    Console.WriteLine(value);//output: 5


    MyClassType obj = new MyClassType();
    obj.Z = 1;
    Foo(obj);
    Console.WriteLine(obj.Z);//output: 5

    MyClassType obj2 = obj;
    Barr(obj2);
    Console.WriteLine(obj1.Z);//output: 1
    Console.WriteLine(obj2.Z);//output: 5
    Console.WriteLine((obj1 == obj2));//output false

}


void Foo(int a)
{
    a = 5;
}

void Bar(ref int a)
{
    a = 5;
}



void Foo(MyClassType a)
{
    a.Z = 5;
}

void Bar(ref MyClassType a)
{
    a = new MyClassType();
    a.Z = 5;
}


This post has been edited by lordofduct: 28 February 2012 - 09:00 AM

Was This Post Helpful? 2
  • +
  • -

#12 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: method parameter as 'ref'

Posted 02 March 2012 - 07:25 PM

Okay, well here's a pretty simple, basic explanation of ref.

Having the ref keyword just basically means you are changing the variable that is put in when you put it through the function.


Vector2 position;

position = DoSomething(position);




Is pretty much the same as

Vector2 position;

DoSomething(ref position);




Hope this helps!

P.S. I agree, MSDN is a really bad teacher for people who are completely new to a concept.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1