Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

In Java, you can do something like this:

 

new Form().show()

 

I don't remember the exact syntax because it's been a while since I worked with Java, but the point is, you can instantiate an object, including forms, and show them without ever assigning a variable name. If you never have to refer to them again, I don't see the harm in doing that (correct me if I'm wrong). I already tried it in .NET, and it resulted in a syntax error. Is there another way to do this? Or is there a reason that I shouldn't do it?

Posted

Instantiation without Declaration

 

I certainly wouldn't advocate such, but:

 

(new MyForm()).Show();

 

Always declare your variables! :D

 

This code is not so easy to read and might cause someone other than yourself to get tripped up. Inevitably, if you're a professional developer, your code will be read by someone else at some point. I see no real benefit in not declaring the variable.

Posted

Thank you for the help. I am assuming that is a C# statement because of the semicolon. I should have clarified that I am working with VB. It still gives me a syntax error in VB.

 

However, I disagree about the readability. I think that statement is pretty straightforward. I think anyone who has been programming with .NET for any length of time is going to be able to see that you are instantiating an object and showing it even though you aren't using a variable.

 

The only time I would use that is in a Sub Main() in order to create a new instance of that class. If that is the only class in my program, I am, basically, running the program. The object will be destroyed when the user closes the program.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...