locked
Use of unassigned local variable RRS feed

  • Question

  • I keep getting a message about a use of an unassigned local variable named 'rc_name'

    Here is the offender.

    string rc_name = rc_name.SelectedIndexChanged.Trim.Text();


    This doesn't work either.

    string rc_name = DropDownList.Items.FindByText("rc_name").Selected = true;

    I've been researching this for 1 hour and I'm more confused than I was before. 

    This works perfectly fine.

    string text1 = rc_type1.Trim();

    I'm working in C# & ASP.NET.

    These DropDownLists are linked to the SQL data source correctly.  There must be some special property for this object that I'm not aware of.  Can someone please point me in the right direction?

    Thanks!


    Check out my book!!!

    http://www.amazon.com/Automating-Business-Processes-Reducing-Increasing-ebook/dp/B01DJJKVZC/ref=sr_1_1?ie=UTF8&qid=1459738423&sr=8-1&keywords=ryan+shuell




    • Edited by ryguy72 Monday, April 4, 2016 3:45 PM
    Monday, April 4, 2016 12:16 AM

Answers

  • How about

    string rc_name = "";
    rc_name = rc_name.SelectedIndexChanged.Trim.Text();

    Just curious will that be fine.

    chanmm


    chanmm

    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 12:26 AM
  • The first statement is using "rc_name" on the right side, which really doesn't make sense because you are declaring "rc_name".

    The second statement is using "=" instead of "==" - and it's assigning this boolean result to a string - the problems just seem to get worse the more you look at this code.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter



    • Edited by Dave Doknjas Monday, April 4, 2016 3:05 AM
    • Proposed as answer by Christopher84 Monday, April 4, 2016 4:29 PM
    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 1:43 AM
  • stringrc_name =rc_name.SelectedIndexChanged.Trim.Text();

    How can that be a string variable named rc_named and  something about rc_name.SelectedIndexedChanged, like it's part of some dropdownlist?

    Somehow you were able to compile the solution? It seems to be confusing the compiler is what it is doing. 

    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 2:00 AM
  • Your first problem is that you are declaring a string with the same name as a DropDownList. You can fix this simply by giving the string a different name.

    // Call the string something besides "rc_name";
    string rcName = rc_name.SomethingOrOther(...);

    Your second problem is that you appear to be trying to assign a boolean to that string, which doesn't make sense. In fact, I can't work out what string you are trying to get, which is why I put SomethingOrOther in the code above instead of the real method call, because I don't know what the real method call should be.

    So the above code should work, provided you can replace SomethingOrOther with the call that you actually need.

    What string are you really trying to get?

    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 2:58 AM
  • I feel like I'm missing something simple, but I just don't know what it is.  Is there something else I can try?  I just want to learn a new trick, and basically get this working too!!

    stringrc_name = DropDownList.Items.FindByText("rc_name").Selected = true;

    At best, what would be returned is some dropdownlist ListItem object and not some string, according to the documentation.

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitemcollection.findbytext%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    You would have to use the return ListItem object's properties to extract the two property values out of the Listitem object, if the Listitem was found, otherwise, the return object's value is null.

    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 3:03 AM
  • This line...

    stringrc_name = DropDownList.Items.FindByText("rc_name").Selected = true;


    ...won't work because, as the complier has said, Items is not a static member of DropDownList. That is, you need an instance of a DropDownList to access it.

    string rcName = rc_name.Items.FindByText("rc_name").Selected = true; // Again, be sure to use different names for the string and the DropDownList.



    And I'm not sure if the above will work anyway, because the right hand side doesn't really make sense - assigning true to Selected and then trying to assign that boolean value to a string.
    • Edited by Ante Meridian Monday, April 4, 2016 3:18 AM Added the bit about not sure it will work anyway.
    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 3:15 AM
  • I wonder if you have a drop down list called rc_name and then you declared a string also called rc_name.

    For example, if you had a label called label1 and did this:

    string label1 = label1.Text; // ERROR CS1061


    The scope of label1 changes to be the string called label1, not the Label called label1, and you get an error saying: 'string' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

    So, you could fix this by referring to the label as this.label1

    string label1 = this.label1.Text; // OK

    So I wonder if in your case, you simply need the same kind of fix?

    There's so much else wrong with what you're doing unfortunately.

    • SelectedIndexChanged is an event, so unlikely what you wanted.
    • assigning a bool to a string is not cool.  the = true is an assignment and not a comparison.  Use == for a comparison.  Use .ToString to convert a bool to a string.

    What are you trying to accomplish?


    • Edited by Wyck Monday, April 4, 2016 3:41 AM
    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 3:40 AM
  • Personally since string is a class, I would have expected "reference not set to an instance of object" exception at runtime. Maybe a "string does not have a SelectedIndexChanged property or field" exception.

    Unless you got a a DropdownList named exactly the same, wich would cause one heck of a confusion for everyone.
    At wich point the compiter should tell you "SelectedIndexChanged" is a Event and those can only be part of a += or -= opeartion, not be read out.
    Unless this code somehow runs from inside the DropdownList (wich might happen if you you inherited from it).

    There are so many reasons this should not compile, the compiler can only tell you teh first one it runs into. You should stop using consecutive . for a while. Make "one field/property access or function call, before I store into a local variable" the rule. You get much more usefull "line of fault" detection out of it.
    The JIT compiler can still go and cut them out if it seems feasible, but the code will get a million times clearer and easier to debug.

    Since you are working with ASP.Net to modify a DB, understanding the page lifecycle becomes absolutley fundamental. You can do a lot of stuff fundamentally wrong by not knowing/understanding it:
    https://msdn.microsoft.com/en-us/library/ms178472.aspx


    • Edited by Christopher84 Monday, April 4, 2016 4:49 PM
    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 4:47 PM

