mjb3030 Posted November 20, 2003 Posted November 20, 2003 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? Quote
BenH Posted November 20, 2003 Posted November 20, 2003 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. Quote
mjb3030 Posted November 20, 2003 Author Posted November 20, 2003 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. Quote
Machaira Posted November 20, 2003 Posted November 20, 2003 You can do: Public Sub Main() Application.Run(New Form1()) End Sub Quote Here's what I'm up to.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.