"Lebesgue" <nos...@spam.jp> wrote:
What internet browser are you using? What spyware protection / firewall
software are you using (these can block popup windows)?
And finally, since this is a C# group, what was the question you got stuck
on? Maybe people here could tell you what the correct answer is and why-
The reason for asking here is that maybe someone has encountered this
problem and figured out a solution (it would avoid me asking :-).
This is the TestOut Question
Q:
You have been asked to create a data type that can easily store and
convert distances of various kinds. Because a distance only consists of
a length value and associated units, a struct type seems to be a good
fit.
Do the following to create the needed struct data type:
Create a new class file called Distance.
Define a public enum called DistanceUnits with Inches, Feet, and Meters
as named values.
Create a public struct called Distance.
Define a public field in the Distance struct called Value of type
double.
Define a public field in the Distance struct called Units of type
DistanceUnits.
Define a constructor in Distance that will set the Value and Units
fields (in that order).
Define a public method in Distance called Convert that takes a
parameter of type DistanceUnits that identifies a new unit of measure.
The Convert method should do the following:
Convert the Value field from the current instance to the new unit of
measure identified by the parameter.
Create a new instance of Distance setting the Value and Units in the
new instance based on the converted value and the new unit of measure.
Return the new Distance struct.
A simple way to convert from one unit of measure to another is to use a
switch statement to convert the current value to an intermediate value
in known units (such as inches). Then use a second switch statement to
convert the intermediate value to the requested new unit of measure.
Use the following conversion values: 12 inches = 1 foot; 39.37 inches =
1 meter.
Run the test program to verify your code. Enter a distance value in the
edit box and select an original and new unit of measure from the drop
down list boxes. The converted distance should appear.
The simulation has a form consisting of the following:
Type: Label, Name: Label1, Text: Distance:
Type: TextBox, Name: valueTextBox, Text: 0
Type: ComboBox, Name: fromComboBox
Type: Label, Name: valueLabel, Text: ?
Type: ComboBox, Name: toComboBox
The code behind the form is the one below:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Structs
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label Label1;
private System.Windows.Forms.TextBox valueTextBox;
private System.Windows.Forms.ComboBox fromComboBox;
private System.Windows.Forms.Label Label2;
private System.Windows.Forms.Label valueLabel;
private System.Windows.Forms.ComboBox toComboBox;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toComboBox = new System.Windows.Forms.ComboBox();
this.valueLabel = new System.Windows.Forms.Label();
this.Label2 = new System.Windows.Forms.Label();
this.fromComboBox = new System.Windows.Forms.ComboBox();
this.valueTextBox = new System.Windows.Forms.TextBox();
this.Label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// toComboBox
//
this.toComboBox.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList;
this.toComboBox.Location = new System.Drawing.Point(328, 16);
this.toComboBox.Name = "toComboBox";
this.toComboBox.Size = new System.Drawing.Size(80, 21);
this.toComboBox.TabIndex = 6;
this.toComboBox.SelectedIndexChanged += new
System.EventHandler(ComboBox_SelectedIndexChanged) ;
//
// valueLabel
//
this.valueLabel.TextAlign =
System.Drawing.ContentAlignment.TopCenter;
this.valueLabel.Location = new System.Drawing.Point(248, 16);
this.valueLabel.Name = "valueLabel";
this.valueLabel.Size = new System.Drawing.Size(72, 23);
this.valueLabel.TabIndex = 5;
this.valueLabel.Text = "?";
//
// Label2
//
this.Label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.Label2.Location = new System.Drawing.Point(232, 16);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(16, 24);
this.Label2.TabIndex = 4;
this.Label2.Text = "=";
//
// fromComboBox
//
this.fromComboBox.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList;
this.fromComboBox.Location = new System.Drawing.Point(152, 16);
this.fromComboBox.Name = "fromComboBox";
this.fromComboBox.Size = new System.Drawing.Size(80, 21);
this.fromComboBox.TabIndex = 3;
this.fromComboBox.SelectedIndexChanged += new
System.EventHandler(ComboBox_SelectedIndexChanged) ;
//
// valueTextBox
//
this.valueTextBox.Text = "0";
this.valueTextBox.TextAlign =
System.Windows.Forms.HorizontalAlignment.Center;
this.valueTextBox.Location = new System.Drawing.Point(72, 16);
this.valueTextBox.Name = "valueTextBox";
this.valueTextBox.Size = new System.Drawing.Size(72, 20);
this.valueTextBox.TabIndex = 2;
this.valueTextBox.TextChanged += new
System.EventHandler(ComboBox_SelectedIndexChanged) ;
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(16, 16);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(56, 23);
this.Label1.TabIndex = 1;
this.Label1.Text = "Distance:";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(424, 54);
this.Controls.Add(this.toComboBox);
this.Controls.Add(this.valueLabel);
this.Controls.Add(this.Label2);
this.Controls.Add(this.fromComboBox);
this.Controls.Add(this.valueTextBox);
this.Controls.Add(this.Label1);
this.Text = "Distance Convert";
this.Load += new System.EventHandler(Form1_Load);
this.ResumeLayout();
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
foreach ( string s in Enum.GetNames(typeof(DistanceUnits)) )
{
fromComboBox.Items.Add(s);
toComboBox.Items.Add(s);
}
fromComboBox.SelectedIndex = 0;
toComboBox.SelectedIndex = 0;
}
private void ComboBox_SelectedIndexChanged(object sender,
System.EventArgs e)
{
try
{
Distance inD = new Distance(Convert.ToDouble(valueTextBox.Text),
(DistanceUnits)fromComboBox.SelectedIndex);
Distance outD =
inD.Convert((DistanceUnits)toComboBox.SelectedInde x);
valueLabel.Text = String.Format("{0:0.#####}", outD.Value);
}
catch
{
valueLabel.Text = "?";
}
}
}
}
Thank you in anticipation.