Was writing a new method to open a custom dialog box I created, and realized that the other method I wrote to open another custom dialog had the same name as the object representing it, but there was no error?

This is what I mean:

Originally the code read:

        Dialogs.Accounts.NewLocation LocationEditor = new Dialogs.Accounts.NewLocation();
        LocationEditor.ShowDialog();

And after the editor evolved I decided to reuse the same form for creating new locations and editing existing ones, so to make the name of the form match it's purpose I changed it from NewLocation to LocationEditor. Now the code reads:

        Dialogs.Accounts.LocationEditor LocationEditor = new Dialogs.Accounts.LocationEditor(SelectedLocation, false);
        LocationEditor.ShowDialog();

There's no error with this? I don't get it, I thought this wasn't allowed? The object's name is LocationEditor and the instance of that object is LocationEditor...same name, same casing...I'm confused, if the method was longer than about 6 lines and the object was used over and over, how would the compiler know what I was talking about, the original object or the instance that already exists? Either way I'm going to change the name of the instance, but I just never caught this until now and just found it odd that it worked.

Recommended Answers

All 2 Replies

Notice that you are using a qualified name for the object (you are placing the namespace in front to specify it), thus in the scope of your code there is no class named 'LocationEditor' so the compiler has no problem with using it as a variable name. If, by chance, you were to put using Dialogs.Accounts; at the top of your code, then the class 'LocationEditor' would be visible (without having to qualify it) and the compiler should complain at that point.

Ahhh, yeah I thought about that, but wasn't sure. Still bizarre to me :) Thanks for the clarification!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.