15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

egular expressions ❘ 217<br />

That is all you have to do! Notice how you take the precaution of checking whether format is null before<br />

you call any methods against this parameter — you want this method to be as robust as reasonably possible.<br />

The format specifiers for all the primitive types are case-insensitive, so that is the behavior that other<br />

developers are going to expect from your class, too. For the format specifier VE, you need each component to<br />

be formatted in scientific notation, so you just use String.Format() again to achieve this. The fields x, y,<br />

<strong>and</strong> z are all doubles. For the case of the IJK format specifier, there are quite a few substrings to be added<br />

to the string, so you use a StringBuilder object to improve performance.<br />

For completeness, you also reproduce the no-parameter ToString() overload developed earlier:<br />

public override string ToString()<br />

{<br />

return "( " + x + ", " + y + ", " + z + " )";<br />

}<br />

Finally, you need to add a Norm() method that computes the square (norm) of the vector because you didn’t<br />

actually supply this method when you developed the Vector struct:<br />

public double Norm()<br />

{<br />

return x*x + y*y + z*z;<br />

}<br />

Now you can try your formattable vector with some suitable test code:<br />

static void Main()<br />

{<br />

Vector v1 = new Vector(1,32,5);<br />

Vector v2 = new Vector(845.4, 54.3, -7.8);<br />

Console.WriteLine("\nIn IJK format,\nv1 is {0,30:IJK}\nv2 is {1,30:IJK}",<br />

v1, v2);<br />

Console.WriteLine("\nIn default format,\nv1 is {0,30}\nv2 is {1,30}", v1, v2);<br />

Console.WriteLine("\nIn VE format\nv1 is {0,30:VE}\nv2 is {1,30:VE}", v1, v2);<br />

Console.WriteLine("\nNorms are:\nv1 is {0,20:N}\nv2 is {1,20:N}", v1, v2);<br />

}<br />

The result of running this sample is this:<br />

FormattableVector<br />

In IJK format,<br />

v1 is 1 i + 32 j + 5 k<br />

v2 is 845.4 i + 54.3 j + -7.8 k<br />

In default format,<br />

v1 is ( 1, 32, 5 )<br />

v2 is ( 845.4, 54.3, -7.8 )<br />

In VE format<br />

v1 is ( 1.000000E+000, 3.200000E+001, 5.000000E+000 )<br />

v2 is ( 8.454000E+002, 5.430000E+001, -7.800000E+000 )<br />

Norms are:<br />

v1 is || 1050 ||<br />

v2 is || 717710.49 ||<br />

This shows that your custom specifiers are being picked up correctly.<br />

regular eXPressions<br />

Regular expressions are part of those small technology areas that are incredibly useful in a wide range of<br />

programs, yet rarely used among developers. You can think of regular expressions as a mini-programming<br />

language with one specific purpose: to locate substrings within a large string expression. It is not a new<br />

technology; it originated in the UNIX environment <strong>and</strong> is commonly used with the Perl programming language.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!