Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2005

Tip: Looking for answers? Try searching our database.

Databinding Null (VB Nothing) values

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Jessee - 11 Feb 2005 09:55 GMT
I have a listbox who's dataSource is an array of Classes of the type
Discount. and this listbox is bound to a Customer's Discount property.

Customer c = CustomerFactory.GetInstance(2342);
Dicsount[] d = DiscountFactory.GetAll();
ListBox1.DataSource=d;
ListBox1.DisplayMember="Description";
Listbox1.DataBindings.Add("SelectedItem",c,"Discount");

This works beautifully.  However, for some customers there will be no
discount. In this case, I would need a "No Discount" option in my listbox
whose value is null so that the customer.Discount property is set to null.  
Can this be done somehow? Can you add an item to a listbox os combobox that
sets a databound property to null?

I know that I could a.s a Discount instance to the array whose description
is "No Discount." However, that's kind of sloppy.  There is a lot of logic in
my app that checks for things like "if(myCustomer.Discount==null)" and
checking for "if(myCustomer.Discount.Description=="No Discount")" is
effectively hard-coding database values into your app (baaaaad idea)

So, any thought? anyone?
carlmanaster - 14 Feb 2005 15:46 GMT
Hi, David,

I don't think the "No Discount" discount is sloppy at all.  It's known
as the "null object" pattern, and can simplify your code a great deal.

http://www.industriallogic.com/xp/refactoring/nullObject.html

Peace,
--Carl

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.