|
Expert Coming wrote: If you need help fixing your car you don't expect me to give you the car to fix do you?
PERFECT.............and sounds more feasible too...........
|
|
|
|
|
If only people would do that. That would be nice.
Come to think of it, I need help fixing my Jet, and Lambo.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
|
|
|
|
|
|
I have created a class called Book:
public class Book
{
public int iBookId = 0;
public String sAuthor = "";
}
Next I load instances of the Book class into an ArrayList. I then have an overloaded method to search for a book by Name or Id.
private static Book _FindBook(int iBookIdToFind, String sNameToFind)
{
Book bookReturn = null;
Book bookArrayBook = null;
int iTotalBooks = books.Count;
for (int iBook = 0; iBook < iTotalBooks; iBook++)
{
bookArrayBook = books[iBook];
if (iBookIdToFind != 0)
{
if (bookArrayBook.iBookId == iBookIdToFind)
{
bookReturn = bookArrayBook;
break;
}
}
else
if (bookArrayBook.sAuthor.ToLower() == sNameToFind.ToLower())
{
bookReturn = bookArrayBook;
break;
}
}
return bookReturn;
}
On this line
bookArrayBook = books[iBook];
I get the error "Cannot implicitly convert type 'object' to 'Book'".
I don't understand this. The object in the collection is already a book.
Anyone?
Everything makes sense in someone's mind
|
|
|
|
|
where do you define your books array? It seems that it's a collection of object rather than an array of book but you haven't posted that part of your code.
Edit: instead of using an ArrayList why not use:
System.Collections.Generic.List<book> books = new System.Collections.Generic.List<book>(); ?
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
I'm using a class-level ArrayList, so at the top I have:
private static ArrayList books;
Then in another method I load the collection using:
Book Book1 = new Book();
Book1.iBookId = 15;
Book1.sAuthor = "George Washington";
books.Add(Book1);
But my original question is still unanswered. The collection is holding references to a Book class, so why is the compiler complaining?
Everything makes sense in someone's mind
|
|
|
|
|
You should not have one method do two things. Split FindBook into two methods, FindBookByID, and FindBookByName.
Also, since you are not returning more than one result(i.e. only returning one book) don't store it and then return, just return as soon as you find a result. This way you don't loop through the rest of the books for no reason.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
|
|
|
|
|
The method onlny DOES do one thing. It searches the collection for a book.
It's private and only called by one of these 2 methods:
public static Book FindBook(int iBookId)
{
return _FindBook(iBookId, "");
}
public static Book FindBook(String sAuthor)
{
return _FindBook(0, sAuthor);
}
Also, notice that in my original positing, once the book is found, it does a Break and drops out the to bottom, where the book is returned. This is in keeping with sound programming which calls for only 1 return from a method.
Everything makes sense in someone's mind
|
|
|
|
|
Right, but it would be better to have a method only do one thing, search by ID, or search by Author name.
By storing the value, breaking and then returning, you are just wasting CPU cycles, and memory. Granted it is a rediculously small amount of both, you code isn't more programmer friendly, or easier to read, so where is the justification.
Just suggestions.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
|
|
|
|
|
The justification is easy.
Why would you copy & paste a block of code multiple times?
What if the property names on the class changed? Then you have to go through multiple block of code and change it all over.
And you CPU cycles statement doesn't make sense. Splitting the code into 2 methods isn't going to change that.
In some situations, like searching a treeview for a node , you can use either an index or a key value to search for a node.
The exposed method is probably overloaded to accept either datd type, just as my code is doing, then passing the necessary paramater value to a working method.
Everything makes sense in someone's mind
|
|
|
|
|
What if I were to want to extend you function to search on more things?
public Book FindBook(string Name, int ID, string Author, int NumPages, int YearPublished, string Publisher)
Now, you check to see which one I passed and search based on that? No, that is horrible coding practice. If your design requires that you do something like that, redesign.
I am just trying to keep you from coding something that will end up on the Coding Horrors forum.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
|
|
|
|
|
Truth is, this isn't production code. If it were, and I needed multiple search parameters, I would create a search class with properties for all searchable data elements and a Search method which would return the found item.
Everything makes sense in someone's mind
|
|
|
|
|
Well, most people just prefer to do it the right way, always. I am one of those people. Except I am bad about programming fuctionality now, coding practice later quite often. But when I do, I still am pretty close.
Also, I would like to point out that if you read what the others are saying lower on this question, they are also saying you should split it up. But again just a suggestion from 3 programmers now.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
|
|
|
|
|
KMAROIS wrote: I get the error "Cannot implicitly convert type 'object' to 'Book'".
I don't understand this. The object in the collection is already a book.
Yes, the actual type of the object is Book, but the type of the reference is Object. You can't assign an Object reference to a Book reference variable without casting it.
Unless you are stuck with framework 1.x, you should not use ArrayList at all. Use a strongly typed generic list instead, i.e. List<Book>. Such a list contains Book referenced instead of Object references, so you don't have to cast them every time you use them.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Using an ArrayList means that you are using a weakly typed collection so that each element in the collection is returned as an Object, not the actual type. If you have access to the generic collections you really should use one of those, probably List<Book> . This also allows you to more easily use some of the Linq extensions (if you are using .NET Fx 3.5) which would mean your find method (and this really should be split up in to separate functions, one for searching by Id and one by name).
Your code would end up looking something like this:
List<Book> books = new List<Book>();
private static Book Find(int bookId)
{
return books.FirstOrDefault(b => b.iBookId == bookId)
}
private static Book Find(string authorName)
{
return books.FirstOrDefault(b => String.Compare(b.sAuthor, authorName) == 0);
} The FirstOrDefault assumes that the list will only contain one entry or that you are interested in only the first entry, which mirrors the code you have in your example.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
Ok. I see. Problem is, I'm stuck with VS 2003 for now, so I'm trying to get it working for the timebeing.
Everything makes sense in someone's mind
|
|
|
|
|
KMAROIS wrote: Problem is, I'm stuck with VS 2003 for now
That's too bad. In that case, you should still define two separate overloads for your Find method (one that takes an int and one that takes a string). You can simplify your find code as well:
private static Book Find(int bookId)
{
Book foundBook = null;
foreach(Book book in books)
{
if (book.iBookId == bookId)
{
foundBook = book;
break;
}
}
return foundBook;
}
private static Book Find(string authorName)
{
Book foundBook = null;
foreach(Book book in books)
{
if (String.CompareOrdinal(book.sAuthor, authorName) == 0)
{
foundBook = book;
break;
}
}
return foundBook;
} If you don't want to use the foreach , you would need to change this line
bookArrayBook = books[iBook]; to
bookArrayBook = (Book)books[iBook];
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
do you have try
bookArrayBook = (Book)books[iBook];
dhaim
programming is a hobby that make some money as side effect
|
|
|
|
|
Hi guys, I'm developing a software here which receives RS232 signals from a specific hardware through the serial port and then processes them. Problem now I'm having is porting this application and the hardware to more newel models of pcs which carries no support for serial ports. One of the methods we opted for was to use a serial to usb converted to send the signals.
Question is, is that possible? Do I need to make changes to my software? Is there a 3rd party support for this? The language I'm developing this software on is C#. Thanks for any suggestions.
|
|
|
|
|
Using the USB adapter windows and your app should just see an RS232 com port that will work like any other. I say 'should' because many of the usb adapters don't carry anything other than the main data lines, leaving the secondary signaling nonfunctional.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
We use those devices. The problems we've seen are limitations on speed, the devices using the wrong serial port and settings on the device causing problems--usually it's the cache being too big or latency being too long. Other than that, you just treat them as a serial port.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
can anyone guide and list resources on how to build my own email software similar to Microsoft Outlook to read incoming emails and send as well.
Thanks
|
|
|
|
|
The better question is Why?
Everything makes sense in someone's mind
|
|
|
|
|
Well - mail classes, such as the Smtp classes, would be a start.
|
|
|
|
|
well, answering (Why) question : is to compete with Microsoft -))((--
now, smtp is to send outgoing mails, what about isting incoming mails?
|
|
|
|