This is a migrated thread and some comments may be shown as answers.

why?? c#: variable 'dataBoundItem' is only assigned to

8 Answers 231 Views
Code Analysis
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Shawn
Top achievements
Rank 1
Shawn asked on 05 Jun 2012, 08:00 PM
 
I have the following code.  For the "dataBoundItem" I am getting the message
"variable 'dataBoundItem' is only assigned to".  Why?  What is wrong with the code?

protected
void rGrid_PageList_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
       {
 
           if (e.Item is GridDataItem)
           {
                   //Get the instance of the right type
                   GridDataItem dataBoundItem = e.Item as GridDataItem;
                    
               if (e.CommandName == "RowClick")
               {
                   string PageNum = Convert.ToString(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Page_Num"]);
                   this.lbl_Page_Num.Text = PageNum.ToString();
                   this.rGrid_User_To_Page.DataBind();
 
               }
 
               if (e.CommandName == "Delete")
               {
                   this.Sds_PageList.Delete();
               }
           }
       }

8 Answers, 1 is accepted

Sort by
0
Zdravko
Telerik team
answered on 06 Jun 2012, 12:55 PM
Hi Shawn,

 Thanks for the feedback.
I tried to reproduce your case and I succeeded. In this case JustCode provide a right warning because your variable is not used further in your code and it is only assigned to.
Thanks.

Please, do not hesitate to contact us if you have any questions.

Regards,
Zdravko
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Shawn
Top achievements
Rank 1
answered on 06 Jun 2012, 01:00 PM
Thanks for your response.  I guess I should have shown you the rest of the code then -- those variables ARE used; see below:

protected void rBtn_Submit_Faults_Click(object sender, EventArgs e)
{
             
            String FaultDesc;
            int SectionId;
            int GroupId = Convert.ToInt32(this.rCbo_Group.SelectedValue);
            string WorkCenterId;
            String SeqNum = this.rCbo_Seq.SelectedValue;
            int Cpid;
            int TravelerStep;
            string WorkType;
            int StepUid;
            string mechanicTrade;
            int schday;
            decimal itemnum;
            string mechanicnum;
            string zone;
 
 
            foreach (GridDataItem item in this.rGrid_Task.SelectedItems)
            {
                TravelerStep = Convert.ToInt32(item["StepId"].Text);
                WorkCenterId = item["WorkCenterId"].Text;
                FaultDesc = item["TaskDescription"].Text;
                Cpid = Convert.ToInt32(item["Cpid"].Text);
                WorkType = item["TypeTask"].Text;
                StepUid = Convert.ToInt32(item["StepUi"].Text);
                GroupId = Convert.ToInt32(item["GroupId"].Text);
                schday = Convert.ToInt32(item["SchdDay"].Text);
                SectionId = Convert.ToInt32(item["SectionId"].Text);
                mechanicnum = item["Mechanic"].Text;
                itemnum = Convert.ToDecimal(item["ItemNum"].Text);
                zone = item["zone"].Text;
                int laststep;
                int FaultId;
                mechanicTrade = item["Mechanic"].Text;
                laststep = Convert.ToInt32(LastStepAdapter.Web_Get_Last_Traveler_Step(SeqNum, Convert.ToInt32(this.rCbo_Group.SelectedValue)));
                Faults.Insert_Group_Fault(SeqNum,StepUid,"",this.rCbo_Production_Order.SelectedValue);
                FaultId= Convert.ToInt32(Faults.Get_Last_Fault(SeqNum, StepUid));
               //FaultSubSteps.Insert_Sub_Step(FaultId, Convert.ToInt32(this.rCbo_Production_Order.SelectedValue)); insert substep added to fault sp
 
            }
            //RadAjaxManager.GetCurrent(this).Alert(myalert);
            this.rGrid_Faults.Rebind();
            this.rGrid_Task.SelectedIndexes.Clear();
 
}
0
Shawn
Top achievements
Rank 1
answered on 06 Jun 2012, 01:07 PM
Ahh...just realized I posted a different section of code; but that example is probably even better.  All the variables defined in that snippet are flagged in the same way, but as you can see they are used in the foreach loop.
0
Ivan
Telerik team
answered on 06 Jun 2012, 01:19 PM
Hi,

Thank you for your feedback.
Yes, all the variables are used in the foreach loop, but they are only assigned to. You never read them.
That is what the warning means - you use these variables only to assign values to them and never read these values.
For example the "zone" variable. You declare it and then assign it the text of the item["zone"] and that is all. It is only assigned to.
If you have any more questions, remarks or I am missing something, please do not hesitate to contact us again!

All the best,
Ivan
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Shawn
Top achievements
Rank 1
answered on 06 Jun 2012, 01:46 PM
Thanks Ivan,

Maybe I'm having trouble understanding the concept here.  For most of the foreach loop we're converting the datatype of the value that gets pulled into the grid and assigning it to the variables declared.  What's wrong with that?  So on button click we assign values to the variables.  I guess beyond that it isn't doing anything with those assignments -- at least not yet.  Are you saying basically that it appears this code isn't doing anything?  i.e. should be stripped out?  Is there a case where this would be desired?
0
Ivan
Telerik team
answered on 06 Jun 2012, 02:14 PM
Hello,

Thank you for your feedback.
As you said it yourself: "we assign values to the variables.  I guess beyond that it isn't doing anything with those assignments". And that is what the warning means- you only assign the variables and do not use them anymore.
Could that part be stripped out?
It depends totally on you. It is your code. As you said you could change the code later and start using these variables.

Regards,
Ivan
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Shawn
Top achievements
Rank 1
answered on 06 Jun 2012, 02:24 PM
OK, one more thing.  It looks to me like this code assigns the values from the grid "rGrid_Task" to the variables defined and then attempts to rebind those with the "rGrid_Faults" grid (this.rGrid_Faults.Rebind();).  Would that be the action or purpose for setting these variables?  Wouldn't that cause these variables to be read?
0
Ivan
Telerik team
answered on 06 Jun 2012, 02:54 PM
Hello,

Thank you for your feedback. I do not see where these variables are added to the data source of "rGrid_Faults" grid. They are only assigned to. Maybe the initial idea was to add them to that data source?

Greetings,
Ivan
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
Code Analysis
Asked by
Shawn
Top achievements
Rank 1
Answers by
Zdravko
Telerik team
Shawn
Top achievements
Rank 1
Ivan
Telerik team
Share this question
or