472,364 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 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 2122

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,...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.