jcrcarmo Posted December 21, 2005 Posted December 21, 2005 Hi everybody, Does anybody know what the correct synthax for the following C# 2005 Espress code is? I keep getting an error message saying that "SelectedItem is a Property but is used like a Method" private void mcMultiColumnComboBox1_SelectedIndexChanged(object sender, EventArgs e) { System.Data.DataRowView myDataRowView = mcMultiColumnComboBox1.SelectedItem(); this.cultivarTextBox.Text = myDataRowView[2].ToString(); } Thanks a lot, JC :) Quote
Leaders snarfblam Posted December 21, 2005 Leaders Posted December 21, 2005 The problem is the parentheses. Where VB syntax can treat a property like a method or a field, C# syntax only allows them to be used like a field. In other words, no parenthases (indexed properties use square brackets []). private void mcMultiColumnComboBox1_SelectedIndexChanged(object sender, EventArgs e) { System.Data.DataRowView myDataRowView = mcMultiColumnComboBox1.SelectedItem; this.cultivarTextBox.Text = myDataRowView[2].ToString(); } Quote [sIGPIC]e[/sIGPIC]
jcrcarmo Posted December 21, 2005 Author Posted December 21, 2005 Hi Marble Eater, I had already tried it without the parenthesis and I get the error message: "Cannot implicitly convert type 'object' to 'System.Data.DataRowView'. An explicit conversion exists (are you missing a cast?)?" Weird, isn't it? Thanks, JC :) Quote
TripleB Posted December 21, 2005 Posted December 21, 2005 Hi, It is not realy weird, it is an entire different error, it says that you are assigning something (your selecteditem) to something that is not compatible with that (your datarowview) It even suggests the answer (are you missing a cast). So your first problem is solved, but you are now facing another one. Greetz Quote
jcrcarmo Posted December 21, 2005 Author Posted December 21, 2005 (edited) Marble Eater, I don't mean to be a drag, but how exactly would you deal with this issue? I'm still learning... THis is what I just did it and worked, but when I stop the debugging process it gives an error message: private void mcMultiColumnComboBox1_SelectedIndexChanged(object sender, EventArgs e) { DataRowView myDataRowView = (DataRowView)mcMultiColumnComboBox1.SelectedItem; this.cultivarTextBox.Text = myDataRowView[2].ToString(); } Thanks, JC :) Edited December 21, 2005 by jcrcarmo Quote
jcrcarmo Posted December 21, 2005 Author Posted December 21, 2005 Hi TripleB, The error message appears when I close the form that cointains the above code and also when I stop debugging the app. The error message is: Object reference not set to an instance of an object. Use the "new" keyword to create an object instance. Check to determine if the object is null before calling the method. Any ideas? Thanks a lot, JC :) Quote
TripleB Posted December 21, 2005 Posted December 21, 2005 Hi TripleB, The error message appears when I close the form that cointains the above code and also when I stop debugging the app. The error message is: Object reference not set to an instance of an object. Use the "new" keyword to create an object instance. Check to determine if the object is null before calling the method. Any ideas? Thanks a lot, JC :) This means that you are using an object that is not instanciated, do you have some code in the close event of the form? You just have to find where the error is thrown. From there on if you give the code I could give you some pointers, But the bottom line is that you are trying to use something that has not been instanciated (ex Form frmExample = new Form() ) Greetz Quote
jcrcarmo Posted December 22, 2005 Author Posted December 22, 2005 Hi TripleB. I had already solved the problem with a Try/Catch statement but I do appreciate your time and help. Thanks a lot, JC :) Quote
Cags Posted December 22, 2005 Posted December 22, 2005 I'm glad you fixed the problem but I just thought I'd mention that by the sound of your problem a try catch statement is not the ideal method of solving it. Quote Anybody looking for a graduate programmer (Midlands, England)?
jcrcarmo Posted December 23, 2005 Author Posted December 23, 2005 (edited) Hi Cags, What's the best way of dealing with this kind of issue? I've searched the MSDN Help but couldn't find any pertinent leads to solving it. Thanks a lot! Happy Holidays, JC :) Edited December 23, 2005 by jcrcarmo Quote
Leaders Iceplug Posted December 23, 2005 Leaders Posted December 23, 2005 For starters, take extra consideration to making sure that all variables are instantiated before they are used... and that they are not closed when you may consider using them again. For isolated cases like this, it is important to first ask: Which object reference is still set to nothing? After you do this (possibly by setting breakpoints or using if (obj is null) to check, next ask yourself "Why would this object still be set to nothing?" It could be a property that returns nothing when nothing is selected... or it could mean that an argument you specified has no results and therefore returns nothing. :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
jcrcarmo Posted December 26, 2005 Author Posted December 26, 2005 Hi Iceplug, thanks for the tip! :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.