c# - Error: SelectedItem is a Property but is used like a Method

jcrcarmo

Active member
Joined
Dec 14, 2005
Messages
32
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 :)
 
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 []).
C#:
 private void mcMultiColumnComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
System.Data.DataRowView myDataRowView = mcMultiColumnComboBox1.SelectedItem;
this.cultivarTextBox.Text = myDataRowView[2].ToString();
}
 
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, isnt it?

Thanks,

JC :)
 
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
 
Marble Eater,

I dont mean to be a drag, but how exactly would you deal with this issue? Im 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 :)
 
Last edited by a moderator:
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 :)
 
jcrcarmo said:
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
 
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 :)
 
Im glad you fixed the problem but I just thought Id mention that by the sound of your problem a try catch statement is not the ideal method of solving it.
 
Hi Cags,

Whats the best way of dealing with this kind of issue? Ive searched the MSDN Help but couldnt find any pertinent leads to solving it. Thanks a lot!

Happy Holidays,

JC :)
 
Last edited by a moderator:
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.
:)
 
Back
Top