|
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?
|
|
|
|
|
|
Well - you should use POP3 for the incoming.
|
|
|
|
|
I have some questions on type casting and inherited classes. First two easy questions:
1. What is the difference between "type casting" and "boxing"? The latter is a term that's new to me and I get the impression that it might be different terms for the same concept? Am I wrong?
2. Given ClassB cb = new ClassB();
Is there a difference between:
ClassA ca = cb as ClassA;
and
ClassA ca = (ClassA)cb;
But now for the more important question. Let's say I have two classes, ClassA and ClassB which inherits ClassA . Typecasting from ClassB to ClassA is a rather straightforward affair:
ClassB cb = new ClassB();<br />
ClassA ca = (ClassA)cb;
Is there a way to typecast the other way round? In other words, I have an instance of ClassA and I want to use it and view it as if it is of ClassB . Of course there might be some member variables specific to ClassB which might not be initialised. I would imagine that the best would be if the typecast first calls the default constructor of ClassB so that those member variables are initialised and then changes the type from ClassA to ClassB or something like that.
Am I making sense at least? Thanks in advance.
|
|
|
|
|
Dewald wrote: Given ClassB cb = new ClassB();
Is there a difference between:
ClassA ca = cb as ClassA;
and
ClassA ca = (ClassA)cb;
The difference here is that the first statement (ClassA ca = cb as ClassA; ) will perform a cast using the <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.isinst.aspx">isinst</a>[<a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.isinst.aspx" target="_blank" title="New Window">^</a>] IL instruction. What this does for you is that it performs a "safe" cast in that if cb implements ClassA or is a derived class of ClassA it is cast to that type; otherwise it will return null . Also, if cb were null already, then a null is also returned.
The ClassA ca = (ClassA)cb; statement performs a cast using the <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.castclass.aspx">castclass</a>[<a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.castclass.aspx" target="_blank" title="New Window">^</a>] IL instruction. If cb implements ClassA or is a derived class of ClassA it is cast to that type; otherwise it will return an InvalidCastException . Also, if cb were null already, then a new object is returned set to a null reference is also returned.
Dewald wrote: Is there a way to typecast the other way round? In other words, I have an instance of ClassA and I want to use it and view it as if it is of ClassB.
Given your examples I don't believe this is possible. If ClassA were to derive from ClassB , you could "downcast" to your base class or "upcast" to a more derived class, but those will only work in certain circumstances and you may not always get the expected results. It sounds like what you are looking for is the concept of covariance and contravariance[^], which currently do not exist in C# in a form that would allow you to do this.
Dewald wrote: What is the difference between "type casting" and "boxing"? The latter is a term that's new to me and I get the impression that it might be different terms for the same concept? Am I wrong?
Boxing is Microsoft's term for encapsulating a value type inside a reference type (usually System.Object). Boxing an instance of a value type is performed in C# by typecasting it to a reference type. An instance of the reference type is created (on the stack), containing a copy of the original value. That implies a stack allocation, plus the garbage collector needs to keep track of the new reference type. Unboxing a value is performed by typecasting the value back to the original type, thereby retrieving the actual value from within the reference.
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
|
|
|
|
|
Answer 1 : Boxing is changing Value type to Reference Type. Generally all the premitive type data is Value type. Type Casting is on the other hand is changing the datatype.
Answer 2: well you can store an object from derived class to the base class type reference. Casting is needed when you need the reverse.
In case of assigning baseclass object to derived classes.. there might be a problem, as you will find some function from derived classes from intellesense menu but are not available to the actual object as .NET brings intellesense menu from the datatype. In the reverse case you can call the derived class functions which are not listed through intellesense.
Does it look clear.
Abhishek Sur
|
|
|
|
|
I have this problem when a form is going to show this form...
frm.ShowDialog(); //here ocured the error
help pls.
spaps
|
|
|
|
|
Without knowing what this form is doing, there's not a lot to go on. Take a look at the full stack trace to try and figure out exactly what's screwed up.
|
|
|
|
|
the form getting some data from db, but all the things in the constructor is going well no prob, only when it goes to the ShowDialog(). it get me this error..
in the CALL STACK is this:
......Main() Line 225 + 0x1a bytes
in the output:
A first chance exception of type 'System.AccessViolationException' occurred in System.Windows.Forms.dll
spaps
|
|
|
|
|
Did you check in frm (the form you want to show) Load event or in its Constructor?
|
|
|
|
|
I've been tasked with writing a windows application. On the asp.net side we can use repeaters to create user controls but I haven't found the equivalent for windows forms. I'm trying to create a group of comboboxes from an array of items. Do I need to make collection of them? And how do I position them on the form? All I see is the absolute X Y coordinates, but what if you have 10 or 20 of these items?
Thanks
|
|
|
|