Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using New but not set to an instance? 1

Status
Not open for further replies.

JoeAtWork

Programmer
Jul 31, 2005
2,285
CA
I’m studying an example of MVP code, the following looks wrong to me (although it works fine):

Code:
      PuzzleForm view = new PuzzleForm();
      IPuzzleModel model = new PuzzleModel(loadImageModel, imageCutter);
      new PuzzlePresenter(model, view);

On the third line, why isn’t it creating an instance of PuzzlePresenter? I have not seen this syntax before. I would expect something like:

Code:
PuzzlePresenter myPresenter = new PuzzlePresenter(model, view);

Can you “new” something but it’s not set to anything? Coming from a VB6 background, it just feels wrong to me.

So is it that there is an instance created, but I just don't have a handle to it via a variable?

Is the scope of this instance within the subroutine where it is created?

 
Put a breakpoint in on the 3rd line and see what item is null. It may be something inside the creation of one of your objects. Take a look in the constructor of the PuzzlePresenter class.

 
I'm sorry, rereading my question I see it is completely misleading.

There is nothing wrong with the code, it works just fine. I am just confused about the syntax, i.e. using the "New" keyword but not setting it to a variable.

 
You can do that and use the class in that single line for the scope of that line.

For example:

string type = new MyObject().GetType().ToString();

Same as you could do:

Console.WriteLine(new MyObject().GetType().ToString());

Typically you would want to assign it to a variable but you don't have to.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top