Cannot Convert string to class type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Queen Soso
    New Member
    • Dec 2009
    • 13

    Cannot Convert string to class type

    hi there,

    When I run this line I get an error:

    obj.number_type = dropdownlist1.s electedItem.val ue;

    number_type is a class type and I want to put a string value in it from a drop down list.

    this is the error:

    Cannot implicitly convert type to 'TEL.MODEL.TEL_ T_NUMBER_TYPE.
    please help :(
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    number_type is a class type
    Then you need to add a property to your Number_Type class to receive the string

    obj.number_type .TelType

    Comment

    • Queen Soso
      New Member
      • Dec 2009
      • 13

      #3
      I tried that but it gives me this error:

      Input String was not in correct Format

      :(

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        I think you were/are on the right track.
        The error message is telling you that it doesn't know how to convert your type "Tel.Model.Tel_ T_Number_type" to a string.
        You need to do that, then send that string to your class.

        Probably something like:
        obj.number_type .TelType = dropdownlist.se lecteditem.valu e.ToString();

        Comment

        • Queen Soso
          New Member
          • Dec 2009
          • 13

          #5
          I tried that it gives me the same error ...

          Comment

          • ThatThatGuy
            Recognized Expert Contributor
            • Jul 2009
            • 453

            #6
            Hey... do this thing

            obj.number_type =dropdownlist1. SelectedItem.To String();

            Comment

            • Queen Soso
              New Member
              • Dec 2009
              • 13

              #7
              I did that it gives the same error,,,,

              Comment

              • cloud255
                Recognized Expert Contributor
                • Jun 2008
                • 427

                #8
                Could you post the entire class definition for 'number_type'?
                If I understand the situation correctly, you have an object called obj one of its members is a number_type object. This object you say is a class. If this is correct the code will never work. C# does not know how to change a string into a number_type (which you defined).

                Try instead to add a string variable to the obj class and assign the value to that variable.

                Comment

                • Queen Soso
                  New Member
                  • Dec 2009
                  • 13

                  #9
                  obj is an object of a class called TEL_T_NUMBER, this is how I created it:
                  TEL_T_NUMBER obj = new TEL_T_NUMBER();

                  Then I used TEL_T_NUMBER properties in this obj like:
                  obj.Number_ID = Convert. ToInt32(textbox 1.text);

                  one of these propreties is number_type which is another class connected to this class and therefore it's treated as a property of TEL_T_NUMBER class.

                  I hope you get it...

                  Comment

                  • cloud255
                    Recognized Expert Contributor
                    • Jun 2008
                    • 427

                    #10
                    Originally posted by Queen Soso
                    one of these propreties is number_type which is another class connected to this class and therefore it's treated as a property of TEL_T_NUMBER class.

                    I hope you get it...
                    That is exactly what I said earlier, you have a class number_type as a property, but the number_type cannot have a string directly assigned to it BECAUSE it is a class. C# does not know how to convert a string into an instance of your custom class.

                    Does number_type have a string property as a member? If so you can assign the value to that member:

                    Code:
                    obj.number_type.MyString = dropdownlist1.selectedItem.value;

                    Comment

                    • Queen Soso
                      New Member
                      • Dec 2009
                      • 13

                      #11
                      okay I created a string property to the number_type class, i did this:

                      obj.number_type .myValue = dropdownlist1.s electedItem.val ue;

                      the right side now is a string but it gives this error:
                      Input String was not in correct Format

                      What to do now?

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Did you try using DropDownList.Se lectedValue Property. Make sure that the Selected Value is a String...if it isn't then you'll run into this problem.

                        -Frinny

                        Comment

                        • Queen Soso
                          New Member
                          • Dec 2009
                          • 13

                          #13
                          it is a string, both sides are string...

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            Obviously it's not a string as you expect it to be.
                            Are you sure that the Selected Value is not Null?

                            -Frinny

                            Comment

                            • Queen Soso
                              New Member
                              • Dec 2009
                              • 13

                              #15
                              its not null i connected the dropdownlist with a table from the database.

                              Comment

                              Working...