IList and Interfaces

erics44

Member
Joined
Dec 9, 2009
Messages
12
Programming Experience
Beginner
Hi

I have created an interface


public interface Ipdf
{
String pdf {get ; set; }
}


and I hope to store a list of names of reports in there


The names are added in an method in the main form


IList<Ipdf> pdfs = new List<Ipdf>();
foreach (FileInfo fi in rgFiles)
{
//code to add the fi.FullName to the IList
}


and i dont know what the "//code to add the fi.FullName to the IList" should be


thanks in advance
 
First up, to follow the .NET Framework convention, your interface should be named 'IPdf' rather than 'Ipdf'.

As for the question, you need at least one type that implements that interface. You can't create instances of an interface directly; you can only create instances of types that implement that interface. As such, your List<Ipdf> must contains instances of types that implement the Ipdf interface. As an example, consider the IComparable interface:
VB.NET:
IList<IComparable> comparables = new List<IComparable>();

comparables.Add(100);
comparables.Add("Hello World");
comparables.Add(true);
We can add an int, a string and a bool to our List because they all implement the IComparable interface. You need to add objects to your List whose types implement the Ipdf interface.
 
May I also remind you this is a VB.Net forum. Just as with other application types ASP.Net projects can be written in different languages, but as this site is dedicated to VB.Net development that is also the language used in ASP.Net section of these forums. We can all use online converters to convert code between different languages when or if we need to, but when posting here the preference would be to use VB.Net code.
 
May I also remind you this is a VB.Net forum. Just as with other application types ASP.Net projects can be written in different languages, but as this site is dedicated to VB.Net development that is also the language used in ASP.Net section of these forums. We can all use online converters to convert code between different languages when or if we need to, but when posting here the preference would be to use VB.Net code.
Hmmm... quite so. I'm so used to posting C# code elsewhere that I didn't even notice.
 
sorry I started the c# thing

using your code jmcilhinney

I am getting the error

cannot convert from 'string' to 'Print_Reports.IPdf'
 
cannot convert from 'string' to 'Print_Reports.IPdf'
Um, I didn't give you any code that you could use. The code I provided was intended as an example of a similar situation but you were certainly not supposed to use it in any way shape or form. I think you need to read my previous post a bit more carefully. I said more than once that you need a type that implements the Ipdf interface. Does String implement the Ipdf interface? No, it doesn't, so you obviously can't add it to your List.

I don't really see the point of that interface anyway. Why not just use a List<string> and store strings in it. That's what you're obviously trying to do anyway, judging from that error message.
 
yep i read your post and it was very good thanks

the thing is im not sure how to do things you are suggesting, do you have any examples?
 
If you don't know how to implement an interface then I would question why you're using interfaces in the first place. Is this just a learning exercise so that you can learn how to use interfaces? If so then that's the sort of information you should be providing us with. If we can't see a reason for you to do something a particular way then we'll advise you not to do it that way. If you're doing it that way specifically to learn how to do it that way then we won't waste our time trying to get you to do it another way.

So, why have you created that Ipdf interface in the first place? What are you hoping to achieve by using it?
 
If you don't know how to implement an interface then I would question why you're using interfaces in the first place. Is this just a learning exercise so that you can learn how to use interfaces? If so then that's the sort of information you should be providing us with. If we can't see a reason for you to do something a particular way then we'll advise you not to do it that way. If you're doing it that way specifically to learn how to do it that way then we won't waste our time trying to get you to do it another way.

So, why have you created that Ipdf interface in the first place? What are you hoping to achieve by using it?

thanks for your posts

ive sussed the problem

i didnt have any concrete instance of the interface :)
 

Latest posts

Back
Top