C# QA Part - Four

Q. Should I always try to place code into instance classes rather than static classes?

A. Not necessarily. As with most things, there are no hard and fast rules. Correctly programming instance classes takes some skill

and experience, and programming static is easier for the beginner. If you want to experiment with instance classes, I encourage you to

do so. However, don't feel as though you have to place everything into instantiated classes.

Q. I want to create a general class with a lot of miscellaneous methodssort of a "catchall" class. What's the best way to do this?

A. If you want to create some sort of utility class, I recommend calling the class something like clsUtility. Then you can use this class

throughout your application to access the utility functions.

Q. Is it possible to capture keystrokes at the form level, rather than capturing them in control events?

A. Yes. For the form's keyboard-related events to fire when a control has the focus, however, you must set the form's KeyPreview

property to true. The control's keyboard events will still fire, unless you set KeyPressEventArgs.Handled to true in the control's

KeyPress event.

Q. You don't seem to always specify a button in your MessageBox.Show() statements throughout this book. Why?

A. If you don't explicitly designate a button or buttons, Visual C# displays the OK button. Therefore, if all you want is an OK button,

you don't need to pass a value to the Buttons argument.

Q. What if I need to draw a lot of lines, one starting where another ends? Do I need to call DrawLine() for each line?

A. The Graphics object has a method called DrawLines(), which accepts a series of points. The method draws lines connecting the

sequence of points.

Q. Is there a way to fill a shape?

A. The Graphics object includes methods that draw filled shapes, such as FillEllipse() and FillRectangle().

Q. What if I want to perform an operation on a file, but something is preventing the operation, such as the file might be open or I

don't have rights to the file?

A. All the method calls have one or more exceptions that can be thrown in the event that the method fails. These method calls are

listed in the online help. You can use the techniques discussed in Hour 15 to trap the exceptions.

Q. What if a user types a filename into one of the file dialog boxes, but the user doesn't include the extension?

A. By default, both file dialog controls have their AddExtension properties set to true. When this property is set to TRue, Visual C#

automatically appends the extension of the currently selected filter.

Q. Can I use a text file to save configuration information?

A. Yes, you could do that. You would need some way to denote the data element. For example, how would you know that the first

line was the BackColor setting, as opposed to a default file path, for example? One method would be to append the data element to a

caption, as in BackColor=White. You would then have to parse the data as you read it from the text file. The Registry is probably a

better solution for something like this, but a text file could be useful if you wanted to transfer settings to a different computer.

Q. Can I store binary data instead of text to a file?

A. Visual C# 2005 includes classes designed to work with binary files: BinaryWriter and BinaryReader. You would need to use

objects based on these classes, instead of using StreamWriter and StreamReader objects.

No comments: