0

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 *************//
4

3 に答える 3

1

なぜこのクラス形式の DLL で ValidateTextbox を認識するのに、using ステートメントで Prog1210 を認識しないと言うのか、

これは、Prog1210.dll が名前空間を使用していないためです。すべてが Prog1210 名前空間にあるように指定した場合は、期待どおりに機能します。

これを変更する場合は、DLL コードを次のように変更します。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms; // required to work with Textboxes

namespace Prog1210
{
    public static class ValidateTextbox
    {
        // .. your code
    }
} // Add closing brace for namespace

txtNumber1を認識しません。

txtNumber1 内に変数はありませんClass1TextBoxメソッドを呼び出すスコープに存在する変数のみを検証できます。

于 2013-09-20T00:08:38.437 に答える
0

あなたの DLL は C++ または他のネイティブ言語で構築されていると思います。これらの種類の DLL をマネージ アセンブリ/DLL から使用することはできません。

機能するには、.NET アセンブリ/DLL またはマネージ C++/CLI DLL である必要があります。

本当にその DLL を変更できない場合は、C++/CLI DLL でラップできます。詳細情報: 「ミドルウェア」として C++/CLI を使用して、ネイティブ C++ から .NET クラスを使用する

于 2013-09-20T00:03:19.533 に答える
0

prog dll に名前空間が必要であり、最初のコード部分でクラスを txtnumber1 の public としてマークする必要があります。フォームのテキストボックスのIDとしてtxtnumber1があることを確認してください

于 2013-09-20T00:07:19.327 に答える