|
It means that you tell the compiler that instead of the reference to a specific class that you have, you want a reference to the same object but the type of the reference should be one of the interfaces that the class implements.
---
b { font-weight: normal; }
|
|
|
|
|
I know, but what is the need of doing this? Why should someone do this?
|
|
|
|
|
Hendrik Debedts wrote: I know
So that's why you asked?
Hendrik Debedts wrote: but what is the need of doing this? Why should someone do this?
To get a reference to an interface. If you for example use the Array.Sort method with an IComparer , you need a reference to an IComparer :
Array.Sort(SomeArrayOfMine, MyOwnComparer);
Here, the reference to the MyOwnComparer object is implicitly casted to IComparer .
---
b { font-weight: normal; }
|
|
|
|
|
Yes ok, but if you can cast an object to an interface, it means that the class of the object implements the interface and when the class implements the interface you don't have firstly cast the object to the interface
|
|
|
|
|
You wouldn't cast a class to an interface, it's unnecessary. But you may need to go the other way.
If you have two classes (A and B) that implement an interface (I), you may have a method that takes a parameter of type I
public void F ( I i )
then inside the method you may need to cast the parameter to it's actually type (however, this may be poor style)
<br />
{<br />
if ( i is A ) { (A) i = blah blah blah }<br />
else<br />
if ( i is B ) { (B) i = blah blah blah }<br />
else throw something perhaps<br />
}<br />
but when calling F you needn't cast your instance to I
<br />
A a = new A() ; <br />
F ( (I) a ) ;
F ( a ) ;
|
|
|
|
|
PIEBALDconsult wrote: You wouldn't cast a class to an interface, it's unnecessary
This is sometimes necessary. If you have a class that implements two interfaces, each with a method of the same name you will need to explictly cast to the appropriate interface in order to access the correct method.
public interface IList {
public void Add(object x);
}
public interface IDictionary {
public void Add(object x);
}
public class Test : IDictionary, IList {
}
Test t = new Test();
((IList)t).Add(x);
((IDictionary)t).Add(x); Without the explicit cast the compiler will not know which Add method you intended to call.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Ew.
I sit corrected. I should have put in more qualifications like "usually unnecessary".
But then you're probably getting into the reason C# only supports single inheritance.
|
|
|
|
|
PIEBALDconsult wrote: But then you're probably getting into the reason C# only supports single inheritance.
Yep. I was approaching this from a C# viewpoint, which does support single inheritance for the base class (you can inherit from as many interfaces as you want).
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Hendrik Debedts wrote: when the class implements the interface you don't have firstly cast the object to the interface
Yes, sometimes you have to.
You can't call a method using parameters of the wrong type. If a method expects an interface reference, you have to use an interface reference. You can't use the object reference, as it's not the correct type.
---
b { font-weight: normal; }
|
|
|
|
|
Oh yes you can, this code compiles without any problem
Here you pass an object of a class who implements the System.Collections.ICollection interface (Person) to a method that has a System.Collections.ICollection attribute (static void aMethod(System.Collections.ICollection i))
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Person oPerson = new Person("Hendrik Debedts");
aMethod(oPerson);
Console.ReadLine();
}
static void aMethod(System.Collections.ICollection i)
{
Console.WriteLine(i);
}
}
public class Person : System.Collections.ICollection
{
public string msNaam;
public Person(string sNaam)
{
this.msNaam = sNaam;
}
public override string ToString()
{
return this.msNaam;
}
public void CopyTo(System.Array array, int index)
{
}
public int Count
{
get
{
int anInteger = 0;
return anInteger;
}
}
public bool IsSynchronized
{
get
{
bool aBoolean = false;
return aBoolean;
}
}
public object SyncRoot
{
get
{
object anObject = new object();
return anObject;
}
}
public System.Collections.IEnumerator GetEnumerator()
{
System.Collections.ArrayList arl = new System.Collections.ArrayList();
return arl.GetEnumerator();
}
}
}
|
|
|
|
|
Hendrik Debedts wrote: Here you pass an object of a class who implements the System.Collections.ICollection interface (Person) to a method that has a System.Collections.ICollection attribute (static void aMethod(System.Collections.ICollection i))
...Thereby causing an implicit cast of the reference. Exactly as in the code I showed earlier in the thread[^].
Just because you don't explicitly write the code for the casting, doesn't mean that the code you write doesn't do a cast.
---
b { font-weight: normal; }
|
|
|
|
|
I would only use the term "cast" when it's explicit, not when it's implicit.
|
|
|
|
|
Then what would you call it?
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
I am student of final year engineering student. I selected “SMS Website” as my project topic. In this I have to send Bulk SMS through website and provide other kind of services related to SMS.
Please tell me which steps I should follow to make this kind of software.
Please send me some material or any kind of source code related to it.
I need your help.
Thank you!!!
Contact Me: ashish.0619@gmail.com
|
|
|
|
|
Someone is using this?
Why me, a asp.net developer, can to need use this in the practice?
(I'm not ranting against WWF, just with sincerity, learn why I must stop some time to learn a new paradigm, what I never needed until now)
Regards
For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.(John 3:16)
|
|
|
|
|
So don't use it. Really.
It really only gets good for things where you need to process items in a specific controlled sequence.
|
|
|
|
|
We have a CMS that allows the user to upload documents, which must be classified and approved before others can see them. We had to write a lot of "plumbing" code to do this relatively simple operation. In the next version of the CMS, this will be handled by the Workflow engine (along with other parts such as the whole content approval process from design through to deployment).
Seriously, you don't need WF. You can write your own code to do what it does, but why would you want to? It's free, and it does what it says - it handles the workflow for you.
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I try to terminate the application from other library of main library. The scene like this.
1st at the main library I reference the second library.
2nd at the Application.Run I attach form from second library (call formx).
3rd at formx I try terminate the main application call Application.Exit().
The phase third is not passed, so anyone have experience to terminate application from other assembly, I try to manage my project (each controls, classes, forms, etc. have it own libraries).
Thanks.
|
|
|
|
|
That's not proper form. A library should NEVER determine when an application should terminate. It should only send back exceptions or some other signal that the root of your application uses to determine IF it should terminate or not, then take proper action on its own.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hi All
Is there a simple way to solve subject in C#, Visual Studio 2003, .NET 1.1 (way wich were simpler then learning AOP and big Inversion of control frameworks like Spring.NET)
PS sorry my poor english
|
|
|
|
|
No, there is no way to do this using the tools that come with the .NET Framework or Visual Studio .NET. You'll need a third party tool to log every call your application makes, though I've never had the need for such a tool.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
|
Hello...
There is a requirement of burning data into a CD from my application which i developed in VC++.net
I tried to use Imapi.h related functionalities. but its not in my XP SDK.So i used another dll file(acclImapiWrapper.Dll) which i downloaded from the website...
http://www.vbaccelerator.com/home/NET/Code/Libraries/Writing_CDs/IMAPI/article.asp
Using this dll file i tried the following code...
try{
DiscMaster* DM ;
SimpleDiscRecorder* sRecorder ;
DiscRecorders* DRs;
` DiscRecorder * DR;
DM = new DiscMaster();
sRecorder = DM->SimpleDiscRecorder;
DRs = DM->DiscRecorders;
int i,cnt;
cnt = DRs->get_Count() ;
for (i=0;i<cnt;i++)
{
="" dr="DM-">DiscRecorders->get_Item(i);
}
DR->OpenExclusive();
DR = DM->DiscRecorders->get_Item(0);
Cursor::Current = System::Windows::Forms::Cursors::WaitCursor ;
if (sRecorder->HasRecordableDrive())
{
//sRecorder->Burn(this->Handle);
DM->RecordDisc(true,true);
}
MessageBox(0,"CD Burning completed successfully","CD Burning",0);
Cursor::Current = System::Windows::Forms::Cursors::Default ;
DR->CloseExclusive();
sRecorder->Dispose();
DR->Dispose();
DM->Dispose();
}catch(COMException* e)
{
e->Message;
}
The data is staging on CD but burning Cd is not successful..
Could any one help me out??
Regards
Salai
Salai
|
|
|
|
|
There's a component called XPBurn, if your app is XP only, that does the trick nicely.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
i am able to add .NET (with C#)in-built calendar control but there are no controls for year navigation within the calendar.Please suggest how i can add my own navigation button for year .
Only month navigation is supported in the calendar.So i need to click 24times to go back 2 years.
Please help...
sumit
|
|
|
|