Difficult one.. please help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    Difficult one.. please help

    Hi I have trying to solve this but I can't understand why it's not working.

    I want to cast a variable dynamically with a type that I pass truogh a string.
    I am receiving a value and want to check if the type is correct before saving it to the XML.
    I am getting the Type from xsd and pass this to a function to convert the newvalue to the proper type.
    I want to avoid doing if statement to check for the type and then try to convert it.

    This is an example my code.

    Dim strType as string = "System.Int 32"
    Dim val as object

    Dim type As Type = (type.GetType(s trType))

    val = CType(val, Type)

    I have tried also passing a different object of type 'Type'

    Is this possible?



    Please help

  • Cor Ligthert

    #2
    Re: Difficult one.. please help

    Helllo mysterious,

    Are you inventing the wheel, all is already build in.

    Some examples
    Dim a As Integer = 1
    Dim b As String = "2"
    Dim c As Decimal = 10.1D
    Dim d As Integer
    Dim f As Decimal
    Dim g As String
    Dim h As Object
    d = CInt(b)
    g = c.ToString
    f = CDec(a)
    h = a
    d = CInt(h)

    You need to cast it to the type you want and especially in that is VBNet
    very powerfull. (I only showed you only a very small selection of methods to
    do that)

    Cor

    I want to cast a variable dynamically with a type that I pass truogh a
    string.
    I am receiving a value and want to check if the type is correct before
    saving it to the XML.
    I am getting the Type from xsd and pass this to a function to convert the
    newvalue to the proper type.
    I want to avoid doing if statement to check for the type and then try to
    convert it.

    This is an example my code.

    Dim strType as string = "System.Int 32"
    Dim val as object
    Dim type As Type = (type.GetType(s trType))
    val = CType(val, Type)
    I have tried also passing a different object of type 'Type'
    Is this possible?

    Please help


    Comment

    • One Handed Man \( OHM - Terry Burns \)

      #3
      Re: Difficult one.. please help

      I think your missing his point. He wants to cast at runtime, and use the
      typename in a string to determine what type the object will be cast to.

      --

      OHM ( Terry Burns )
      . . . One-Handed-Man . . .
      If U Need My Email ,Ask Me

      Time flies when you don't know what you're doing

      "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
      news:%23pQEYtAj EHA.2992@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > Helllo mysterious,
      >
      > Are you inventing the wheel, all is already build in.
      >
      > Some examples
      > Dim a As Integer = 1
      > Dim b As String = "2"
      > Dim c As Decimal = 10.1D
      > Dim d As Integer
      > Dim f As Decimal
      > Dim g As String
      > Dim h As Object
      > d = CInt(b)
      > g = c.ToString
      > f = CDec(a)
      > h = a
      > d = CInt(h)
      >
      > You need to cast it to the type you want and especially in that is VBNet
      > very powerfull. (I only showed you only a very small selection of methods[/color]
      to[color=blue]
      > do that)
      >
      > Cor
      >
      > I want to cast a variable dynamically with a type that I pass truogh a
      > string.
      > I am receiving a value and want to check if the type is correct before
      > saving it to the XML.
      > I am getting the Type from xsd and pass this to a function to convert the
      > newvalue to the proper type.
      > I want to avoid doing if statement to check for the type and then try to
      > convert it.
      >
      > This is an example my code.
      >
      > Dim strType as string = "System.Int 32"
      > Dim val as object
      > Dim type As Type = (type.GetType(s trType))
      > val = CType(val, Type)
      > I have tried also passing a different object of type 'Type'
      > Is this possible?
      >
      > Please help
      >
      >[/color]


      Comment

      • Cor Ligthert

        #4
        Re: Difficult one.. please help

        Terry,

        I know however I wrote inventing the wheel, why would that be needed, one
        reason can be that a lot of people are looking to use the Var from scripting
        languages, however when you put a value in a object there is not any need
        for that.

        You can get it back by just naming the type of the receiving value.

        Cor


        Comment

        • One Handed Man \( OHM - Terry Burns \)

          #5
          Re: Difficult one.. please help

          In his scenario, he needs to discover the type for his XML storage.

          --

          OHM ( Terry Burns )
          . . . One-Handed-Man . . .
          If U Need My Email ,Ask Me

          Time flies when you don't know what you're doing

          "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
          news:eU2YICBjEH A.3988@tk2msftn gp13.phx.gbl...[color=blue]
          > Terry,
          >
          > I know however I wrote inventing the wheel, why would that be needed, one
          > reason can be that a lot of people are looking to use the Var from[/color]
          scripting[color=blue]
          > languages, however when you put a value in a object there is not any need
          > for that.
          >
          > You can get it back by just naming the type of the receiving value.
          >
          > Cor
          >
          >[/color]


          Comment

          • Cor Ligthert

            #6
            Re: Difficult one.. please help

            Terry

            As sender you can just cast it as I wrote, as a receiving field it is with
            the informaiton I have now an almost a ridiculous question.

            A kind of inventing the wheel.

            I know that you can make a loop in it to fill it, however from where than?

            Create an unknown XML file from an unknown table

            (when it is a datatable by the way it is)
            ds.add.table(x)

            Cor


            Comment

            • One Handed Man \( OHM - Terry Burns \)

              #7
              Re: Difficult one.. please help

              OK, when you create an XML/XSD combination, you need to know the Type of the
              Data. He is saying he does not know this at design time, so he needs to
              check this when he creates the XSD. I understand what you are saying, but
              I'm not yet convinced that this is enough for the OP, he has not been
              verbosely Explicit in what he is trying to achieve, so I think we should
              wait until he replies rather than trying to interpolate his post.



              --

              OHM ( Terry Burns )
              . . . One-Handed-Man . . .
              If U Need My Email ,Ask Me

              Time flies when you don't know what you're doing

              "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
              news:eiwx82BjEH A.4092@TK2MSFTN GP10.phx.gbl...[color=blue]
              > Terry
              >
              > As sender you can just cast it as I wrote, as a receiving field it is with
              > the informaiton I have now an almost a ridiculous question.
              >
              > A kind of inventing the wheel.
              >
              > I know that you can make a loop in it to fill it, however from where[/color]
              than?[color=blue]
              >
              > Create an unknown XML file from an unknown table
              >
              > (when it is a datatable by the way it is)
              > ds.add.table(x)
              >
              > Cor
              >
              >[/color]


              Comment

              • David

                #8
                Re: Difficult one.. please help

                On 2004-08-27, <.> <> wrote:[color=blue]
                > This is a multi-part message in MIME format.
                >
                > ------=_NextPart_000_ 000A_01C48C4E.A 386BA70
                > Content-Type: text/plain;
                > charset="iso-8859-1"
                > Content-Transfer-Encoding: quoted-printable[/color]

                If you could turn that off, it would be nice...
                [color=blue]
                >
                > Hi I have trying to solve this but I can't understand why it's not =
                > working.[/color]

                The problem is that CType doesn't do what you think it does. It isn't a
                runtime function taking a System.Type, it's a keyword that is bound at
                compile time.

                Also, Type.GetType is really worth avoiding if possible, there's all
                kinds of undocumented wackiness in there.

                All in all, if it's possible your best bet is to follow Cor's advice.
                Have some sort of dispatch function that includes all the types you care
                about in a case statement, and hard-code the conversions...

                Select Case typeString
                Case "System.Int 32"
                variable = CInt(s)

                Case "System.Boolean "
                variable = CBool(s)


                And so on...



                Comment

                • Guest's Avatar

                  #9
                  Re: Difficult one.. please help

                  Hi There,

                  what I'm need to do is to validate the NewValue before saving it in the xml.
                  At the moment I am validating the xml/xsd everytime I change a value.

                  I would like to avoid writting the following
                  Select Case typeString
                  Case "System.Int 32"
                  variable = CInt(s)

                  Case "System.Boolean "
                  variable = CBool(s)

                  but for some reason I can't use the following
                  I am getting the Datatype from the XSD Schema file.
                  I hope I made myself clearer...

                  Thanks for the help and coments.

                  Example of what I am trying to achieve..

                  covertType ("xx", XMLSchemType)

                  private function convertType(byr ef val as string , byref strType as string)

                  ' this is return from XMLSchemaType ---- "System.Int 32"
                  Dim newval as object

                  Dim myType As Type = (type.GetType(X MLSchemaType))

                  try
                  newval = CType(val, myType) ---- although myType is Type it will not be
                  accepted.. How can I do this..
                  Catch er as Exeption
                  console.write ("Value is of wrong data type")
                  End Try

                  end function





                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: Difficult one.. please help

                    ..
                    It sounds like you want to use Convert.ChangeT ype.

                    Dim strType as string = "System.Int 32"
                    Dim val as object

                    Dim type As Type = (type.GetType(s trType))

                    val = "100"

                    val = Convert.ChangeT ype(val, type)

                    Will convert the string "100" to the integer 100.

                    See System.Convert class for restrictions.

                    Hope this helps
                    Jay


                    <.> wrote in message news:unikIt$iEH A.2764@TK2MSFTN GP11.phx.gbl...
                    Hi I have trying to solve this but I can't understand why it's not working.

                    I want to cast a variable dynamically with a type that I pass truogh a
                    string.
                    I am receiving a value and want to check if the type is correct before
                    saving it to the XML.
                    I am getting the Type from xsd and pass this to a function to convert the
                    newvalue to the proper type.
                    I want to avoid doing if statement to check for the type and then try to
                    convert it.

                    This is an example my code.

                    Dim strType as string = "System.Int 32"
                    Dim val as object

                    Dim type As Type = (type.GetType(s trType))

                    val = CType(val, Type)

                    I have tried also passing a different object of type 'Type'

                    Is this possible?



                    Please help


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: Difficult one.. please help

                      ..
                      In addition to System.Convert class I identified in my earlier post, there
                      is System.Xml.XmlC onvert, however I am not seeing a "ChangeType " on the
                      XmlConvert class.

                      The advantage of XmlConvert is it uses XML formatting for the conversion,
                      the advantage of Convert it is will change the type of an object from one
                      type to another.

                      Hope this helps
                      Jay

                      <.> wrote in message news:OdC7HpDjEH A.592@TK2MSFTNG P11.phx.gbl...[color=blue]
                      > Hi There,
                      >
                      > what I'm need to do is to validate the NewValue before saving it in the[/color]
                      xml.[color=blue]
                      > At the moment I am validating the xml/xsd everytime I change a value.
                      >
                      > I would like to avoid writting the following
                      > Select Case typeString
                      > Case "System.Int 32"
                      > variable = CInt(s)
                      >
                      > Case "System.Boolean "
                      > variable = CBool(s)
                      >
                      > but for some reason I can't use the following
                      > I am getting the Datatype from the XSD Schema file.
                      > I hope I made myself clearer...
                      >
                      > Thanks for the help and coments.
                      >
                      > Example of what I am trying to achieve..
                      >
                      > covertType ("xx", XMLSchemType)
                      >
                      > private function convertType(byr ef val as string , byref strType as[/color]
                      string)[color=blue]
                      >
                      > ' this is return from XMLSchemaType ---- "System.Int 32"
                      > Dim newval as object
                      >
                      > Dim myType As Type = (type.GetType(X MLSchemaType))
                      >
                      > try
                      > newval = CType(val, myType) ---- although myType is Type it will not[/color]
                      be[color=blue]
                      > accepted.. How can I do this..
                      > Catch er as Exeption
                      > console.write ("Value is of wrong data type")
                      > End Try
                      >
                      > end function
                      >
                      >
                      >
                      >
                      >[/color]


                      Comment

                      Working...