BLOG.CSHARPHELPER.COM: Use overloaded methods to simulate optional parameters in C#
Use overloaded methods to simulate optional parameters in C#
Visual Basic allows you to define a method with optional parameters. If the calling code omits an optional parameter, the method uses a default value for it.
For example, suppose the DrawDiamond method draws a diamond shape and has the following signature.
The method takes as parameters the Graphics object on which to draw, a RectangleF giving where to draw, a Pen, and a Brush. If you omit the optional Brush, the method does not fill the diamond. If you also omit the Pen, the method draws the diamond with a black Pen and doesn't fill the diamond.
C# doesn't allow you to designate parameters as optional but sometimes you can overload a method to achieve a similar effect.
To overload a method, make multiple methods with the same name but different signatures. The methods' parameter lists must be different in data type or number so C# can tell which one you want to invoke based on the parameters that you pass to the method call.
The following code shows three overloaded versions of the DrawDiamond method. Notice how the first invokes the second, the second invokes the third, and the third does all of the real work. The first two methods are basically versions with missing parameters.
// Draw a diamond, optionally filling it. private void DrawDiamond(Graphics gr, RectangleF bounds) { DrawDiamond(gr, bounds, Pens.Black); } private void DrawDiamond(Graphics gr, RectangleF bounds, Pen diamond_pen) { DrawDiamond(gr, bounds, diamond_pen, Brushes.Transparent); } private void DrawDiamond(Graphics gr, RectangleF bounds, Pen diamond_pen, Brush diamond_brush) { // Make the diamond's points. PointF[] points = new PointF[4]; float xmid = (bounds.Left + bounds.Right) / 2; float ymid = (bounds.Top + bounds.Bottom) / 2; points[0] = new PointF(xmid, bounds.Top); points[1] = new PointF(bounds.Right, ymid); points[2] = new PointF(xmid, bounds.Bottom); points[3] = new PointF(bounds.Left, ymid);
// Fill the diamond. gr.FillPolygon(diamond_brush, points);
// Draw the diamond. gr.DrawPolygon(diamond_pen, points); }
The Random class's Next method generates random numbers. To make random words, make an array of letters. Then use a Random object to pick one of the letters to add to the word. Repeat until the word is as long as you need.
When you enter the number of words and the word length and click Go, the following code generates the random words and adds them to a ListBox. (In a real program you might want to do something else with the words such as write them into a file or put them in a list or array.
// Make the random words. private void btnGo_Click(object sender, EventArgs e) { lstWords.Items.Clear();
// Get the number of words and letters per word. int num_letters = int.Parse(txtNumLetters.Text); int num_words = int.Parse(txtNumWords.Text);
// Make an array of the letters we will use. char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
// Make a random number generator. Random rand = new Random();
// Make the words. for (int i = 1; i <= num_words; i++) { // Make a word. string word = ""; for (int j = 1; j <= num_letters; j++) { // Pick a random number between 0 and 25 // to select a letter from the letters array. int letter_num = rand.Next(0, letters.Length - 1);
// Append the letter. word += letters[letter_num]; }
// Add the word to the list. lstWords.Items.Add(word); } }
The example program uses the following Paint event handler to draw two diamonds, one with every parameter specified and one with the Pen and Brush parameters missing.
The program can also define the following version of DrawDiamond, which omits the Pen but takes a Brush as a parameter. The program can define this version because it's parameter list is different in type from the parameter lists used by all of the other versions.
Comments