|
I'm assuming like Christian that your question is about using interfaces in your own classes. Interfaces do not offer any significant performance benefit. However, they are very useful in situations where classes do inherit from the same base type. Here's a quick example of using interfaces:
public class SecretService : ISecurityCleared
....
public class CIA : ISecurityCleared
...
public class President : ISecurityCleared
...
//ISecurityCleared interface...
public interface ISecurityCleared
{
void ViewTopSecretMessage(string message);
}
//sample code...
private void TopSecretMessage(string topsecretmessage)
{
List<ISecurityCleared> securityCleared = new List<ISecurityCleared>();
securityCleared.Add(new SecretService());
securityCleared.Add(new CIA());
securityCleared.Add(new President());
for (int i = 0; i < securityCleared.Count; i++)
securityCleared.ViewTopSecretMessage(topsecretmessage);
}
Again, no performance increase, just cleaner code without having to worry about inheritance issues.
Cheers,
Richard
There cannot be a crisis today; my schedule is already full.
|
|
|
|
|
Richard Blythe wrote: securityCleared.Add(new President());
I wish...
|
|
|
|
|
No comment
There cannot be a crisis today; my schedule is already full.
|
|
|
|
|
Another advantage of interfaces (again assuming you are talking about code constructs and not the UI) is that they provide a well defined contract to program against. In other words, if you know that a certain type supports a certain interface, then you know that it implements the methods on that interface. An example:
public interface IMyInterface
{
void DoSomething();
}
public class MyInterfaceClass : IMyInterface
{
public void DoSomething()
{
Console.WriteLine("IMyInterface called");
}
} Then, you could do something like the following:
public class MyGenericClass<T> where T : IMyInterface, new()
{
public void CallAMethod()
{
T value = new T();
value.DoSomething();
}
} The generic class is constrained to using classes that support the IMyInterface interface, which means you could write code like this:
new MyGenericClass<MyInterfaceClass>().CallAMethod();
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Another benefit of interfaces is that they help with code reuse.
If a method parameter is an interface type, you can call it by passing an object of ANY class that implements that interface.
|
|
|
|
|
thanks all for your help!
|
|
|
|
|
look at this situation(this is just an example for clarification):
i have 2 threads:
thread1: is my main thread,( the static Program class) and should for example Run a form.
thread2: is my sec thread and should show a Splash screen that should be displayed according to my main thread.
thread1(main) wants to update a progressbar on thread2. i am a little confused...
my solution:
i guess that i could solve this by implementing events, rising them on thread1 and Handling them on thread2 and do the requested job accordingly(i.e. updating a progress).
but sth new arises and that was Cross-Thread-Oper... , and there is no Invoke Metod for my this obj (that is Static Program class, don't call me stupid) to prevent this exception(cross-thread-...excption).
|
|
|
|
|
Have you tried progressbar.Invoke ?
|
|
|
|
|
Thread1 should call Invoke/BeginInvoke on the progressBar in Thread2.
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) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
I know the Invoke procedure, but we can use Invoke when we have a Form!(Form.Invoke())
my threads are implemented in Program class where i Run my application(and there is no this.Invoke method!):
look here: my Form1 obj(f) during its lifetime will raise some events! i want to handle those events from thread2 and do the jobs in thread2!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Server
{
static class Program
{
static System.Threading.Thread thread2 = null;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Form1 f = new Form1();
thread2 = new System.Threading.Thread(new System.Threading.ThreadStart(worker));
thread2.IsBackground = true;
thread2.ApartmentState = System.Threading.ApartmentState.STA;
thread2.Start();
try
{
f.job1 += new EventHandler(Job1);
f.job2 += new EventHandler(Job2;
f.job3 += new EventHandler(Job3);
Application.Run(f);
}
catch(Exception r)
{
}
}
static void worker()
{
}
}
|
|
|
|
|
Someone has asked a similar question to yours, here[^], perhaps you can get some ideas from there.
There are also several articles here on CodeProject. Use the Search articles box, at the top of the page, and search for threaded splash screen, or similar.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Does the worker() method create a Form? What do the event handlers for Job1, Job2 and Job3 do - update the form created inside worker?
If you're getting a CrossThread exception, it means you're updating *some* UI control from the wrong thread - it doesn't necessary have to be the main Form.
|
|
|
|
|
In general speaking, i want to handle events that raise by thread1 and process them with thread2, that's all!
|
|
|
|
|
My first thought is a synchronized queue of delegates (which represent fired events) but that would be painful and probably overkill.. It's just a thought, not advice.
|
|
|
|
|
Hi.
My solution (working in VS 2008) has a C# console app, and a C# web service. The service has an entity data model. It has numerous stored procedures. For one of the stored procs, which just returns data (SELECT blah blah FROM...), I added a function import called "Iservice." I saved all, then in my console app, updated the service reference to my C# service. Now, in my console app, I am trying to do the following:
myEnts db = new myEnts(new Uri("http://localhost:3811/isr.svc"));
foreach(st_service sr in db.Iservice(false))
Console.WriteLine("Number#: {0} " + sr.num);
This doesn't work. "Iservice" is the name of the function import I created and I can see it in the object browser, but when I look at the service reference, I can't see it in the object browser. The compiler doesn't like "db.Iservice(false)" and the message is that myEnts from the service reference does not containt a definition for "Iservice."
So when I am in the service project, and look at the object browser, I can see Iservice - however when I look at the service reference in the object browser, from the console project, I do not see Iservice. How come this happens? How can I fix this? I tried saving, closing, and reloading MSVS2008, I tried updating the service reference multiple times. I googled for examples of calling stored procs in the EDM, and tried different ways of calling it as shown here:
http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/13/mapping-stored-procedure-results-to-a-custom-entity-in-entity-framework.aspx[^]
http://blogs.msdn.com/bags/archive/2009/03/13/entity-framework-modeling-select-stored-procedures.aspx[^]
http://blogs.msdn.com/adonet/archive/2007/09/14/how-to-map-stored-procedures-using-the-ado-net-entity-framework.aspx[^]
I'd appreciate any help.
PS -- The stored proc that I created a function import for takes in a BIT as a parameter. When in my service project, and looking at the object browser, it shows Iservice as "Iservice(bool?)" which is strange to me. Why the question mark?
|
|
|
|
|
dfn wrote: "Iservice(bool?)" which is strange to me. Why the question mark?
I think that it is treating it as nullable.
Apart from that, I am lost.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Henry Minute wrote: I am lost
Have a break. You probably have just exhausted your brain cell with a though FLP problem.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
lol. My brain could use a break from it too you know, or a hint.
|
|
|
|
|
dfn wrote: Iservice(bool?)
? is appended to value types to make them nullable; so bool? is either true, false, or null.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Thanks for the help. At least that ? isn't a mystery now. Nothing glaringly wrong with what I'm doing though? I still can't figure out why I can't call the function import I made for that stored proc...
|
|
|
|
|
I'm dynamically adding custom user controls to another user control which contains various flowlayoutpanels, and my problem is anchoring. I have to resize the flowlayoutpanels to fit the maximum width of the dynamic controls being added, and I edited all those controls to have proper anchoring so they resize while maintaining the UI look and feel desired.
This works great individually for each user control in design mode, but not when they are added to the user control which will contain them all.
All fun, but apparently anchoring to a flowlayoutpanel doesn't work (I have no idea why) and I was hoping someone has a workaround?
I'm already considering shifting to regular panels, but I'm reluctant to do this shift so quickly before inspecting other possible workarounds.
Docking doesn't work, the flowlayoutpanel can be anchored to the main User control and widens / shrinks as desired, but to reiterate the controls contained within the flowlayoutpanel, even with anchoring, do not have the desired behavior.
|
|
|
|
|
To quote from the MSDN Documentation for FlowLayoutPanel
Docking and anchoring behaviors of child controls differ from the behaviors in other container controls. Both docking and anchoring are relative to the largest control in the flow direction. For more information, see How to: Anchor and Dock Child Controls in a FlowLayoutPanel Control.
The link, from above[^]
That is all the help I can give, tried a FLP once and found docking etc. too much for my brain cell to cope with.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Gave you a 5 for the link, but I forgot to mention that I checked it out earlier with no avail to my dilemma.
Thanks though, perhaps a second read will help.
|
|
|
|
|
Hi all
if some one can plz send me program that play song and u can choose the song from a list
i wiil be glad i work whise Microsoft Visual Studio 2008
My e-mail: sagiklan@gmail.com
tnx sagiklan 
|
|
|
|
|
If you just want a light-weight method to play WAV files, then use the System.Media.SoundPlayer class. Otherwise, just embed a Windows Media Player control into your form
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|