locked
New to this and need some help RRS feed

  • Question

  • Hello, I am learning c# and was given a copy of some video tutorials and they are using 2005, the video does not get any errors but when I have exactly what is on the screen I get errors:

    cannot implicitly convert type int into string

    cannot implicitly convert type string into int

     

    What is wrong?

    ...

    private int weight = 1;

     

    ....

     

            public string Weight
            {
                get { return weight; }
                set { weight = value; }
            }

    Friday, April 23, 2010 1:00 AM

Answers

  • private int weight;

    public int Weight

    {

         get { return weight;}

         set { weight = value;}

    }

     

    you set type does not same.


    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same;
    • Proposed as answer by Mr. Javaman Friday, April 23, 2010 1:41 AM
    • Marked as answer by Liliane Teng Tuesday, April 27, 2010 1:38 AM
    Friday, April 23, 2010 1:33 AM
  • Hi Doltek,
      The issue is you have a private varaible of type int, and you are having a propery of type string.
    since you are directly assign and return the values, type mismatch will occur.

    If you want to store string value in Weight property, declare weight as string, not as int. Because the property gets or sets string values.

    eg:

            private string weight;

            public string Weight
            {
                get { return weight; }
                set { weight = value; }
            }

    else

            private int weight;

            public int Weight
            {
                get { return weight; }
                set { weight = value; }
            }

    if you are using .NET 3.5 or greater as target framework, you no need to declare the private variable. The compiler itself will do that.

    so you can simply write

    public string Weight { get; set; }

                   OR

    public int Weight { get; set; }

     


    -- Thanks Ajith R
    • Marked as answer by Liliane Teng Tuesday, April 27, 2010 1:39 AM
    Friday, April 23, 2010 5:16 AM

All replies

  • private int weight;

    public int Weight

    {

         get { return weight;}

         set { weight = value;}

    }

     

    you set type does not same.


    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same;
    • Proposed as answer by Mr. Javaman Friday, April 23, 2010 1:41 AM
    • Marked as answer by Liliane Teng Tuesday, April 27, 2010 1:38 AM
    Friday, April 23, 2010 1:33 AM
  • I still get the same error

     

    Here is the complete code (so far) for the .cs for reference

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Hyperion
    {
        class Item
        {
            private string title;
            private string pickupText;
            private int weight = 1;
            //public int Weight;  <--- I added this in as the previous post sudgested.

            #region properties

            public string Title
            {
                get { return title; }
                set { title = value; }
            }

            public string PickupText
            {
                get { return pickupText; }
                set { pickupText = value; }
            }

            public string Weight
            {
                get { return weight; }
                set { weight = value; }
            }

            #endregion
        }
    }

    Friday, April 23, 2010 1:46 AM
  • I guess it would help if I mentioned that I was using 2010
    Friday, April 23, 2010 3:40 AM
  • Hi Doltek,
      The issue is you have a private varaible of type int, and you are having a propery of type string.
    since you are directly assign and return the values, type mismatch will occur.

    If you want to store string value in Weight property, declare weight as string, not as int. Because the property gets or sets string values.

    eg:

            private string weight;

            public string Weight
            {
                get { return weight; }
                set { weight = value; }
            }

    else

            private int weight;

            public int Weight
            {
                get { return weight; }
                set { weight = value; }
            }

    if you are using .NET 3.5 or greater as target framework, you no need to declare the private variable. The compiler itself will do that.

    so you can simply write

    public string Weight { get; set; }

                   OR

    public int Weight { get; set; }

     


    -- Thanks Ajith R
    • Marked as answer by Liliane Teng Tuesday, April 27, 2010 1:39 AM
    Friday, April 23, 2010 5:16 AM