Reputation: 781
I'm assigned a task to use a DLL file in C#. I have created the DLL file (Prog1210.dll) and have added it as a reference into the Solution explorer in C#. The DLL file has a variable txtNumber1 which is trying to be accessed in this main class.
Just wondering why it recognizes ValidateTextbox in the DLL in this class form, but says it doesn't recognize Prog1210 in the using statement, and doesn't recognize 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();
}
}
}
}
My DLL File:
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 *************//
Upvotes: 0
Views: 1094
Reputation: 814
You need a namespace in the prog dll and you need to mark the class in your first piece of code as public for txtnumber1. And ensure you have txtnumber1 as the id of a textbox in your form
Upvotes: 0
Reputation: 564931
Just wondering why it recognizes ValidateTextbox in the DLL in this class form, but says it doesn't recognize Prog1210 in the using statement,
This is because your Prog1210.dll didn't use a namespace. If you had specified everything to be in the Prog1210 namespace, it would have worked as you expected.
If you wish to change this, change your DLL code to:
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
and doesn't recognize txtNumber1.
There is no txtNumber1
variable within Class1
. You can only validate a TextBox
variable which exists in the scope where you call the method.
Upvotes: 1
Reputation: 2408
I guess your DLL was built with C++ or with some other native language. You can't use these kinds of DLLs from a managed assembly/dll.
To work, it would need to be a .NET Assembly/DLL or managed C++/CLI DLL.
If you really can't change that DLL you could wrap it with a C++/CLI DLL. For more information: Using .NET class from native C++ using C++/CLI as a 'middleware'
Upvotes: 0