All replies

  • How about

    string rc_name = "";
    rc_name = rc_name.SelectedIndexChanged.Trim.Text();

    Just curious will that be fine.

    chanmm


    chanmm

    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 12:26 AM
  • The first statement is using "rc_name" on the right side, which really doesn't make sense because you are declaring "rc_name".

    The second statement is using "=" instead of "==" - and it's assigning this boolean result to a string - the problems just seem to get worse the more you look at this code.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter



    • Edited by Dave Doknjas Monday, April 4, 2016 3:05 AM
    • Proposed as answer by Christopher84 Monday, April 4, 2016 4:29 PM
    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 1:43 AM
  • stringrc_name =rc_name.SelectedIndexChanged.Trim.Text();

    How can that be a string variable named rc_named and  something about rc_name.SelectedIndexedChanged, like it's part of some dropdownlist?

    Somehow you were able to compile the solution? It seems to be confusing the compiler is what it is doing. 

    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 2:00 AM
  • Sorry all, but none of these suggestions worked. 


    I feel like I'm missing something simple, but I just don't know what it is.  Is there something else I can try?  I just want to learn a new trick, and basically get this working too!!


    Check out my book!!!

    http://www.amazon.com/Automating-Business-Processes-Reducing-Increasing-ebook/dp/B01DJJKVZC/ref=sr_1_1?ie=UTF8&qid=1459738423&sr=8-1&keywords=ryan+shuell






    • Edited by ryguy72 Monday, April 4, 2016 3:45 PM
    Monday, April 4, 2016 2:34 AM
  • Your first problem is that you are declaring a string with the same name as a DropDownList. You can fix this simply by giving the string a different name.

    // Call the string something besides "rc_name";
    string rcName = rc_name.SomethingOrOther(...);

    Your second problem is that you appear to be trying to assign a boolean to that string, which doesn't make sense. In fact, I can't work out what string you are trying to get, which is why I put SomethingOrOther in the code above instead of the real method call, because I don't know what the real method call should be.

    So the above code should work, provided you can replace SomethingOrOther with the call that you actually need.

    What string are you really trying to get?

    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 2:58 AM
  • I feel like I'm missing something simple, but I just don't know what it is.  Is there something else I can try?  I just want to learn a new trick, and basically get this working too!!

    stringrc_name = DropDownList.Items.FindByText("rc_name").Selected = true;

    At best, what would be returned is some dropdownlist ListItem object and not some string, according to the documentation.

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitemcollection.findbytext%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    You would have to use the return ListItem object's properties to extract the two property values out of the Listitem object, if the Listitem was found, otherwise, the return object's value is null.

    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 3:03 AM
  • This line...

    stringrc_name = DropDownList.Items.FindByText("rc_name").Selected = true;


    ...won't work because, as the complier has said, Items is not a static member of DropDownList. That is, you need an instance of a DropDownList to access it.

    string rcName = rc_name.Items.FindByText("rc_name").Selected = true; // Again, be sure to use different names for the string and the DropDownList.



    And I'm not sure if the above will work anyway, because the right hand side doesn't really make sense - assigning true to Selected and then trying to assign that boolean value to a string.
    • Edited by Ante Meridian Monday, April 4, 2016 3:18 AM Added the bit about not sure it will work anyway.
    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 3:15 AM
  • I wonder if you have a drop down list called rc_name and then you declared a string also called rc_name.

    For example, if you had a label called label1 and did this:

    string label1 = label1.Text; // ERROR CS1061


    The scope of label1 changes to be the string called label1, not the Label called label1, and you get an error saying: 'string' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

    So, you could fix this by referring to the label as this.label1

    string label1 = this.label1.Text; // OK

    So I wonder if in your case, you simply need the same kind of fix?

    There's so much else wrong with what you're doing unfortunately.

    • SelectedIndexChanged is an event, so unlikely what you wanted.
    • assigning a bool to a string is not cool.  the = true is an assignment and not a comparison.  Use == for a comparison.  Use .ToString to convert a bool to a string.

    What are you trying to accomplish?


    • Edited by Wyck Monday, April 4, 2016 3:41 AM
    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 3:40 AM
  • Thanks for the help everyone.  I haven't used C# in a while, and I've never used it for this kind of thing, so I'm sure I'm making some kind of dumb mistake.  Although, it may be a bit complex, as I've tried several combinations of things and nothing has worked so far.

    Basically, I have a few DropDown objects linked to a few fields in an Access database.  I'm trying to setup a small web demo to allow users to select an item from these DropDown controls, and when several selections are made, I want the user to pass all the selected data into another Access table.  

    I'm showing a couple screen shots below, as a picture is worth 1000 words.  Maybe my naming conventions are wrong.  Not sure.  The 'DD' is for DropDown.

    So, 'rc_nameDD' is the name of the DataTextField control and the 'rc_name' is the name of the field in the Access table. 

    I don't think it has anything to do with .Selected = true;

    I'm not trying to auto-select an object.  I want a user to select an item from each of several DropDown lists and click a button to send the results to an Access database.  I think all of this is fine.  My concept, and sample code, works fine with TextBox objects.  It just won't work with DropDown objects for some reason.


    Check out my book!!!

    http://www.amazon.com/Automating-Business-Processes-Reducing-Increasing-ebook/dp/B01DJJKVZC/ref=sr_1_1?ie=UTF8&qid=1459738423&sr=8-1&keywords=ryan+shuell




    • Edited by ryguy72 Monday, April 4, 2016 3:45 PM
    Monday, April 4, 2016 2:33 PM
  • Personally since string is a class, I would have expected "reference not set to an instance of object" exception at runtime. Maybe a "string does not have a SelectedIndexChanged property or field" exception.

    Unless you got a a DropdownList named exactly the same, wich would cause one heck of a confusion for everyone.
    At wich point the compiter should tell you "SelectedIndexChanged" is a Event and those can only be part of a += or -= opeartion, not be read out.
    Unless this code somehow runs from inside the DropdownList (wich might happen if you you inherited from it).

    There are so many reasons this should not compile, the compiler can only tell you teh first one it runs into. You should stop using consecutive . for a while. Make "one field/property access or function call, before I store into a local variable" the rule. You get much more usefull "line of fault" detection out of it.
    The JIT compiler can still go and cut them out if it seems feasible, but the code will get a million times clearer and easier to debug.

    Since you are working with ASP.Net to modify a DB, understanding the page lifecycle becomes absolutley fundamental. You can do a lot of stuff fundamentally wrong by not knowing/understanding it:
    https://msdn.microsoft.com/en-us/library/ms178472.aspx


    • Edited by Christopher84 Monday, April 4, 2016 4:49 PM
    • Marked as answer by ryguy72 Tuesday, April 5, 2016 12:17 AM
    Monday, April 4, 2016 4:47 PM
  • Sorry for being so think everyone.  After all that, it was a dang naming issue!

    This works fine.

    string rc_name = dropdownlist1.SelectedItem.Text;//get text

    This link  was a good resource.

    http://stackoverflow.com/questions/11791319/getting-the-value-selected-from-drop-down-list-into-a-string-variable-c-sharp

    Thanks for the help!!


    MY BOOK

    Tuesday, April 5, 2016 12:17 AM
  • My concept, and sample code, works fine with TextBox objects.  It just won't work with DropDown objects for some reason.

    Why would a dropdown list for a Web UI be any different if you were using a combobox in a Windows forum, concerning the SelectedValue? One clicks on a line in the dropdownlist, then the SelectedValue of the item in the list is set to the SelectedValue.

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    https://www.youtube.com/watch?v=WTPGe8DcnUs

    Tuesday, April 5, 2016 1:29 AM
  • For future reference, if you had just asked "How do I get the text of the selected item in a DropDownList called dropdownlist1?", we could have told you the answer in about five seconds flat.

    Wyck and I both asked what you were trying to do, and that was all we wanted to know. We didn't need to know that the list was linked to a database, or that the whole thing was a web demo, and we really didn't need any screenshots.

    Let this be a lesson in getting to the point.

    Tuesday, April 5, 2016 3:07 AM