Picturebox

  • Thread starter Thread starter cs
  • Start date Start date
C

cs

What's picturebox?

Reading MSDN, there is a function call defined as:

FunctionName(ref count)

What does "ref" here indicate?
 
cs said:
FunctionName(ref count)

What does "ref" here indicate?

The ref keyword indicates that you need to your parameter By Reference
instead of By Value. In the case of reference types, the difference is
academic.

However, with value types such as structs and primitives, it's possible
that the method you're calling will change the value of the parameter.
If so, that change will be reflected in the calling method.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Frank... Hmm. If you write a swap routine, by ref works and by value
does not. So I am not sure the difference is just academic.

Regards,
Jeff
The ref keyword indicates that you need to your parameter By
Reference instead of By Value. In the case of reference types, the
difference is academic.<
 
Jeff said:
Frank... Hmm. If you write a swap routine, by ref works and by value
does not. So I am not sure the difference is just academic.

You're going to have to provide an example as I do not understand what
you are referring to. When passing a reference type ByVal, only the
handle to the object is duplicated and passed. You're still manipulating
the original object. That sounds pretty academic to me.

ByRef and ByVal only have meaningful semantics when passing value types.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Frank... This is pretty obscure behaviour so don't sweat it.

using System;

namespace TestSwap
{
abstract class Drawable
{
abstract public void DrawMe();
}
class Circle : Drawable
{
public override void DrawMe()
{
System.Console.WriteLine("Circle");
}
}
class Square : Drawable
{
public override void DrawMe()
{
System.Console.WriteLine("Square");
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static public void SwapByValue(Drawable d1, Drawable d2)
{
Drawable temp= d1;
d1 = d2;
d2= temp;
}
static public void SwapByRef(ref Drawable d1, ref Drawable d2)
{
Drawable temp= d1;
d1 = d2;
d2= temp;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Drawable wasCircle= new Circle();
Drawable wasSquare= new Square();
wasCircle.DrawMe(); // Circle
wasSquare.DrawMe(); // Square
SwapByValue(wasCircle, wasSquare); // fails
wasCircle.DrawMe(); // Circle
wasSquare.DrawMe(); // Square
SwapByRef(ref wasCircle, ref wasSquare); // succeeds
wasCircle.DrawMe(); // Square
wasSquare.DrawMe(); // Circle
System.Console.ReadLine();
}
}
}


Regards,
Jeff
You're going to have to provide an example as I do not understand what
you are referring to.<
 
Frank Oquendo said:
You're going to have to provide an example as I do not understand what
you are referring to. When passing a reference type ByVal, only the
handle to the object is duplicated and passed. You're still manipulating
the original object. That sounds pretty academic to me.

ByRef and ByVal only have meaningful semantics when passing value types.

Not true at all. Simple example:

Foo f = new Foo();
SomeMethod (ref f);

...

void SomeMethod (ref Foo x)
{
x = null;
}

If the parameter were not passed by reference, there would be no change
to the value of f. As it is, f is changed to null.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
 
Jon said:
Not true at all. Simple example:

A conveniently simple example, at that. What you've demonstrated is that
you can chang what the copy of an object handle points to without
effecting the original object. However, that behavior changes as soon as
you manipulate an object's properties, quite in contradiction to what
quite a few people would expect. Here's an example.

using System;

namespace ConsoleApplication6
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Foo foo = new Foo();
foo.Text = "Old text";
Bar(foo);
Console.WriteLine(foo.Text);
Console.ReadLine();
}

public class Foo
{
private string _text;

public string Text
{
get { return _text; }
set { _text = value; }
}
}

static void Bar(Foo foo)
{
foo.Text = "New Text";
}
}
}

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Frank Oquendo said:
A conveniently simple example, at that. What you've demonstrated is that
you can chang what the copy of an object handle points to without
effecting the original object.

It doesn't change the "object handle" at all - it changes what value
the variable has.
However, that behavior changes as soon as
you manipulate an object's properties, quite in contradiction to what
quite a few people would expect.

Well, only if those people hadn't actually bothered to find out what
reference types are...

The point, however, is that passing a reference variable *by reference*
does something different to passing it by value, and the difference is
far from academic.
 
Back
Top