Object operations: C# vs. VB

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vik

    Object operations: C# vs. VB

    I need to make some calculations, e.g. addition, on the objects x and y
    which are numbers (of the same or different types) and may be DBNulls.

    In VB, the following code is correct:
    Public Shared Function XYZ(ByVal x, ByVal y)
    XYZ = x + y
    End Function

    In C#, the identical code is wrong:
    public static object XYZ(object x, object y) {
    object z = x + y; //cannot add objects
    return z;
    }

    Should I write the different procedures in C# for the different data types
    or is there a better way?

    Thanks.


  • Michael Cummings

    #2
    Re: Object operations: C# vs. VB

    I ran across this blog entry today, which could help if you are using
    generics.


    Otherwise you'll need to set up type specific XYZ methods.

    Michael Cummings
    michaelc AT NOSPAM magenic DOT com
    Magenic Technologies

    "Vik" <viktorum@==yah oo.com==> wrote in message
    news:uoN0l3IlGH A.4104@TK2MSFTN GP04.phx.gbl...[color=blue]
    >I need to make some calculations, e.g. addition, on the objects x and y
    >which are numbers (of the same or different types) and may be DBNulls.
    >
    > In VB, the following code is correct:
    > Public Shared Function XYZ(ByVal x, ByVal y)
    > XYZ = x + y
    > End Function
    >
    > In C#, the identical code is wrong:
    > public static object XYZ(object x, object y) {
    > object z = x + y; //cannot add objects
    > return z;
    > }
    >
    > Should I write the different procedures in C# for the different data types
    > or is there a better way?
    >
    > Thanks.
    >
    >[/color]


    Comment

    • Marina Levit [MVP]

      #3
      Re: Object operations: C# vs. VB

      That is because VB is a different language.

      It has something called Option Strict. When it is off, VB does almost no
      type safety checking. It will do something called late binding, which means
      figuring out the data types of objects at run time, and casting them then.
      This means you can have something like:

      Dim obj as new Object()

      obj.SomeFunctio n()

      At run time, this will fail, since the Object class has no SomeFunction
      method. However, this will compile with option strict off. And only at run
      time will you get an error once the runtime figures out that there is no
      SomeFunction class here. If 'obj' was pointing to an instance of a class
      that had such a method, then it would be able to run it.

      In general, I always recommend keeping Option Strict On. Generally, it will
      allow you to catch errors at compile time rather then at run time.

      "Vik" <viktorum@==yah oo.com==> wrote in message
      news:uoN0l3IlGH A.4104@TK2MSFTN GP04.phx.gbl...[color=blue]
      >I need to make some calculations, e.g. addition, on the objects x and y
      >which are numbers (of the same or different types) and may be DBNulls.
      >
      > In VB, the following code is correct:
      > Public Shared Function XYZ(ByVal x, ByVal y)
      > XYZ = x + y
      > End Function
      >
      > In C#, the identical code is wrong:
      > public static object XYZ(object x, object y) {
      > object z = x + y; //cannot add objects
      > return z;
      > }
      >
      > Should I write the different procedures in C# for the different data types
      > or is there a better way?
      >
      > Thanks.
      >
      >[/color]


      Comment

      • sloan

        #4
        Re: Object operations: C# vs. VB


        As illuded to earlier:

        If you put these 2 items at the top of each and every .vb (Vb.net class)
        file you have:

        Option Strict On

        Option Explicit On



        then that makes it much more like c# as it can get.






        "Vik" <viktorum@==yah oo.com==> wrote in message
        news:uoN0l3IlGH A.4104@TK2MSFTN GP04.phx.gbl...[color=blue]
        > I need to make some calculations, e.g. addition, on the objects x and y
        > which are numbers (of the same or different types) and may be DBNulls.
        >
        > In VB, the following code is correct:
        > Public Shared Function XYZ(ByVal x, ByVal y)
        > XYZ = x + y
        > End Function
        >
        > In C#, the identical code is wrong:
        > public static object XYZ(object x, object y) {
        > object z = x + y; //cannot add objects
        > return z;
        > }
        >
        > Should I write the different procedures in C# for the different data types
        > or is there a better way?
        >
        > Thanks.
        >
        >[/color]


        Comment

        • Vik

          #5
          Re: Object operations: C# vs. VB

          Thank you.

          Are generics available in .NET 1.1?

          The following code does not work (many errors on the function declaration):
          using System;
          public class GenericMethod {
          public static E XYZ< E >( E x, E y ) {
          E z = x + y;
          return z;
          }
          }

          Vik

          "Michael Cummings" <michaelc@magen ic.com> wrote in message
          news:%23%234ORB JlGHA.1640@TK2M SFTNGP02.phx.gb l...[color=blue]
          >I ran across this blog entry today, which could help if you are using
          >generics.
          > http://weblogs.asp.net/cnagel/archiv...06/386147.aspx
          >
          > Otherwise you'll need to set up type specific XYZ methods.
          >
          > Michael Cummings
          > michaelc AT NOSPAM magenic DOT com
          > Magenic Technologies
          >
          > "Vik" <viktorum@==yah oo.com==> wrote in message
          > news:uoN0l3IlGH A.4104@TK2MSFTN GP04.phx.gbl...[color=green]
          >>I need to make some calculations, e.g. addition, on the objects x and y
          >>which are numbers (of the same or different types) and may be DBNulls.
          >>
          >> In VB, the following code is correct:
          >> Public Shared Function XYZ(ByVal x, ByVal y)
          >> XYZ = x + y
          >> End Function
          >>
          >> In C#, the identical code is wrong:
          >> public static object XYZ(object x, object y) {
          >> object z = x + y; //cannot add objects
          >> return z;
          >> }
          >>
          >> Should I write the different procedures in C# for the different data
          >> types or is there a better way?
          >>
          >> Thanks.
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Peter Bromberg [C# MVP]

            #6
            RE: Object operations: C# vs. VB

            If the objects may be numbers but of differenty types and especially if they
            could be DbNull.Value then you are going to have to do some type checking in
            the method. Above all, as mentioned by other posters, with Option Strict and
            Option Explicit turned on as they always should be, your VB.NET method is not
            equivalent to the C# one, it's signature should be

            Public Shared Function XYZ(ByVal x as Object, ByVal y as Object)

            -- to be equivalent to the C# version.

            In general, I would have to agree that using VB.NET's weak GetObjectByValu e
            semantics to avoid strict type checking is almost never a good idea.

            Peter



            --
            Co-founder, Eggheadcafe.com developer portal:

            UnBlog:





            "Vik" wrote:
            [color=blue]
            > I need to make some calculations, e.g. addition, on the objects x and y
            > which are numbers (of the same or different types) and may be DBNulls.
            >
            > In VB, the following code is correct:
            > Public Shared Function XYZ(ByVal x, ByVal y)
            > XYZ = x + y
            > End Function
            >
            > In C#, the identical code is wrong:
            > public static object XYZ(object x, object y) {
            > object z = x + y; //cannot add objects
            > return z;
            > }
            >
            > Should I write the different procedures in C# for the different data types
            > or is there a better way?
            >
            > Thanks.
            >
            >
            >[/color]

            Comment

            • Michael Cummings

              #7
              Re: Object operations: C# vs. VB

              No, generics are only available in .Net 2.0.
              If you are using .Net 1.1, you'll need to use different methods for the
              different types you need to support.

              "Vik" <viktorum@==yah oo.com==> wrote in message
              news:O676INKlGH A.3512@TK2MSFTN GP03.phx.gbl...[color=blue]
              > Thank you.
              >
              > Are generics available in .NET 1.1?
              >
              > The following code does not work (many errors on the function
              > declaration):
              > using System;
              > public class GenericMethod {
              > public static E XYZ< E >( E x, E y ) {
              > E z = x + y;
              > return z;
              > }
              > }
              >
              > Vik
              >
              > "Michael Cummings" <michaelc@magen ic.com> wrote in message
              > news:%23%234ORB JlGHA.1640@TK2M SFTNGP02.phx.gb l...[color=green]
              >>I ran across this blog entry today, which could help if you are using
              >>generics.
              >> http://weblogs.asp.net/cnagel/archiv...06/386147.aspx
              >>
              >> Otherwise you'll need to set up type specific XYZ methods.
              >>
              >> Michael Cummings
              >> michaelc AT NOSPAM magenic DOT com
              >> Magenic Technologies
              >>
              >> "Vik" <viktorum@==yah oo.com==> wrote in message
              >> news:uoN0l3IlGH A.4104@TK2MSFTN GP04.phx.gbl...[color=darkred]
              >>>I need to make some calculations, e.g. addition, on the objects x and y
              >>>which are numbers (of the same or different types) and may be DBNulls.
              >>>
              >>> In VB, the following code is correct:
              >>> Public Shared Function XYZ(ByVal x, ByVal y)
              >>> XYZ = x + y
              >>> End Function
              >>>
              >>> In C#, the identical code is wrong:
              >>> public static object XYZ(object x, object y) {
              >>> object z = x + y; //cannot add objects
              >>> return z;
              >>> }
              >>>
              >>> Should I write the different procedures in C# for the different data
              >>> types or is there a better way?
              >>>
              >>> Thanks.
              >>>
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • David Anton

                #8
                RE: Object operations: C# vs. VB

                Not really.
                Public Shared Function XYZ(ByVal x, ByVal y)
                is just a shorthand in VB of writing
                Public Shared Function XYZ(ByVal x As Object, ByVal y As Object) As Object

                (yet another questionable 'feature' of VB...)
                --
                David Anton
                Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

                Instant C#: VB to C# converter
                Instant VB: C# to VB converter
                Instant C++: C# to C++ converter
                Instant C++: VB to C++ converter


                "Peter Bromberg [C# MVP]" wrote:
                [color=blue]
                > If the objects may be numbers but of differenty types and especially if they
                > could be DbNull.Value then you are going to have to do some type checking in
                > the method. Above all, as mentioned by other posters, with Option Strict and
                > Option Explicit turned on as they always should be, your VB.NET method is not
                > equivalent to the C# one, it's signature should be
                >
                > Public Shared Function XYZ(ByVal x as Object, ByVal y as Object)
                >
                > -- to be equivalent to the C# version.
                >
                > In general, I would have to agree that using VB.NET's weak GetObjectByValu e
                > semantics to avoid strict type checking is almost never a good idea.
                >
                > Peter
                >
                >
                >
                > --
                > Co-founder, Eggheadcafe.com developer portal:
                > http://www.eggheadcafe.com
                > UnBlog:
                > http://petesbloggerama.blogspot.com
                >
                >
                >
                >
                > "Vik" wrote:
                >[color=green]
                > > I need to make some calculations, e.g. addition, on the objects x and y
                > > which are numbers (of the same or different types) and may be DBNulls.
                > >
                > > In VB, the following code is correct:
                > > Public Shared Function XYZ(ByVal x, ByVal y)
                > > XYZ = x + y
                > > End Function
                > >
                > > In C#, the identical code is wrong:
                > > public static object XYZ(object x, object y) {
                > > object z = x + y; //cannot add objects
                > > return z;
                > > }
                > >
                > > Should I write the different procedures in C# for the different data types
                > > or is there a better way?
                > >
                > > Thanks.
                > >
                > >
                > >[/color][/color]

                Comment

                • Göran Andersson

                  #9
                  Re: Object operations: C# vs. VB

                  VB does automatic conversion to a data type that handles the largest
                  conceivable value for the operands. The equivalent C# code would be:

                  public static object XYZ(object x, object y) {
                  object z = Convert.ToDoubl e(x) + Convert.ToDoubl e(y);
                  return z;
                  }

                  To handle DBNull values you have to check if the type of each parameter
                  is DBNull before you try to convert them, and return an appropriate value:

                  public static object XYZ(object x, object y) {
                  if (x is DBNull) return DBNull.Value;
                  if (y is DBNull) return DBNull.Value;
                  return Convert.ToDoubl e(x) + Convert.ToDoubl e(y);
                  }

                  Vik wrote:[color=blue]
                  > I need to make some calculations, e.g. addition, on the objects x and y
                  > which are numbers (of the same or different types) and may be DBNulls.
                  >
                  > In VB, the following code is correct:
                  > Public Shared Function XYZ(ByVal x, ByVal y)
                  > XYZ = x + y
                  > End Function
                  >
                  > In C#, the identical code is wrong:
                  > public static object XYZ(object x, object y) {
                  > object z = x + y; //cannot add objects
                  > return z;
                  > }
                  >
                  > Should I write the different procedures in C# for the different data types
                  > or is there a better way?
                  >
                  > Thanks.
                  >
                  >[/color]

                  Comment

                  Working...