VB and implicit conversions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chad Z. Hower aka Kudzu

    VB and implicit conversions

    I have an implicit conversion set up in an assembly from a Stream to
    something else. In C#, it works. In VB it does not.

    Does VB support implicit conversions? And if so any idea why it would work in
    a C# program but not VB?


    --
    Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
    "Programmin g is an art form that fights back"


    ELKNews - Get your free copy at http://www.atozedsoftware.com

  • Klaus Löffelmann

    #2
    Re: VB and implicit conversions

    Chad,

    no, VB doesn't support implicit castings in the current version. That is, it
    doesn't support implicit castings from non-primitive types. Only Whidbey
    will support that (and hopefully it will be produce code that is compatible
    with the code generated by C# when using the implicit operator); in the
    meanwhile, you have to use the DirectCast keyword to unbox an object.

    Klaus

    "Chad Z. Hower aka Kudzu" <cpub@hower.org > schrieb im Newsbeitrag
    news:Xns9480925 40FCF9cpub@127. 0.0.1...[color=blue]
    > I have an implicit conversion set up in an assembly from a Stream to
    > something else. In C#, it works. In VB it does not.
    >
    > Does VB support implicit conversions? And if so any idea why it would work[/color]
    in[color=blue]
    > a C# program but not VB?
    >
    >
    > --
    > Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
    > "Programmin g is an art form that fights back"
    >
    >
    > ELKNews - Get your free copy at http://www.atozedsoftware.com
    >[/color]


    Comment

    • Chad Z. Hower aka Kudzu

      #3
      Re: VB and implicit conversions

      "Klaus Löffelmann" <fornewsgroups@ loeffelmann.de> wrote in
      news:401a46c5$0 $1000$9b622d9e@ news.freenet.de :[color=blue]
      > Whidbey will support that (and hopefully it will be produce code that is
      > compatible with the code generated by C# when using the implicit
      > operator); in the meanwhile, you have to use the DirectCast keyword to
      > unbox an object.[/color]

      Ack. :(

      I understand that there are differences between VB and C#. But how can MS
      leave someting like this out? Esp from VB, the king language of anti type
      safety. Where you could string = integer. :(

      Can you give me an example of DirectCast?



      --
      Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
      "Programmin g is an art form that fights back"


      ELKNews - Get your free copy at http://www.atozedsoftware.com

      Comment

      • Armin Zingler

        #4
        Re: VB and implicit conversions

        "Chad Z. Hower aka Kudzu" <cpub@hower.org > schrieb[color=blue]
        > I have an implicit conversion set up in an assembly from a Stream to
        > something else. In C#, it works. In VB it does not.
        >
        > Does VB support implicit conversions? And if so any idea why it would
        > work in a C# program but not VB?[/color]

        Code?


        --
        Armin

        How to quote and why:



        Comment

        • Cor

          #5
          Re: VB and implicit conversions

          Hi Kudzu,

          Inclusive the integer to string, although it is originaly Armin territory
          because of that sample.
          :-))

          Dim a As Object
          a = 1
          Dim b As Integer
          b = DirectCast(a, Integer)
          MessageBox.Show (b.ToString)

          Cor


          Comment

          • Klaus Löffelmann

            #6
            Re: VB and implicit conversions

            Chad,



            I wouldn't call VB the king language of anti type safety. At least, not
            anymore. And, of course, only if you set



            Option Strict True



            at the beginning of each source file or in the project settings.With this
            setting, you can only cast those types implicitly, which are implicitly
            castable in C#, too.



            To provide more background: VB internally uses a special flag to recognize a
            type for casting implicitly. When you write a static operator implicit
            function with C#, C# generates a procedure called op_implicit, if I remember
            correctly. You can use this function from VB, too, but only by this name (so
            it's not implicitly usable, obviously...)



            However, you can't set the flag from C# (and, unfortunately, neither from
            VB), so a type conversion could be done implicitly from VB.

            But DirectCast works fine in VB:



            Dim var as mySpecialObject =DirectCast(spe cialObjectBoxed InWhatEverType,
            mySpecialObject )



            Keep in mind, that DirectCast only does the same as the cast operator does
            in C#: (In this example specialObjectBo xedInWhatEverTy pe boxed an object of
            type mySpecialObject ). It just unboxes a type. If you really want to do a
            type conversion from one type to another, you should provide a constructor
            with a parameter for your class, you could use like this:



            Dim var as new mySpecialObject (OtherSpecialOb jectToCastFrom) .



            In the constructor for mySpecialObject provide the code for the actual
            conversion.



            Klaus.




            "Chad Z. Hower aka Kudzu" <cpub@hower.org > schrieb im Newsbeitrag
            news:Xns9480998 267791cpub@127. 0.0.1...[color=blue]
            > "Klaus Löffelmann" <fornewsgroups@ loeffelmann.de> wrote in
            > news:401a46c5$0 $1000$9b622d9e@ news.freenet.de :[color=green]
            > > Whidbey will support that (and hopefully it will be produce code that is
            > > compatible with the code generated by C# when using the implicit
            > > operator); in the meanwhile, you have to use the DirectCast keyword to
            > > unbox an object.[/color]
            >
            > Ack. :(
            >
            > I understand that there are differences between VB and C#. But how can MS
            > leave someting like this out? Esp from VB, the king language of anti type
            > safety. Where you could string = integer. :(
            >
            > Can you give me an example of DirectCast?
            >
            >
            >
            > --
            > Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
            > "Programmin g is an art form that fights back"
            >
            >
            > ELKNews - Get your free copy at http://www.atozedsoftware.com
            >[/color]


            Comment

            • Chad Z. Hower aka Kudzu

              #7
              Re: VB and implicit conversions

              "Cor" <non@non.com> wrote in news:uSQ#Sty5DH A.2136@TK2MSFTN GP12.phx.gbl:[color=blue]
              > Dim a As Object
              > a = 1
              > Dim b As Integer
              > b = DirectCast(a, Integer)
              > MessageBox.Show (b.ToString)[/color]

              Thanks. A direct conversion will be much easier than that. :(

              But thanks for the answer.


              --
              Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
              "Programmin g is an art form that fights back"


              ELKNews - Get your free copy at http://www.atozedsoftware.com

              Comment

              • Chad Z. Hower aka Kudzu

                #8
                Re: VB and implicit conversions

                "Armin Zingler" <az.nospam@free net.de> wrote in news:uiS5Hoy5DH A.2392
                @TK2MSFTNGP11.p hx.gbl:[color=blue][color=green]
                >> Does VB support implicit conversions? And if so any idea why it would
                >> work in a C# program but not VB?[/color]
                >
                > Code?[/color]

                Its the very last VB example here:


                You can see the C# version directly above it. The parameter type to the Get
                method is Borland.VCL.Cla sses.TStrings, which has an implict operator added
                to it that allows conversion to it from System.IO.Strea m.

                It looks like VB users might have to do direct conversions, which at least is
                easyer than the DirectBox exmaple posted here.

                That is something like:

                new TCLRStream(MySt ream)

                instead of just:

                MyStream

                whenever TStrings is needed.


                --
                Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
                "Programmin g is an art form that fights back"


                ELKNews - Get your free copy at http://www.atozedsoftware.com

                Comment

                • Chad Z. Hower aka Kudzu

                  #9
                  Re: VB and implicit conversions

                  "Klaus Löffelmann" <fornewsgroups@ loeffelmann.de> wrote in
                  news:401a4e49$0 $1014$9b622d9e@ news.freenet.de :[color=blue]
                  > I wouldn't call VB the king language of anti type safety. At least, not
                  > anymore. And, of course, only if you set[/color]

                  Yes, its been cleaned up in .net. I just find it ironic that traditionaly
                  VB has been anything but typesafe, in fact explicitly breaking it. And now
                  that they've cleaned it up, they gave C# implicit covnersion, but not VB.
                  [color=blue]
                  > Option Strict True[/color]

                  Yes I have this. But its my understanding that this applies to being able
                  to put integers into short ints, and the type off poor variations that VB
                  used to be rampant with.
                  [color=blue]
                  > at the beginning of each source file or in the project settings.With
                  > this setting, you can only cast those types implicitly, which are
                  > implicitly castable in C#, too.[/color]

                  But it applies only to ordinals, etc AFAIK.
                  [color=blue]
                  > To provide more background: VB internally uses a special flag to
                  > recognize a type for casting implicitly. When you write a static
                  > operator implicit function with C#, C# generates a procedure called
                  > op_implicit, if I remember correctly. You can use this function from VB,
                  > too, but only by this name (so it's not implicitly usable, obviously...)[/color]

                  Yes, exactly. :)
                  [color=blue]
                  > However, you can't set the flag from C# (and, unfortunately, neither
                  > from VB), so a type conversion could be done implicitly from VB.[/color]

                  Exactly the problem it seems. :(
                  [color=blue]
                  > But DirectCast works fine in VB:
                  >
                  > Dim var as mySpecialObject =DirectCast(spe cialObjectBoxed InWhatEverType,
                  > mySpecialObject )[/color]

                  specialObjectBo xedInWhatEverTy pe would be in my case System.IO.Strea m
                  (Source) or Borland.VCL.Cla sses.TStrings (Destination)?
                  [color=blue]
                  > Keep in mind, that DirectCast only does the same as the cast operator
                  > does in C#: (In this example specialObjectBo xedInWhatEverTy pe boxed an
                  > object of type mySpecialObject ). It just unboxes a type. If you really[/color]

                  Is this the destination type or the source tyep though?
                  [color=blue]
                  > want to do a type conversion from one type to another, you should
                  > provide a constructor with a parameter for your class, you could use
                  > like this:[/color]

                  Already have that, and thats what the implicit operator does. However the
                  point is that this conversion is used often to pass in an argument, and the
                  idea was to free the user from needing to know there was even a difference.



                  --
                  Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
                  "Programmin g is an art form that fights back"


                  ELKNews - Get your free copy at http://www.atozedsoftware.com

                  Comment

                  • Armin Zingler

                    #10
                    Re: VB and implicit conversions

                    "Chad Z. Hower aka Kudzu" <cpub@hower.org > schrieb[color=blue]
                    > "Armin Zingler" <az.nospam@free net.de> wrote in
                    > news:uiS5Hoy5DH A.2392 @TK2MSFTNGP11.p hx.gbl:[color=green][color=darkred]
                    > >> Does VB support implicit conversions? And if so any idea why it
                    > >> would work in a C# program but not VB?[/color]
                    > >
                    > > Code?[/color]
                    >
                    > Its the very last VB example here:
                    > http://www.atozed.com/indy/Texts/VSIntro.iwp
                    >
                    > You can see the C# version directly above it. The parameter type to
                    > the Get method is Borland.VCL.Cla sses.TStrings, which has an implict
                    > operator added to it that allows conversion to it from
                    > System.IO.Strea m.
                    >
                    > It looks like VB users might have to do direct conversions, which at
                    > least is easyer than the DirectBox exmaple posted here.
                    >
                    > That is something like:
                    >
                    > new TCLRStream(MySt ream)
                    >
                    > instead of just:
                    >
                    > MyStream
                    >
                    > whenever TStrings is needed.[/color]

                    What is an "implicit operator"? Currently I don't understand the problem.
                    I'm happy now that I have to cast explicitly, otherwise casting errors might
                    occur at runtime because I don't get a error message at compile time.


                    --
                    Armin

                    How to quote and why:



                    Comment

                    • Chad Z. Hower aka Kudzu

                      #11
                      Re: VB and implicit conversions

                      "Armin Zingler" <az.nospam@free net.de> wrote in
                      news:OrsxIFz5DH A.2392@TK2MSFTN GP11.phx.gbl:[color=blue]
                      > What is an "implicit operator"? Currently I don't understand the
                      > problem. I'm happy now that I have to cast explicitly, otherwise casting
                      > errors might occur at runtime because I don't get a error message at
                      > compile time.[/color]

                      An implicit conversion is performed by an implicit operator in C#.

                      That is when I need to convert between two types, .net "asks" the destination
                      if it know how to convert the source into a compatible type. This is how the
                      ordinal types work internally.

                      You could never get them at runtime - C# resolves them at compile time and
                      only allows conversions that have "handlers" to do so. Basically C# auto
                      "casts" (its not truly cast in the traditional sense, but more of a
                      conversion) to the other type.


                      --
                      Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
                      "Programmin g is an art form that fights back"


                      ELKNews - Get your free copy at http://www.atozedsoftware.com

                      Comment

                      • Klaus Löffelmann

                        #12
                        Re: VB and implicit conversions

                        Chad,

                        see inlines...

                        Klaus

                        "Chad Z. Hower aka Kudzu" <cpub@hower.org > schrieb im Newsbeitrag
                        news:Xns9480A26 37E6D4cpub@127. 0.0.1...[color=blue]
                        > "Klaus Löffelmann" <fornewsgroups@ loeffelmann.de> wrote in
                        > news:401a4e49$0 $1014$9b622d9e@ news.freenet.de :[color=green]
                        > > I wouldn't call VB the king language of anti type safety. At least, not
                        > > anymore. And, of course, only if you set[/color]
                        >
                        > Yes, its been cleaned up in .net. I just find it ironic that traditionaly
                        > VB has been anything but typesafe, in fact explicitly breaking it. And now
                        > that they've cleaned it up, they gave C# implicit covnersion, but not VB.
                        >[color=green]
                        > > Option Strict True[/color]
                        >
                        > Yes I have this. But its my understanding that this applies to being able
                        > to put integers into short ints, and the type off poor variations that VB
                        > used to be rampant with.
                        >[color=green]
                        > > at the beginning of each source file or in the project settings.With
                        > > this setting, you can only cast those types implicitly, which are
                        > > implicitly castable in C#, too.[/color]
                        >
                        > But it applies only to ordinals, etc AFAIK.
                        >[color=green]
                        > > To provide more background: VB internally uses a special flag to
                        > > recognize a type for casting implicitly. When you write a static
                        > > operator implicit function with C#, C# generates a procedure called
                        > > op_implicit, if I remember correctly. You can use this function from VB,
                        > > too, but only by this name (so it's not implicitly usable, obviously...)[/color]
                        >
                        > Yes, exactly. :)
                        >[color=green]
                        > > However, you can't set the flag from C# (and, unfortunately, neither
                        > > from VB), so a type conversion could be done implicitly from VB.[/color]
                        >
                        > Exactly the problem it seems. :(
                        >[color=green]
                        > > But DirectCast works fine in VB:
                        > >
                        > > Dim var as mySpecialObject =DirectCast(spe cialObjectBoxed InWhatEverType,
                        > > mySpecialObject )[/color]
                        >
                        > specialObjectBo xedInWhatEverTy pe would be in my case System.IO.Strea m
                        > (Source) or Borland.VCL.Cla sses.TStrings (Destination)?
                        >[color=green]
                        > > Keep in mind, that DirectCast only does the same as the cast operator
                        > > does in C#: (In this example specialObjectBo xedInWhatEverTy pe boxed an
                        > > object of type mySpecialObject ). It just unboxes a type. If you really[/color]
                        >
                        > Is this the destination type or the source tyep though?[/color]

                        mySpecialObject is the one, you like other types being casted to.
                        Further provide Toxxx-Methods for your "Special"-Object, to cast them back
                        to other types, and your done!
                        [color=blue]
                        >[color=green]
                        > > want to do a type conversion from one type to another, you should
                        > > provide a constructor with a parameter for your class, you could use
                        > > like this:[/color]
                        >
                        > Already have that, and thats what the implicit operator does. However the
                        > point is that this conversion is used often to pass in an argument, and[/color]
                        the[color=blue]
                        > idea was to free the user from needing to know there was even a[/color]
                        difference.

                        But wouldn't you then create a "new old Visual Basic" behaviour, that you
                        don't like? I think, explicit casting does more for type safety than
                        implicit casting?!
                        [color=blue]
                        >
                        >
                        >
                        > --
                        > Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
                        > "Programmin g is an art form that fights back"
                        >
                        >
                        > ELKNews - Get your free copy at http://www.atozedsoftware.com
                        >[/color]


                        Comment

                        • Armin Zingler

                          #13
                          Re: VB and implicit conversions

                          "Chad Z. Hower aka Kudzu" <cpub@hower.org > schrieb[color=blue]
                          > "Armin Zingler" <az.nospam@free net.de> wrote in
                          > news:OrsxIFz5DH A.2392@TK2MSFTN GP11.phx.gbl:[color=green]
                          > > What is an "implicit operator"? Currently I don't understand the
                          > > problem. I'm happy now that I have to cast explicitly, otherwise
                          > > casting errors might occur at runtime because I don't get a error
                          > > message at compile time.[/color]
                          >
                          > An implicit conversion is performed by an implicit operator in C#.
                          >
                          > That is when I need to convert between two types, .net "asks" the
                          > destination if it know how to convert the source into a compatible
                          > type. This is how the ordinal types work internally.
                          >
                          > You could never get them at runtime - C# resolves them at compile
                          > time and only allows conversions that have "handlers" to do so.
                          > Basically C# auto "casts" (its not truly cast in the traditional
                          > sense, but more of a conversion) to the other type.[/color]

                          Thanks for the explanation. I know too little about C#


                          --
                          Armin

                          Comment

                          • Chad Z. Hower aka Kudzu

                            #14
                            Re: VB and implicit conversions

                            "Klaus Löffelmann" <fornewsgroups@ loeffelmann.de> wrote in news:401a61d7$0
                            $1039$9b622d9e@ news.freenet.de :[color=blue]
                            > mySpecialObject is the one, you like other types being casted to.
                            > Further provide Toxxx-Methods for your "Special"-Object, to cast them[/color]
                            back

                            Defeats the purpose unfortunately. It requires the user to interact
                            directly with the destination type.
                            [color=blue]
                            > But wouldn't you then create a "new old Visual Basic" behaviour, that you
                            > don't like? I think, explicit casting does more for type safety than
                            > implicit casting?![/color]

                            No, because behaviour is well defined and among truly convertible types.
                            Formerly in VB you could do 2 + 2 + 1 and sometimes get 23 because of the
                            rules of conversion of data types when added / concatted. And the fact that
                            concat and add were the same symbol. | was only added later and they still
                            preverved +.

                            These conversions are well defined with rules about what can go where, and
                            do not create commonly abmigous circumstance. They also do not allow data
                            loss, or if they do they have strict rules about it and only at the
                            override of the user.

                            Im all too well acquainted with VB prior to .net.

                            I know I will offend some people - but VB *was* a horrible mess of a
                            language. VB.net finally fixed things - but unfortunately MS also seems to
                            have just tinkered in some spots too for no reason.

                            I started work in VB 1 for DOS, long before most of you probably ever heard
                            of VB. Prior to that in PDS which was VB's successor, at least in language.

                            I've worked very heavily in VB all the way up to 4 and did some work in 5
                            and 6 as well.

                            VB 3 was a decent language with some pitfalls. As a very active VB person
                            at the time and regular VB magazine contributor I was with the rest of the
                            community hopeful. But then MS totally let us down with 4.

                            VB4 was not only incredibly buggy (Curse of MS and version 4) but it was
                            abboration. They should have taken this opportunity to fix some of the
                            issues in VB3 as a langauge. Instead they did not fix them, but actually
                            took the language BACKWARDS.

                            I went Delphi as it came out at the same time. It had everything VB4 should
                            have had and more.

                            Its nice to see that VB.net has finally "Fixed" VB.



                            --
                            Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
                            "Programmin g is an art form that fights back"


                            ELKNews - Get your free copy at http://www.atozedsoftware.com

                            Comment

                            • Chad Z. Hower aka Kudzu

                              #15
                              Re: VB and implicit conversions

                              And dont even get me started on the fact that VB had a "Option Implicit" in
                              it - let alone that it was the DEFAULT.

                              The number of bugs that this leads to is truly incredible, and only has a
                              place in scripting languages, if that.

                              Any mispelled variable becomes a new instance and instantly evaluates to 0.


                              --
                              Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
                              "Programmin g is an art form that fights back"


                              ELKNews - Get your free copy at http://www.atozedsoftware.com

                              Comment

                              Working...