1. Technology

Learning to use structures by coding one for complex numbers

By permission of the VB.NET author Tim Patrick

By

Updated August 10, 2008

Tim Patrick's book, "Visual Basic 2005 Cookbook" (reviewed here) contains a great example showing how to use structures in Visual Basic .NET. And if you're a little curious about a mathematical idea called "complex numbers" then you can learn about both at the same time. If you need to know how to use complex numbers in a VB program, this is your article!

Tim Patrick's books are on my mind right now because O'Reilly has just published his latest book called "Programming Visual Basic 2008" (978-0-596-51843-1). This book uses a single project, a system to manage office functions in a library, as a basis for explaining what looks to be an "a to z" explanation of everything you need to know about Visual Basic including the new enhancements for 2008. And all this is packed into book that is a reasonable size and won't break your arm when you read it.

I'll publish a complete review of his latest book in a few days, but for now, I received permission from Tim to use this example from his earlier "Cookbook". His code was written for VB 2005, but I'm using VB 2008 Express to write this article.

There's no native support for complex numbers in the .NET Framework, or in any of Microsoft's products. But that doesn't mean they're not important. As Wikipedia notes, "Complex numbers are used in many different fields including applications in engineering, electromagnetism, quantum physics, applied mathematics, and chaos theory." In fact, you may remember beautiful mathematical pictures that were the result of something called "the Mandlebrot Set". If you decide to write a program to display these fractal pictures, you'll need complex numbers.

I think it's necessary to at least give a very brief description of a complex number before getting into the code to create them in VB. In math, a complex number is a "two part" number that is normally written:

a + bi

The symbols a and b are "real" numbers (think of them as being just ordinary numbers) and i is a special number that is defined as:

i ^ 2 = -1

In words, "i squared is equal to minus one". This is a math equation, not a VB statement.

If you play around with this equation for a while, you'll convince yourself that there is no real number that you can multiply by itself and get a minus one. So i is traditionally called "imaginary". But like Wikipedia notes, "the discovery of quantum mechanics, showed that nature has no preference for 'real' numbers and its most real descriptions often require complex numbers, the 'imaginary' part being just as physical as the 'real' part."

Anyway ... enough math. Well ... almost enough. The standard math operations of addition, subtraction, multiplication and division are defined for complex numbers too, but the operations themselves are quite different. It's these operations that are not natively supported in Visual Basic. So if you're going to write a program to display Mandlebrot images or use complex numbers in any other way, you'll need some code for these operations. That's what this structure does.

Here are some of the coding concepts, in addition to coding a structure, that this example illustrates:

* Overloading the New method in the definition of the structure to allow a new complex number to be created by passing the 'real' and 'imaginary' part, or another complex number. I added some code to Tim's structure to allow negative numbers to be handled better and to allow a New method using a String parameter in the form "a + bi" as well.

* Operator Overloading. This technique is only available in .NET. I described the technique in this article but this is a better example than the one I used there.

* String Manipulation. The code I wrote to handle a New method using a String parameter illustrates string parsing.

The code is explained starting on the next page.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. using structures - Write the code for complex numbers and learn structures too!

©2014 About.com. All rights reserved.