|
C# interfaces are not as great as COM interfaces. COM interfaces were supposed to act like a contract, and by the way do separate the interface from the actual implementation.
Using C# this is no longer true, as there is no more "header" files.
The only thing good about C# interfaces is that your class can derive from more than one interface, whereas your class cannot derive from more than one class (same Java limitation).
MS quote (http://www.microsoft.com/ddk) : As of September 30, 2002, the Microsoft® Windows® 2000 DDK, the Microsoft Windows 98 DDK, and the Microsoft Windows NT® 4.0 DDK will no longer be available for purchase or download on this site.
|
|
|
|
|
As C# is currently not having a const keyword for return values, I certainly would make an interface for structures that I want to pass as readonly.
Another thing to consider is whether the implementing structure needs to subclass something other than ICustomer, e.g; CPenquin: public ICustomer and CHuman: public ICustomer...
I also find code more readable when you pass interfaces with a limited number of members.
And last: using interfaces makes your code very flexible.
"After all it's just text at the end of the day. - Colin Davies
"For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus
|
|
|
|
|
You define an interface when you have two or more classes implementing common behavoir.
For example "Customer" and "Supplier" might both implement a IContact interface with methods for setting their address, name etc.
You only want to define an interface when there is more than one class that implements that interface, or if there might be in the future.
|
|
|
|
|
I have to agree with Stephane.
I always try look for places where interfaces will be beneficial, but rarely find such places. One example of the top of my head is for "late-binding". Other than that I find them only really useful in Collections. I'm still pretty new to C# (6 months) so maybe they will start making more sense later.
Cheers
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
It's only useful when saying "I have two or more very different objects that do the same thing in very different ways, but I want a seperate piece of code to perform that common action regardless of the way te different objects act during that action."
And if that makes no sense, read the other post I just put in this thread
It's not infinitely useful but then neither was COM Interfacing but it was forced on you. This is just a simplification on the grounds that some people might still want this kind of functionality.
In other words, don't look for a place to use it, just remember it's there if you ever stumble upon something complicated like this
Paul
|
|
|
|
|
The usefulness of interfaces is best described (IMO) in terms of ArrayList.Sort().
An ArrayList can hold an array of pretty much any kind of object. So how does it sort them? What is the easiest way to define a function that could be sorting strings, objects, numbers, and so on?
You can't use ToString() because 10.ToString() == "10", 5.ToString() == "5". Thus 10.ToString() < 5.ToString().
So ArrayList.Sort calls IComparible.CompareTo(object obj) on each object to sort them. Thus if you want to do an ArrayList.Sort then all the objects in the ArrayList must implement the IComparable interface or it will not be able to cast it.
eg.
private class SortableObject : IComparable
{
public int CompareTo(object Item)
{
}
} This way I can exactly define how I want one SortableObject to compare to another. ArrayList.Sort doesn't give a damn how I compare them, just that I define a method in the exact format public int CompareTo(object Item) . It is a contract between my object and ArrayList.Sort saying I'll do the comparison, you do the sort.
Paul
|
|
|
|
|
You're right . Interface names such like ICompare, IClonable and stuff like that are in fact useful when searching the MSDN doc for classes with particular features, and as such act as search filters.
In the end, you get faster to what you are looking for. I vote on that a 5.
MS quote (http://www.microsoft.com/ddk) : As of September 30, 2002, the Microsoft® Windows® 2000 DDK, the Microsoft Windows 98 DDK, and the Microsoft Windows NT® 4.0 DDK will no longer be available for purchase or download on this site.
|
|
|
|
|
|
Hi,
I have some questions about thread programming in C#:
1. Is there any number limitation for threads
2. When multiple threads are initiated (started), each returning an object array, is there any mechanism I can utilise to check all the threads been finished? What if any of them throws an exception?
Thanks!!
|
|
|
|
|
I am trying to set tags for individual Combobox items. In looking through the Combobox doc, I cannot find the equivalent of SetItemData/GetItemData. The tag property is set at the Combobox level, and not at the item level. Can someone point me to an example, or how to set the tags for invidual combobox items. I would like to store database record ids as tags in the combobox.
Gaulles
Gaulles
Gaulles Technologies, Inc.
http://www.gaulles.com
|
|
|
|
|
Hi
Do this:
public class ComboBoxItem
{
object tag = null;
string text = null;
public ComboBoxItem(string text)
{
this.text = text;
}
public object Tag {get {return tag;} set {tag = value;}}
public override string ToString(){return text;}
}
Now just add these objects to ComboBox.Items. ie
comboBox1.Items.Add( new ComboBoxItem("some text"));
You can set/get the tags via:
object tag = ((ComboBoxItem) comboBox1.Items[i]).Tag;
Hope this helps
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
I thought of doing something similar, but because this was already part of MFC (SetItemData/GetItemData), I was digging around to see if it is already part of the .NET framework ComboBox class. I cannot understand it when MS takes away features that were already existing, and used by several applications.
Thanks for the info. I will implement it.
|
|
|
|
|
Gaul wrote:
I thought of doing something similar, but because this was already part of MFC (SetItemData/GetItemData),
You will ussually add an object to ComboBox.Items and then just override the ToString() method in the objects's class. Alternatively, you can display any property (obvously the ToString() method gets called on the Property's reflected type) with ComboBox.ValueMember. NOTE: however there is a bug ,according to me anyways, when the ComboBox gets sorted and retrieving SelectedItem in relation to SelectedValue. Allways makes sure the object is the same.
Hope this helps
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
Gaul wrote:
I cannot understand it when MS takes away features that were already existing, and used by several applications.
On the contrary, the functionality is still there; just how you use it is changed.
Rather than following the MFC paradigm where you added strings then set related data separately; you just add the data and tell the combobox how to represent it (by setting the DisplayMember and ValueMember properties of the combobox, if DisplayMember isn't set it defaults to calling ToString() )
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
hi!
i try to devolop an add-in for outlook xp which uses the SelectionChange event. If you select a contact the eventhandler adds or removes a command bar control. so far it works fine. but if you click the new button the supposed action works fine, but afterwards no event works anymore. you would have to restart the application to make it work again. any ideas what might be wrong and how i can solve the problem. (i'm using the pi's from microsoft)
thanks in advance
benedikt
|
|
|
|
|
Hi
Maybe the event resets after one use???? Try initializing it again when dealing with the event.
Hope it works
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
thanks for the answer. i had the same idea but it didn't work.
benedikt
|
|
|
|
|
DOPE
Maybe theres another event firing??? I havent looked at it yet. I dont even trust Outlook so I dont have it installed.
Cheers
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
no event is fired any more.
i don't know what to do. nothing else than problems....
|
|
|
|
|
Hey!
I've got a problem with my custom collection! The collection is used as a type for a property in my control. When I put the control to the form in the Designer, and Click the (... ) button at that property, new window is shown. I can add and remove elements in collection. But when I click the Add button, the type of new element is System.Object , not the type of elements of my collection.
I want to do sth like ListView.ListViewItemCollection , which is editable through the designer!
Thanks!
Ñ There is only one MP Ð
|
|
|
|
|
Hi
I assume you are inheriting from ArrayList. You should inherite from CollectionBase and expose the protected List 's methods as it needs like:
public virtual void Add(Item value)
{
this.List.Add(value);
}
I did this in a few minutes and everything works 100%. IMPORTANT: Like all designer related stuff, the custom (really a strongly typed) collection as well as the objects inserted into the collection MUST have default constructors.
Cheers
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
Strange! Take a look at my code:
public class R
{
public R(){a=10;b=56;}
int a, b;
public int A{
get{return a;}
set{a = value;}}
public int B
{
get{return b;}
set{b = value;}
}
}
public class Col: CollectionBase
{
public Col(){}
public virtual void Add(R valueT)
{
this.List.Add(valueT);
}
}
But the designer still adds the System.Object item, not the R item. What's wrong?
Ñ There is only one MP Ð
|
|
|
|
|
Hi MP
Actually I realised today that you just need the indexer
CollectionEditor from MSDN:
Notes to Inheritors: This editor can edit collections that have an Item property. The editor can determine the type of the collection from the Item property, if it exists. If the collection does not have this property, or if you want to provide collections of more than one type, you can override certain protected members of this class to customize the editor to support other types of collections.
so just do this:
public MyItem this[int i]
{
get {return (MyItem) base.List[i];}
set {base.List[i] = value;}
}
Cheers
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
Thanks!
Now it's working!
Ñ There is only one MP Ð
|
|
|
|
|
I want to do something like this:
Type t1=Type.GetType("UserControl");
UserControl Control = (UserControl)Page.LoadControl("Components/UserControl.ascx");
control = (t1)Control;
while compiling,system say "could not find type or naming space t1".
How can I use my type to define a variable?
Thanks!
|
|
|
|