0

I'm trying to get to the just of the Out parameter. I have seen many examples, but they all have a method where there is no new class that is used..

I get 2 errors when calling this test code:

The out parameter 'w' must be assigned to before control leaves the current method.
Use of unassigned out parameter 'w'.



public partial class Form1 : Form

{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        Widget newWidget;
        Widget.Create(out newWidget);

    }
}
class Widget
{
    private String name;
    public String Name { get { return name; } set { name = value; } }

    public static void Create(out Widget w)
    {
        w.name = "1";

    }
}

This works , but what does it help?

  public static void Set(out Widget w)
        {

            Widget x = new Widget();
            x.name = "1";
            w = x;

        }
Peter PitLock
  • 1,823
  • 7
  • 34
  • 71
  • What is the question? What isn't clear? – Amit Apr 09 '16 at 19:08
  • Think Im missing a basic concept, cannot figure out why I am getting the 2 errors mentioned – Peter PitLock Apr 09 '16 at 19:11
  • The error messages are very clear. Your new (edited) question is again unclear – Amit Apr 09 '16 at 19:29
  • i made sense of the error messages now, and fixed the project so it works. Now battling to see what the use of out is, if Im going to create a new Widget in the Create method anyway. But i'll prob have to delete the question as it sounds like i did not ask a good question. – Peter PitLock Apr 09 '16 at 19:33
  • Don't use `out` here... just return the widget. Unless you're working on really low level code, you do not need to use `out` parameters. – Jeff Mercado Apr 09 '16 at 19:38
  • Im getting quizzed soon on Out parameter for a test, wanted to understand more of the subject. All examples are primitive, wanted to get deeper more realistic understanding on subject. – Peter PitLock Apr 09 '16 at 19:46
  • All you really need to know about `out` parameters is that the method that declares them _must_ set the parameter to some value, just like a method that returns a value must actually return a value. Nowadays, you never really need to use them as there are better ways to achieve the same result that they were designed to accomplish. – Jeff Mercado Apr 09 '16 at 19:51
  • Thanks Jeff, with the last code addition i included, would that pass or is it missing the point? – Peter PitLock Apr 09 '16 at 19:54
  • Yeah, that would work. You declared `w` to be an out parameter, and you set it so it should be fine. If you're still not clear on the purpose of the `out` parameter, I go over the motivations of its inclusion [in this question](http://stackoverflow.com/q/4102892/390278). (FYI, if you want to make sure that I get a notification that you left a comment, use `@username` in your comment to make sure it gets to who you want. I wouldn't have seen your comment otherwise, but I just happen to have this question still open) – Jeff Mercado Apr 09 '16 at 20:00

2 Answers2

1

This code does not make sense. The out parameter are usually used for value types (primitives) and not to reference types.

A value type, for example an int, are copied into a method and updates to this variable does not affect the original one. To update original value you can use the out parameter.

You can rewrite your code to look something like this instead.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Widget newWidget = new Widget("nameOfWidget");
    }
}

class Widget
{
    private String name;
    public String Name { get { return name; } set { name = value; } }

    public Widget(string nameOfWidget)
    {
        name = nameOfWidget;
    }
}
Hypnobrew
  • 1,120
  • 9
  • 23
  • "The out parameter are usually used for value types (primitives) and not to reference types." ...no, the `out` parameter is used for any type, whether it be a value or reference type. You're confusing this with mutable objects or something else. – Jeff Mercado Apr 09 '16 at 20:06
  • Well, I wrote "usually", but I'm aware there could be other case where it actually makes sense. Since the widget in this example is a reference type it is just a very odd way of instantiating the widget class. Maybe he's looking into a factory class/method but it doubt it... – Hypnobrew Apr 09 '16 at 20:20
0

The example you mentioned shows only primitive types. Assigning primitive types can be done the following way, both are equal:

i = 44;
i = new System.Int32(44);

As you can see, a new integer has been created. You have to do the same with your Widget Class before leaving the method. Hope this helps to clarify the MSDN examples.

Bastian
  • 1
  • 1