473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Explicit interface class implementation binding to DataGridViewComboBoxColumn.ValueMember

Ken
Hi

I have a little application that does datavalidation.
It supports dynamically loaded plugins (you drop a dll with a class
implementing IValidator<Tin the same dir as the main application).
All classes that implement this interface are shown in a drop down in
a DataGridViewComboBoxColumn named Validator.

By choosing the class to validate this specific data in the drop
downbox IValidator<T>.Validate is called and returns true/false. Which
in turn alerts the users. (The data can be any kind of audio data,
image data, text data ...)

DataGridViewComboBoxColumn validatorColumn =
dragDropDataGridView1.Columns["Validator"] as
DataGridViewComboBoxColumn;
if (validatorColumn != null)
{
validatorColumn.DisplayMember = "Name";
validatorColumn.ValueMember = "IValidator<string>.Name"; // this line
fails
}

It also fails like this
validatorColumn.ValueMember = "Name";

Error is cannot convert to string. I have a ToString() implemented in
the actual validator implementation.

Actual IValidator<Timplementation
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using TMData.Data.Validation;
using System.Text.RegularExpressions;

namespace Validators
{
// Dummy implementation to show concept
class EmailValidator : IValidator<string>
{
#region IValidator<stringMembers

string IValidator<string>.Name
{
get
{
return GetType().ToString();
}
}
string IValidator<string>.Error
{
get
{
return "The data does not have the correct format for
an email";
}
}

bool IValidator<string>.Validate(string data)
{
if (data == string.Empty) return true;

Regex re = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]
{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|
[0-9]{1,3})(\]?)$");
if (re.IsMatch(data))
return (true);
else
return (false);

}

// This does not work
string IValidator<string>.ToString()
{
return ((IValidator<string>)this).Name;
}

// This does not work
public string ToString()
{
return ((IValidator<string>)this).Name;
}

// This does not work
public overrride string ToString()
{
return ((IValidator<string>)this).Name;
}
#endregion
}
}

However if I make the interface implementation implicit it works (like
this)

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using TMData.Data.Validation;
using System.Text.RegularExpressions;

namespace Validators
{
class EmailValidator : IValidator<string>
{
#region IValidator<stringMembers

public string Name
{
get
{
return GetType().ToString();
}
}
public string Error
{
get
{
return "The data does not have the correct format for
an email";
}
}

public bool Validate(string data)
{
if (data == string.Empty) return true;

Regex re = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]
{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|
[0-9]{1,3})(\]?)$");
if (re.IsMatch(data))
return (true);
else
return (false);

}

public override string ToString()
{
return Name;
}
#endregion
}
}
Why?
I prefer to work with explicit interfaces to encourage separation like
this since it is not all developers that does interface based
programming you can force them by using explicit interface
implementation.

ISomeInterface obj = new SomeObject();

instead of

SomeObject obj = new SomeObject();

Maybe it has some thing to do with that I instantiate the objects like
this.

(In form load)

DirectoryInfo dirInfo = new
DirectoryInfo(Path.GetDirectoryName(Assembly.GetEn tryAssembly().Location));
Array.ForEach<FileInfo>(dirInfo.GetFiles("*.dll"),
delegate(FileInfo fileInfo)
{

Array.ForEach<Type>(Assembly.LoadFile(fileInfo.Ful lName).GetTypes(),
delegate(Type type)
{
if (type.IsClass &&
(type.GetInterface("IValidator`1") != null))
{

validators.Add((IValidator<string>)Activator.Creat eInstance(type));
}
}
);
}
);

I have tried using the generic Activator.CreateInstance<Tbut I could
not get it to work properly.

Regards
Ken

Mar 4 '07 #1
0 2191

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Steve W. | last post by:
I just read the section (and did the exercise) in the C# Step by Step book that covers Explict Interface Implementation (where you specify in the method implementation the specific interface that...
2
by: COLIN JACK | last post by:
Hi All, I've got a situation where I'm implementing an interface (BaseInterface in example below) and I want to use explicity interface implementation of an event so that I can add type safety. ...
7
by: Sam | last post by:
Hi, Here's my class's constructor: Public Sub New(ByVal frmParent As Form, ByVal curRow As Integer) MyBase.New() End Sub I have a member variable that I want to initialize with frmParent....
1
by: Shawn | last post by:
I am trying to use a DataGridView to display one column of textboxes with a single value from a query and then next to it have a combobox that displays results from another query. What happens is...
0
by: Roger Odermatt | last post by:
Hello I have binding a class to a DataGridView and this class have a property from another class and this i want binding to a DataGridViewComboBoxColumn. So when i change a item in the Combo and...
4
by: Mathieu Cartoixa | last post by:
Hi, I have been annoyed in one of my recent projects with a problem related to the explicit implementation of an interface on a value type. I will take an example to show the problem. Say we...
0
by: Brandon Driesen | last post by:
The following illustrates my question. Why is it when I bind to an a collection of items whose interface implementation is explicit, there is an error during the binding process wherein the error...
1
by: =?Utf-8?B?Sk0=?= | last post by:
In an application I have an interface with methods and properties. The interface is used on a Class (ie class MyClass : IMyClassA, IMyClassB). On a windows form I define a BindingSource...
2
by: puzzlecracker | last post by:
I don't see the purpose of explicit interface implementation other than to hide its signature in the class that implements it, and, instead, write your own, perhaps with a different signature,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.