C# で DLL ファイルを使用するタスクが割り当てられています。DLL ファイル (Prog1210.dll) を作成し、C# のソリューション エクスプローラーに参照として追加しました。DLL ファイルには、このメイン クラスでアクセスしようとしている変数 txtNumber1 があります。
なぜこのクラス形式の DLL で ValidateTextbox を認識し、using ステートメントで Prog1210 を認識せず、txtNumber1 を認識しないと言うのか不思議です。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Prog1210;
namespace StaticClass
{
class Class1
{
private void btnValidate_Click(object sender, EventArgs e)
{
// Use the ValidateTexbox class that has been added to this project
if (ValidateTextbox.IsPresent(txtNumber1) &&
ValidateTextbox.IsDouble(txtNumber1) &&
ValidateTextbox.IsWithinRange(txtNumber1, 1.0, 100.0))
{
MessageBox.Show("Textbox value is valid!", "Good Data");
}
else
{
// The ValidateTexbox methods assigns an error message to the Tag
// property of the textbox.
string display = (string)txtNumber1.Tag;
MessageBox.Show(display, "Bad Data");
txtNumber1.Focus();
}
}
}
}
私のDLLファイル:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms; // required to work with Textboxes
public static class ValidateTextbox
{
// A class of static methods that will validate data in a textbox.
// An error message is assigned to the Tag property of the textbox.
//******** Empty Textbox Check ****************//
public static bool IsPresent(TextBox textbox)
{
if (textbox.Text == "")
{
textbox.Tag = "A value is required...";
return false;
}
return true;
}
// ******* Valid Data Type Check ***********//
public static bool IsInt(TextBox textbox)
{
try
{
Convert.ToInt32(textbox.Text);
return true;
}
catch (Exception)
{
textbox.Tag = "The value must be an integer...";
return false;
}
}
public static bool IsDouble(TextBox textbox)
{
try
{
Convert.ToDouble(textbox.Text);
return true;
}
catch (Exception)
{
textbox.Tag = "The value must be a double...";
return false;
}
}
public static bool IsDecimal(TextBox textbox)
{
try
{
Convert.ToDecimal(textbox.Text);
return true;
}
catch (Exception)
{
textbox.Tag = "The value must be a decimal...";
return false;
}
}
//*********** Valid Range Check - Overloaded Methods *************//