Related Articles

5 users responded in this post

Subscribe to this post comment rss or trackback url
User Gravatar
krftsman said in November 20th, 2007 at 7:30 am

How ya likin’ that C#?

User Gravatar
dante8 said in November 20th, 2007 at 11:02 am

I really like it… there are a few irritating things, but by and large it is a very well thought out language.

One of the things that is driving me crazy is that I made a custom user control and got that implemented just fine. My main form window is adding instances of these controls to a FlowLayoutPanel, but I wanted to design some controls where I could go through them and affect the individual items in the panel (labels, buttons, etc).

I figured that since adding them to panel1.Controls.Add(p2) seems to collect them in some fashion, I could do what Java can and just get to them from that Controls stack, cast them back to their original type, and use all of the elements within the panel. For the life of me I can’t seem to figure out how to actually make that work. Instead, I built some logic into the panel to be aware of its previous and next panel, and am handling it within the panel instance. It’s not ideal, because it is interweaving a lot of controller logic in with how the view is presented, but again… I can’t figure it out.

I’ve found that the online documentation is adequate, however when it comes to specific issues it can be rather hard to find some help. I was using a MaskedTextBox to do some input validation, and it took me around an hour to find an example where someone showed how to make it default to the front of the textbox mask instead of wherever the user clicked. I tried all the obvious things, but in the end it came down to some gratuitous hack using the selection elements of the text box. Not very well designed, IMHO.

Aside from those irrtating things, I *LOVE* (with hearts and stars flying off of it) the visual form editor. If Java had something worthwhile like that available I’d be a happy, happy man. Syntactically, the two languages (C# and Java) are so similar that it almost isn’t worth mentioning. I think that Eclipse’s error reporting and debugging tools are a little more feature rich, but the C# ones are certainly usable.

So there ya have it… those are my thoughts. :)

User Gravatar
krftsman said in November 20th, 2007 at 7:49 pm

I’m not sure exactly what you are trying to do, but, from your example, assuming p2 is another panel, you’d have to do something like (and this is off-the-cuff):

foreach(Panel p in panel1.Controls)
{
foreach(Control c in p.Controls)
{
if (c is Button)
// do whatever
else if (c is Textbox)
// do whatever
}
}

Essentially, it should be almost exactly like Java. If I was there and could see specifically what you are doing, I could tell you exactly how to do it.

For help, the MSDN is outstanding as far as specific info on the libraries themselves, but I, too, have found some of the examples to be over-simplified. I often frequent the microsoft.public.dotnet.csharp.* newsgroups for help.

I do have to take offense at your debugging statement. There is no debugger on earth that beats the debugger in Visual Studio. Last time I used eclipse (about 1 year ago), that sorry example of debugger it had wasn’t even worth using. ;p

User Gravatar
dante8 said in November 20th, 2007 at 8:32 pm

Yeah, that’s effectively what I did… a little less elegant than that. I would prefer if there was a way to reconstitute c into its unique class (like Button, or TextBox) but casting them always seems to freak it out when I do it.

Well, I guess I can see your point about the debugger, but I hate how the app tends to destructively crash instead of get handled by the IDE in such a way that you can end it gracefully. That part I did like about Eclipse, there wasn’t a 30 second lag period while Windows figures out how to resolve this bad piece of code that I did.

Good stuff, and a good learning experience!

User Gravatar
krftsman said in November 20th, 2007 at 10:58 pm

Look into the ‘is’ and ‘as’ operators. I think that’s what you’re looking for. Simply casting the c to a type will ‘freak out’ because c may not be the type you are trying to cast it to. The as operator will perform the cast, but simply return null if the cast is invalid rather than throwing an exception.

I’m not sure what that lag is about on exceptions, but I do notice that as well. I counteract that by never writing bad code. ;p

Leave A Reply

 Username (Required)

 Email Address (Remains Private)

 Website (Optional)