locked
cannot implicitly convert type void to int in c#

    Question

  • I having some problem with the int conversion and its complaining about streamID that says  "cannot implicitly convert type void to int in c#"   That ddlstreamid.SelectedValue is a dropdownlist. I was trying to convert to int.

     

    private int UpdateCourse()
        {
            int streamID = Int32.Parse(ddlstreamid.SelectedValue);


            return
                ITSquaredController.UpdateCourse(int.Parse(courseid.Text), streamID, coursename.Text, coursedesc.Text) ;

        }

    Friday, October 15, 2010 8:18 PM

Answers

  • Okay - so that's a new method. It sounds like it needs to return the course id - if so, you can do:

     

    private int UpdateCourse()
    {
      int streamID = Int32.Parse(ddlstreamid.SelectedValue);
      int courseId = int.Parse(courseid.Text);
      ITSquaredController.UpdateCourse(courseId, streamID, coursename.Text, coursedesc.Text) ;
    
      return courseId;
    }
    
    I have no idea exactly what SetCourseInfo is expecting, though - if it's the stream ID, you can return that instead.


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, October 15, 2010 8:49 PM
    Moderator

All replies

  • The error is because ITSquaredController.UpdateCourse does not return an int - it's a method that is defined as:

        void ITSquaredController.UpdateCourse(int courseId, ...)

     

    Unless you need a return value, you can avoid this error by rewriting your code as:

     

     

    // No need to return a value, since there's nothing to return
    private void UpdateCourse()
    {
        int streamID = Int32.Parse(ddlstreamid.SelectedValue);
    
    
          ITSquaredController.UpdateCourse(int.Parse(courseid.Text), streamID, coursename.Text, coursedesc.Text) ;
    
    }
    
    Alternatively, you can return some integer value from your method.


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, October 15, 2010 8:22 PM
    Moderator
  • actually I tried that one that you suggested like

    private
     void
     UpdateCourse()
    {
      int
     streamID = Int32.Parse(ddlstreamid.SelectedValue);
    
    
       ITSquaredController.UpdateCourse(int
    .Parse(courseid.Text), streamID, coursename.Text, coursedesc.Text) ;
    
    }
    

    it works but its complaining about SetCourseInfo(UpdateCourse());    or  SetCourseInfo(AddNewCourse());    that says about

    The best overloaded method match for 'DesktopModules_Class_Module_CourseAttribute.SetCourseInfo(int)' has some invalid arguments   

     

     

    protected void btnsave_Click(object sender, EventArgs e)
        {
            // int streamID = Int32.Parse(ddlstreamid.SelectedValue);
            try
            {
                string mode = (string)this.ViewState["mode"];
                if (mode == "add")
                {
                    SetCourseInfo(AddNewCourse());
                    LBLMessage.Text = "New Course is created!";
                }
                else
                {
                    SetCourseInfo(UpdateCourse());
                    LBLMessage.Text = "Course is updated!";
                }
            }
            catch (Exception ex)
            {
                LBLMessage.Text = ex.Message;
            }

    Friday, October 15, 2010 8:46 PM
  • Okay - so that's a new method. It sounds like it needs to return the course id - if so, you can do:

     

    private int UpdateCourse()
    {
      int streamID = Int32.Parse(ddlstreamid.SelectedValue);
      int courseId = int.Parse(courseid.Text);
      ITSquaredController.UpdateCourse(courseId, streamID, coursename.Text, coursedesc.Text) ;
    
      return courseId;
    }
    
    I have no idea exactly what SetCourseInfo is expecting, though - if it's the stream ID, you can return that instead.


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, October 15, 2010 8:49 PM
    Moderator
  • thanks it actually work.
    Friday, October 15, 2010 9:12 PM