Continuing my series of short but useful tips and tricks, one usually needs to find the directory of an executing assembly. The simple console application below shows how to do this. The Visual Basic example is after the C# one.
C#
Visual Basic
Continuing my series of short but useful tips and tricks, one usually needs to find the directory of an executing assembly. The simple console application below shows how to do this. The Visual Basic example is after the C# one.
A recurring requirement in every day programming is determining whether a value exists with a certain range. Since it is exam time (for most students at present), I resolved to write a quick application demonstrating this requirement based around examination marks.
This quick demo is written in WPF, but should work in Silverlight or Windows Phone 7 with no alteration. The Range class though, can be used throughout .NET in general.
To start with, create a new WPF application in visual Studio and call it ExamApp. Add a new folder to the project called Range and in this declare a new interface called IRange with the following members
Create a new class in the same folder called Range which implements the interface above
Add a new Folder to the project called Examinations and in this add a new class called Percentage. This Percentage class inherits off Range, and here you can create whatever logical range class you may desire for your own applications
using System;
Add a new enumeration called Grade thus
namespace ExamApp.Examinations
Add a new interface called ITest with the following members.
namespace ExamApp.Examinations
Add a new folder called Converters to the project, and add the following
using System;
As you can see most of the logic is executed here. Add a new class to the project called Student with the following properties. Also note that this implements the ITest interface that was declared earlier
using ExamApp.Examinations;
We need a collection of students to show in a list so create the following class that inherits off ObservableCollection<T>
using System.Collections.ObjectModel;
Now the data structures are complete, add the following .xaml markup
Add the following code to the constructor of the main window
You should have the following
As you can see it is quite a simple and pretty reusable way to use ranges in your application.
The source code for this simple example is available here
Presently, I am working on a scientific application, and have been creating some data tables. A request was made to me to ensure that some data columns be rounded to 3 significant figures e.g.
10.1234 becomes 10.1
10.7335 becomes 10.7
1.1003 becomes 1.10
I thought through this request with nonchalance at first, but was surprised just how difficult and time consuming it turned out to be to fulfil. After trying out many examples on the interwebs, including some lengthy code libraries, the solution was simplicity itself, using the .NET string format modifier G0 where the number after G is the number of significant figures desired, so
Console.WriteLine((10.12345).ToString(“G3”));
Will give you a result of 10.1
This works fine and as expected, but the bug is when you have 10.0, what is output to screen is 10
After traipsing through numerous articles, the result was that this behaviour was “by design”. After communicating this “by design” feature to my boss (and numerous scientists), their retort was that it was a bug and ought to be corrected. The reason why they were so annoyed was because the missing trailing zero indicates a level of accuracy that is an unacceptable omission in their field, thus I was advised to prefer two decimal places instead.
One of the first rules you are taught as a new programmer, is that numbers in computer programs are zero based i.e. you start counting at zero because zero is a number, so why in this case are the .NET framework designers ignoring the zero? One must say that one concurs with this being a bug, and wish the framework library team could correct this behaviour, to save developers polluting their codebases with conversions to strings and back to numbers, just so one can present data in a way that is meaningful to mathematically based users.