|
In short an interface is a contract for an ability that you can implement in a class.
For example, a class can implement the IComparable interface, which means that you can compare one instance of the class to another. A method like List.Sort can then sort a list of instances, without the need to know anything at all about the class other than it implements the interface.
To make a method more flexible you can accept parameters of an interface type instead of a specific class. If for example your method needs to loop through some items, it can accept a parameter of the interface type IEnumerable instead of an array . Then your method can be used with any class that implements the interface, like an array , a List , a Queue , a Dictionary.KeyCollection , et.c.
You can also create interfaces to use between your own classes.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
An interface is an aspect. For example, all cars have a steering wheel (aspect of steering) but when the steering happens the implementation differs with every manufacturer or they might not, however, a user does not care what happens in the middle. The user only cares about the interface (steering) and the outcome (wheels to turn). Interfaces in programming serve the same purpose. Different classes can have their own implementation but the input and output will always be the same. Consider this:
public class Customer
{
Save()
{
ISaver saver = new DBSaver();
saver.Insert(this); // Here we are asking an implementation of Insert which inserts to the database.
}
}
internal class DBSaver : ISaver
{
public void Save(Customer c)
{
// Here the implementation is to save to DB
}
}
Now if your users decide they do not want to save to databases anymore but want to save to a flat file (not sure why they would do this but who cares), all you have to do is write another class which implements the interface, unit test it and then change only one line of code in Customer as below:
public class Customer
{
Save()
{
ISaver saver = new FlateFileSaver(); // Only this line is changed
saver.Insert(this); // Here we are asking an implementation of Insert which inserts to the flat file.
}
}
That is it and you are good to go. Your UI is still calling CustomerObject.Save() but something different is happening--polymorphism.
There are many other advantages to using Interfaces but this is the basic idea. Sometimes it also helps layout the foundation (blueprint foundation) for something. For example, I do not know how to make a good container class but if I implement the IList interface provided by .NET then I will have a good container will all the appropriate methods. Of course, you have to write good implementation because even if you write garbage inside the implementation that is what will get carried out.
|
|
|
|
|
I have an ArrayList which is used as the datasource of a comboBox:
ArrayList CorrespondenceSources = new ArrayList();
CorrespondenceSources.Add(new CorrAddress("D", "Debtor"));
CorrespondenceSources.Add(new CorrAddress("S", "Solicitor"));
CorrespondenceSources.Add(new CorrAddress("R", "Representitive"));
ComboBox1.DataSource = CorrespondenceSources;
How can I get the ArrayList index for the comboBox element with the value of "S" please?
|
|
|
|
|
Hi,
maybe this helps:
<br />
int iIndex = CorrespondenceSource.IndexOf(ComboBox1.SelectedValue);<br />
Does this help?
Regards
Sebastian
P.S.: Just a hint, did you take a look at List<t>, maybe this suits better than an ArrayList.
|
|
|
|
|
Actually a HashTable will be more ideal but you are right List is better than arraylist.
|
|
|
|
|
I want to create a child form within a mdi that behaves somewhat like a dialog form and I know that I can't do that and that it would defeat the purpose of an mdi all together but here is my dilemma. I need for the child form after a button is clicked on that child form for it to return to the parent form so I can pull values from it. Now if it was a dialogue I could do something like this;
if(form.ShowDialog == DialogResult.OK)
{
any information I want to pull from my dialog I can put here.
}
How can I accomplish this in this case?
|
|
|
|
|
It's a bit of a hack, but you could make an event in the child an fire it when it's about to close. You could even pass the info in the event parameters.. (or just pull them from the child form directly)
|
|
|
|
|
harold aptroot wrote: bit of a hack, but you could make an event in the child
Not a hack, this is exactly what events are for
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
That is a good solution and I prefer it. But if you are not familiar with events, a naive solution can also be for the child to call some properties of the parent form and provide the information before it is closed. For example:
class Parent
{
public void foo()
{
MyChildForm child = new MyChildForm();
child.Show();
}
public string SetThisMyChild
{
set
{
_comingFromChild = value;
}
}
private string _comingFromChild;
}
public class MyChildForm
{
public void CloseButton_Click(object sender, EventArgs e)
{
_mainForm.SetThisMyChild = something;
}
}
|
|
|
|
|
Hello everyone,
I met with timeout issue. Here is my whole code. I just send Http request to two local LAN servers and see if response could be received in given time, then I treat the server as healthy, or else I will treat them as unhealthy.
The two servers are simple ASP.Net ASMX Web Serviers Web Servers on IIS. The timeout will happen for both servers are PingServers method executing for 4-5 times successfully. It is strange timeout since the server are pretty fast -- I use IE to access the same URL, it takes less than 1 second to return, but in my code, I set timeout to be 4 seconds, it will prints timeout.
Does anyone have any ideas? Any bugs in my code?
static void PingServers(Object state)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create((String)state);
request.Timeout = 4 * 1000;
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
Console.WriteLine((String)state + " " + ex.ToString());
return;
}
Console.WriteLine((String)state + " is healthy");
return;
}
static Timer[] monitorTimers = new Timer[2];
static void Main(string[] args)
{
monitorTimers[0] = new Timer(PingServers, "http://localserver1/test/test.ashx", 0, 10 * 1000);
monitorTimers[1] = new Timer(PingServers, "http://localserver2/test/test.ashx", 0, 10 * 1000);
Console.ReadLine();
return;
}
thanks in advance,
George
|
|
|
|
|
How to translate the property of the control in the properties window?
For example,there is a Button on the winForm,then many properties is listed in the properties windows,such as Text、BackColor,now I want to translate the name of the property from english into other languange:
for instance:
English Chinese
Text 文本
BackColor 背面颜色
Thanks all in advance!
|
|
|
|
|
Donwload the .NET languagepack[^] of your choice and install it. It doesn't only translate error-messages, but also properties, if I'm not mistaken.
You cannot translate the texts by yourself, since there is no interface.
|
|
|
|
|
Thank Eddy for your answer.
I remember my colleague I have achieved this funtion in his program,not handling by the system languagepack,He can change the Text property of the button with any name.
|
|
|
|
|
The language-pack does it automatically, and the translations are embedded deep in the .NET runtime. It's a low-impact solution to your question, and I suggest you take a look at it before experimenting with sourcecode.
BTW, my Visual Studio IDE @ work uses these Dutch language-packs, and it's bloody annoying to search for something simple like "ForeColor". Not only is the sort-order different (Voorgrondkleur), but it's in a category with a different name. It gets worse when you're facing a localized version of an error - and you need to search the English-based MSDN for the same error.. It doesn't increase the productivity, it just causes more bugs.
Within the computer, everything should remain in a single language. That has nothing to do with the language that the end-user sees. It's just not possible to teach a computer all the localized versions of a boolean.
mctramp168 wrote: I remember my colleague I have achieved this funtion in his program,not handling by the system languagepack,He can change the Text property of the button with any name.
Phone your collegue, ask how he did it
You could also redefine the property-names, as shown in the example below;
[Browsable(false)]
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
}
[Browsable(true), Category("Appearance")]
public System.Drawing.Color VeurgrondKleur
{
get
{
return this.ForeColor;
}
set
{
this.ForeColor = value;
}
}
This will hide the "ForeColor" from the property-editor in Visual Studio. This example was built on a derivate of a textbox.
I wouldn't dub that a good programming practice. It makes things more complex, without actually adding much value. If you're facing a development-team that has a hard-time with English, then consider upgrading their knowledge of the language. The ROI would be higher when you train the Visual-Studio-user to communicate in English then trying to adapt the environment to the user.
Hope this helps,
I are troll
|
|
|
|
|
Thank Eddy for your zealous help.
I know your above handleing method,if each property is handled like that, It needs a lot of time, I saw a set software, it can let non-programmer user to design a winform after you teach them simply, the winform's property is translated by the programmer, then the non-programmer can very easy to learn to design simple winform. I only want to know this skill of translating the property of the control.
I try to ask my past colleague.
Thanks !
|
|
|
|
|
i need to detect windowstate outside form
ex. my application is running when i open a application (ex. internet explorer), i need to detect windowstate of internet explorer
how can i do it?
please give some tip or hint to me
thanks a lot...^^
|
|
|
|
|
Hi,
this is a two-step process requiring some P/Invoke to Win32 functions:
1. find the handle of the right window; depending on what you know about it, you can read
Process.MainWindowHandle; or by enumerating all windows, see EnumWindows[^]
2. now get information on that window, possibly by calling GetWindowPlacement[^]
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text output (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:14 PM
|
|
|
|
|
GetWindowPlacment <- how to use it?
i'm testing, but i don't understand about this function.
if i need to know application state, ex. minimize, maximize, normal
how can i do? i'll get information window to compare.
please give more tip or hint to me.
thanks so much...
|
|
|
|
|
Hi,
I gave you the link to the MSDN page on GetWindowPlacement; what you want is the value of showCmd.
Since this is a native function and your app is a .NET app, you will need some P/Invoke stuff; I hope you are familiar with that, if not be warned, it ain't that simple.
Here is a link to P/Invoke GetWindowPlacement[^].
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text output (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:14 PM
|
|
|
|
|
|
Hello everyone,
I met with a system hang issue (a program does not run to complete). It is a managed code program (C# console) and run on Windows Server 2003 x64. Are there any ideas or recommended documents about how to narrow down such hang problem?
thanks in advance,
George
|
|
|
|
|
What that application is doing? Is that entering into some infinite loop?
|
|
|
|
|
Thanks Navaneeth,
The application is dealing with sending data to a web services and retrieve results to analyze. Any general ideas to debug and analyze further for application hang problem?
regards,
George
|
|
|
|
|
George_George wrote: Any general ideas to debug and analyze further for application hang problem?
You can step into the code and check which line execution is hanging. Identify why it is hanging. If the method which is hanging provides a timeout mechanism, use that so that your code will timeout if it can't process within the limit and will not make the whole application hang.
|
|
|
|
|