Click here to Skip to main content
15,899,475 members
Articles / Programming Languages / C#

Generics Converter for Basic Types

Rate me:
Please Sign up or sign in to vote.
2.36/5 (8 votes)
17 Jun 2010CPOL2 min read 23.1K   102   6  
A generics method to convert an object to a basic type.
namespace ConvertExDemo
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (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.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.int32TextBox = new System.Windows.Forms.TextBox();
			this.boolTextBox = new System.Windows.Forms.TextBox();
			this.decimalTextBox = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.testButton = new System.Windows.Forms.Button();
			this.int32ResultLabel = new System.Windows.Forms.Label();
			this.boolResultLabel = new System.Windows.Forms.Label();
			this.decimalResultLabel = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(12, 9);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(34, 13);
			this.label1.TabIndex = 0;
			this.label1.Text = "Int32:";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(12, 35);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(49, 13);
			this.label2.TabIndex = 1;
			this.label2.Text = "Boolean:";
			// 
			// int32TextBox
			// 
			this.int32TextBox.Location = new System.Drawing.Point(73, 6);
			this.int32TextBox.Name = "int32TextBox";
			this.int32TextBox.Size = new System.Drawing.Size(100, 20);
			this.int32TextBox.TabIndex = 2;
			// 
			// boolTextBox
			// 
			this.boolTextBox.Location = new System.Drawing.Point(73, 32);
			this.boolTextBox.Name = "boolTextBox";
			this.boolTextBox.Size = new System.Drawing.Size(100, 20);
			this.boolTextBox.TabIndex = 3;
			// 
			// decimalTextBox
			// 
			this.decimalTextBox.Location = new System.Drawing.Point(73, 58);
			this.decimalTextBox.Name = "decimalTextBox";
			this.decimalTextBox.Size = new System.Drawing.Size(100, 20);
			this.decimalTextBox.TabIndex = 5;
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(12, 61);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(48, 13);
			this.label3.TabIndex = 4;
			this.label3.Text = "Decimal:";
			// 
			// testButton
			// 
			this.testButton.Location = new System.Drawing.Point(98, 84);
			this.testButton.Name = "testButton";
			this.testButton.Size = new System.Drawing.Size(75, 23);
			this.testButton.TabIndex = 6;
			this.testButton.Text = "Test";
			this.testButton.UseVisualStyleBackColor = true;
			this.testButton.Click += new System.EventHandler(this.testButton_Click);
			// 
			// int32ResultLabel
			// 
			this.int32ResultLabel.AutoSize = true;
			this.int32ResultLabel.Location = new System.Drawing.Point(179, 9);
			this.int32ResultLabel.Name = "int32ResultLabel";
			this.int32ResultLabel.Size = new System.Drawing.Size(71, 13);
			this.int32ResultLabel.TabIndex = 7;
			this.int32ResultLabel.Text = "<Int32 result>";
			// 
			// boolResultLabel
			// 
			this.boolResultLabel.AutoSize = true;
			this.boolResultLabel.Location = new System.Drawing.Point(179, 35);
			this.boolResultLabel.Name = "boolResultLabel";
			this.boolResultLabel.Size = new System.Drawing.Size(86, 13);
			this.boolResultLabel.TabIndex = 8;
			this.boolResultLabel.Text = "<Boolean result>";
			// 
			// decimalResultLabel
			// 
			this.decimalResultLabel.AutoSize = true;
			this.decimalResultLabel.Location = new System.Drawing.Point(179, 61);
			this.decimalResultLabel.Name = "decimalResultLabel";
			this.decimalResultLabel.Size = new System.Drawing.Size(85, 13);
			this.decimalResultLabel.TabIndex = 9;
			this.decimalResultLabel.Text = "<Decimal result>";
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(276, 116);
			this.Controls.Add(this.decimalResultLabel);
			this.Controls.Add(this.boolResultLabel);
			this.Controls.Add(this.int32ResultLabel);
			this.Controls.Add(this.testButton);
			this.Controls.Add(this.decimalTextBox);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.boolTextBox);
			this.Controls.Add(this.int32TextBox);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox int32TextBox;
		private System.Windows.Forms.TextBox boolTextBox;
		private System.Windows.Forms.TextBox decimalTextBox;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button testButton;
		private System.Windows.Forms.Label int32ResultLabel;
		private System.Windows.Forms.Label boolResultLabel;
		private System.Windows.Forms.Label decimalResultLabel;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions