|
Hi Adam,
that is the exception:
Cannot implicitly convert type 'System.Collections.Generic.List<Teste.App.Myclass> to 'System.Collections.Generic.IList<Teste.App.IMyclass>. An explicit conversion exists (are you missing a cast?)
Ubirajara Mendes
|
|
|
|
|
Ahh... I see the problem now.
Your problem is one related to covariance and contravariance[^]. Let me show you an example:
public interface IMyClass { }
public class MyClass : IMyClass { }
public class MyOtherClass : IMyClass { } In this example, you have an interface, and two classes that implement said interface. Now, you've created a generic list of your first class type:
List<MyClass> listMyClass = new List<MyClass>(); Let's say, for the sake of this argument you weren't getting that cast error, and you could cast to a generic list of your interface type:
List<IMyClass> listIMyClass = ( List<IMyClass> )listMyClass;
Now, while only one list exists (because your cast just changed the type of the reference, not the contents of the list) you have created a reference that would allow you to add other objects that implement IMyClass to that list. Think about this:
listMyClass.Add( new MyOtherObject() );
listIMyClass.Add( new MyOtherObject() );
The problem isn't that you're casting a List<> to an IList<> , it's that you're trying to cast from a list with one generic parameter type to another.
Adam Maras | Software Developer
Microsoft Certified Professional Developer
|
|
|
|
|
Hi Adam,
I undertand what you mean, but trying do this example I got the same error. The C# doesn't converting the concreted List to an interface list.
I solve the problem by another way. I'm using an Generic class to access data on data base, and when I need to create a List of some Interface I'm using the default (private word) to create using the default constructor of the object.
Thanks a lot for your help, I really appreceate it.
Ubirajara Mendes
|
|
|
|
|
how can i drag and drop images from one picturebox to another picturebox? or to drag and drop images from a picturebox to a form? i'm really having a problem with this part of my program.. thanks in advance guys.. i know you can help me on this..
|
|
|
|
|
|
Hello All ,
Kindly let me know the where we can use factory patern . Kindly explain the real time example of Factory Design Pattern.
Regards,
Phani
|
|
|
|
|
|
jpk420 wrote: Kindly let me know the where we can use factory patern . Kindly explain the real time example of Factory Design Pattern.
Its a huge topic and you will get better idea from some books rather than from a forum post. Consider reading this[^] book to learn design patterns.
In short, Factory pattern is used to encapsulate the object creation. It is widely used pattern. Quoting from Head first design pattern book,
The factory design pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.
|
|
|
|
|
Hello All ,
I would like to know when should we decide that we must interface ? why should we use them ?
Kindly give one of real time example where we have used them .
Regards,
Jaddu
|
|
|
|
|
jpk420 wrote: I would like to know when should we decide that we must interface ? why should we use them ?
Inheritance is a good choice when:
1.Your inheritance hierarchy represents an "is-a" relationship and not a "has-a" relationship.
2.You can reuse code from the base classes.
3.You need to apply the same class and methods to different data types.
4.The class hierarchy is reasonably shallow, and other developers are not likely to add many more levels.
5.You want to make global changes to derived classes by changing a base class.
Source : MSDN[^]
When to use Interface[^]
jpk420 wrote: Kindly give one of real time example where we have used them
How to: Create and Implement Interfaces[^]
Above are the best possible resource that I have provided to you. You should search Google once before asking this type of questions
|
|
|
|
|
An interfaces is a contract. It plays a vital role in extending the design of an application. A real world example of interfaces would be the iterator implementation on .NET. IEnumerable interface is used to implement iterators in .NET.
IEnumerable exposes a method to get an enumerator and the users can use this without knowing much about the implementation. Consider how ASP.NET controls like GridView, DataGrid works. It doesn't know what type of objects will be binded to it. All it expects is an enumerable type and this helps these controls to handle various data structures.
|
|
|
|
|
Hi,
an interface is an abstraction, it is a contract saying some methods/properties have to be or will be available, while hiding the exact nature of the object.
Example:
int sum1(IEnumerable<int> data) {...}
int sum2(int[] data) {...}
int sum3(List<int> data) {...}
sum1 is the most general method, it specifies its parameter as little as possible, without giving up on its purpose. So you can pass it a real List, or an array, or... whereas sum2() and sum3() are more specific, i.e. less generally applicable.
So you achieve a lesser degree of dependency of sum1() on its data types.
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.
|
|
|
|
|
How can I work query wizard and the query output in the order of datagridview
|
|
|
|
|
mohammad alnoed wrote: How can I work query wizard and the query output in the order of datagridview
Can you briefly explain what is you exact requirement. Its seems you are troubling with the same issue for quite a long time, because you are asking the same question multiple times [ Your All Posts[^] ]
Either you are not clear about what you are going to do or you are not explained us correctly.
Thanks !
|
|
|
|
|
Tozih I work for the application of the research work by the name if the login name in the first column shows the datagridview data in columns datagridview Alakhari in the withdrawal of the name which was introduced in advance in the first column
|
|
|
|
|
Unfortunately, this is no clearer.
If you have a friend or colleague, that has better English than you, please get them to help you formulate your question. Or write it in your native language and get Google Translate or BabelFish, or similar to translate it for you before posting.
That way someone may be able to help you with your problem.
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.”
|
|
|
|
|
Hi,
I have implemented a communication model where I have a listening socket, waiting for requests on a specific port.
Once it gets called, it generates a client that creates two sockets: one sending a request to my server and another one that represents the client itself (and displays the data).
Perhaps it is a bit clearer with code:
public void StartRelay()
{
try
{
ClientSocket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnClientReceive), ClientSocket);
DestinationSocket.BeginReceive(RemoteBuffer, 0, RemoteBuffer.Length, SocketFlags.None, new AsyncCallback(this.OnRemoteReceive), DestinationSocket);
}
catch
{
Dispose();
}
}
protected void OnClientReceive(IAsyncResult ar)
{
try
{
int Ret = ClientSocket.EndReceive(ar);
if (Ret <= 0)
{
Dispose();
return;
}
DestinationSocket.BeginSend(Buffer, 0, Ret, SocketFlags.None, new AsyncCallback(this.OnRemoteSent), DestinationSocket);
}
catch
{
Dispose();
}
}
protected void OnRemoteSent(IAsyncResult ar)
{
try
{
int Ret = DestinationSocket.EndSend(ar);
if (Ret > 0)
{
ClientSocket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnClientReceive), ClientSocket);
return;
}
}
catch { }
Dispose();
}
protected void OnRemoteReceive(IAsyncResult ar)
{
try
{
int Ret = DestinationSocket.EndReceive(ar);
if (Ret <= 0)
{
Dispose();
return;
}
ClientSocket.BeginSend(RemoteBuffer, 0, Ret, SocketFlags.None, new AsyncCallback(this.OnClientSent), ClientSocket);
}
catch
{
Dispose();
}
}
protected void OnClientSent(IAsyncResult ar)
{
try
{
int Ret = ClientSocket.EndSend(ar);
if (Ret > 0)
{
DestinationSocket.BeginReceive(RemoteBuffer, 0, RemoteBuffer.Length, SocketFlags.None, new AsyncCallback(this.OnRemoteReceive), DestinationSocket);
return;
}
}
catch { }
Dispose();
}
This is working fine. However, I would like to replace the destination socket by an asynchronous webrequest and set the RemoteBuffer on "ClientSocket.BeginSend", by the result of that request (in bytes). The reason why I want to do this, is because I have a server with authentication and the webrequest allows me to use credentials, while the socket communication (in destinationSocket) will fail...
I am a bit confuse on how to use the asyncronous webrequest with the asyncronous client socket...
I imagine I have to do something like this?
request = (HttpWebRequest)WebRequest.Create(myUri);
request.Credentials = CredentialCache.DefaultCredentials;
IAsyncResult r = (IAsyncResult)request.BeginGetResponse(
new AsyncCallback(RespCallback), request);
and "RespCallback" call the "ClientSocket.BeginSend"; however, for retrieving the buffer, I need to get the stream of the request, and if that is also asynchronous I also need a "begin..." and a callback... for the stream...
Plus, do I need to store this buffer in a variable anywhere? (I imagine if the buffer is smaller than the response, the function will get called multiple times!)
Does anybody has implemented or experienced something like this?
Basically, I have a client socket and I want to send to it assyncronously a buffer of data that is get asynchronously from a webrequest...
Any suggestions will be really welcome! Thanks in advance for your time,
cheers,
Jo
|
|
|
|
|
I am trying to delete a file that is being reported in use and I am not sure how to conquer the issue. Here is the relevant code, in my opinion.
string destdir = "c:\\moved\\";<br />
this.picturebox1.image = image.fromfile("c:\\test.jpg");
--code stuff--
<br />
this.picturebox1.image = null;<br />
this.picturebox1.invalidate();<br />
File.Move("c:\\test.jpg",destdir + "test.jpg");
There is a lot more to the code than that, obviously, but this is the crux of the issue, anyone have a suggestion on how I can properly close the file in order to move / delete it?
|
|
|
|
|
Image.FromFile() keeps the file open as long as the Image is alive.
You must Dispose of it to free the file! Setting the reference null does not achieve this, it may or may not result in the GC to collect the image the next time it runs, i.e. the next time a memory request cannot be satisfied without first performing a collection.
The alternative is to replace FromFile() by FromSream(); that one does not lock the file.
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.
modified on Tuesday, August 4, 2009 12:26 PM
|
|
|
|
|
Luc Pattyn wrote: The alternative is to replace FromFile() by FromSream(); that one does not lock the file.
I was going to suggest that. But I was not sure that if you disposed the stream, will picture box still shows it?
|
|
|
|
|
Image.FromFile() generates a real Image and locks the file so it can still get at the metadata available through GetPropertyItem().
Image.FromStream() generates a real Image and no longer needs the stream; you can do to the stream whatever you like, the image couldn't care less.
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.
|
|
|
|
|
hi,i need to use a telephone component for my application,such as answering machin or ...
some friend adviced me kdtele component ,but i dont know how to use them
if any body knows please help me
thanx
|
|
|
|
|
Try here[^]
only two letters away from being an asset
|
|
|
|
|
|
This is probably pretty simple, but I'm a bit unsure of how to approach this. I have a path to a particular bmp file in my C# code. I want to display this picture in my HTML page. How would I pass this path to the HTML code instead of hardcoding the path?
C#:
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\temp.bmp";
HTML (hard coded at the moment):
<img src="C:\\Users\\L****\\AppData\\Local\\Program\\temp.bmp" alt="Graph"/>
|
|
|
|