C# to VB translation problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

    C# to VB translation problem

    I have a little problem I'm not clear on. I program mostly in VB, I am
    somewhat familiar with C#, and I've been told to translate a program. I was
    under the general impression that since C# and VB are both Dot Net languages,
    it was more or less possible to directly translate everything, aside from a
    few individual language quirks.

    In the original C# I have the following:
    foreach (AccountInfo item in Accounts)
    {
    if (item.LastChang edBy != null)
    {
    ... do some stuff here.
    }
    }

    where LastChangedBy is an integer property of the object AccountInfo

    My freeware translator has rendered this in VB as:
    For Each item As AccountInfo In Accounts
    If item.LastChange dBy IsNot Nothing Then

    VS does not like this and gives an error: 'IsNot' requires operands that
    have reference types, but this operand has the value type 'Integer'.

    How would you translate this?
  • Chris Dunaway

    #2
    Re: C# to VB translation problem

    On Aug 22, 8:18 am, B. Chernick <BChern...@disc ussions.microso ft.com>
    wrote:
    I have a little problem I'm not clear on. I program mostly in VB, I am
    somewhat familiar with C#, and I've been told to translate a program. I was
    under the general impression that since C# and VB are both Dot Net languages,
    it was more or less possible to directly translate everything, aside from a
    few individual language quirks.
    >
    In the original C# I have the following:
    foreach (AccountInfo item in Accounts)
    {
    if (item.LastChang edBy != null)
    {
    ... do some stuff here.
    }
    >
    }
    >
    where LastChangedBy is an integer property of the object AccountInfo
    >
    My freeware translator has rendered this in VB as:
    For Each item As AccountInfo In Accounts
    If item.LastChange dBy IsNot Nothing Then
    >
    VS does not like this and gives an error: 'IsNot' requires operands that
    have reference types, but this operand has the value type 'Integer'.
    >
    How would you translate this?
    Can you verify the type of LastChangedBy? Even in C# you can't
    compare an integer to null. You would get a similar compile error in
    C# as well. LastChangedBy must be something other than integer.

    Chris

    Comment

    • Martin Honnen

      #3
      Re: C# to VB translation problem

      B. Chernick wrote:
      I have a little problem I'm not clear on. I program mostly in VB, I am
      somewhat familiar with C#, and I've been told to translate a program. I was
      under the general impression that since C# and VB are both Dot Net languages,
      it was more or less possible to directly translate everything, aside from a
      few individual language quirks.
      >
      In the original C# I have the following:
      foreach (AccountInfo item in Accounts)
      {
      if (item.LastChang edBy != null)
      {
      ... do some stuff here.
      }
      }
      >
      where LastChangedBy is an integer property of the object AccountInfo
      >
      My freeware translator has rendered this in VB as:
      For Each item As AccountInfo In Accounts
      If item.LastChange dBy IsNot Nothing Then
      >
      VS does not like this and gives an error: 'IsNot' requires operands that
      have reference types, but this operand has the value type 'Integer'.
      >
      How would you translate this?
      If LastChangedBy is indeed an int then item.LastChange dBy != null is
      always true so you could simply remove the if check and only include the
      block e.g.


      foreach (AccountInfo item in Accounts)
      {
      {
      ... do some stuff here.
      }
      }

      which then becomes

      For Each item As AccountInfo In Accounts
      ... do some stuff here
      Next

      --

      Martin Honnen --- MVP XML

      Comment

      • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

        #4
        RE: C# to VB translation problem

        I guess that you have in c# a nullable int delclared: int? LastChangedBy

        If so, then the property in vb should be declared as: Nullable(Of Integer)

        "B. Chernick" wrote:
        I have a little problem I'm not clear on. I program mostly in VB, I am
        somewhat familiar with C#, and I've been told to translate a program. I was
        under the general impression that since C# and VB are both Dot Net languages,
        it was more or less possible to directly translate everything, aside from a
        few individual language quirks.
        >
        In the original C# I have the following:
        foreach (AccountInfo item in Accounts)
        {
        if (item.LastChang edBy != null)
        {
        ... do some stuff here.
        }
        }
        >
        where LastChangedBy is an integer property of the object AccountInfo
        >
        My freeware translator has rendered this in VB as:
        For Each item As AccountInfo In Accounts
        If item.LastChange dBy IsNot Nothing Then
        >
        VS does not like this and gives an error: 'IsNot' requires operands that
        have reference types, but this operand has the value type 'Integer'.
        >
        How would you translate this?

        Comment

        • =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

          #5
          RE: C# to VB translation problem

          Actually no. The original local variable is declared:
          private int _lastChangedBy = 0;
          The property itself looks conventional.

          However I should explain that the overall application is written using CSLA
          2.1.4 and the programmer who wrote it just left on a long vacation without
          leaving a scrap of documentation. Then management told me to translate it
          using whatever freeware I could find. I barely knew CSLA existed before this
          week. I love this job.

          (Let me make a guess here. In the original C#, the class itself is declared
          public class AccountInfo : Csla.BusinessBa se<AccountInfo>
          Could this have something to do with the ability of C# to test for nulls
          with an integer property in this situation? But this won't translate to VB?)

          "Family Tree Mike" wrote:
          I guess that you have in c# a nullable int delclared: int? LastChangedBy
          >
          If so, then the property in vb should be declared as: Nullable(Of Integer)
          >
          "B. Chernick" wrote:
          >
          I have a little problem I'm not clear on. I program mostly in VB, I am
          somewhat familiar with C#, and I've been told to translate a program. I was
          under the general impression that since C# and VB are both Dot Net languages,
          it was more or less possible to directly translate everything, aside from a
          few individual language quirks.

          In the original C# I have the following:
          foreach (AccountInfo item in Accounts)
          {
          if (item.LastChang edBy != null)
          {
          ... do some stuff here.
          }
          }

          where LastChangedBy is an integer property of the object AccountInfo

          My freeware translator has rendered this in VB as:
          For Each item As AccountInfo In Accounts
          If item.LastChange dBy IsNot Nothing Then

          VS does not like this and gives an error: 'IsNot' requires operands that
          have reference types, but this operand has the value type 'Integer'.

          How would you translate this?

          Comment

          • Tom Shelton

            #6
            Re: C# to VB translation problem

            On 2008-08-22, B Chernick <BChernick@disc ussions.microso ft.comwrote:
            Actually no. The original local variable is declared:
            private int _lastChangedBy = 0;
            The property itself looks conventional.
            >
            However I should explain that the overall application is written using CSLA
            2.1.4 and the programmer who wrote it just left on a long vacation without
            leaving a scrap of documentation. Then management told me to translate it
            using whatever freeware I could find. I barely knew CSLA existed before this
            week. I love this job.
            >
            (Let me make a guess here. In the original C#, the class itself is declared
            public class AccountInfo : Csla.BusinessBa se<AccountInfo>
            Could this have something to do with the ability of C# to test for nulls
            with an integer property in this situation? But this won't translate to VB?)
            >
            This code, if it is really an int is going to cause issues in C# as well.
            It's a bug in the C# code. It would generate a compiler warning about the
            expression always being true because an int can never be null.

            The only way that test would work in reality is if the int was int?...
            --
            Tom Shelton

            Comment

            • Rich P

              #7
              RE: C# to VB translation problem

              Try

              For Each item As AccountInfo In Accounts
              If item.LastChange dBy <DBNull.Value Then
              ...

              Rich

              *** Sent via Developersdex http://www.developersdex.com ***

              Comment

              • =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

                #8
                Re: C# to VB translation problem

                Looks like you're right. I compiled the original C# code and got a warning
                for the same line stating that 'The result of the expression is always 'true'
                since a value of type 'int' is never equal to 'null' of type 'int?'.

                I'm going to treat this as a 'political' problem, if you know what I mean....

                "Tom Shelton" wrote:
                On 2008-08-22, B Chernick <BChernick@disc ussions.microso ft.comwrote:
                Actually no. The original local variable is declared:
                private int _lastChangedBy = 0;
                The property itself looks conventional.

                However I should explain that the overall application is written using CSLA
                2.1.4 and the programmer who wrote it just left on a long vacation without
                leaving a scrap of documentation. Then management told me to translate it
                using whatever freeware I could find. I barely knew CSLA existed before this
                week. I love this job.

                (Let me make a guess here. In the original C#, the class itself is declared
                public class AccountInfo : Csla.BusinessBa se<AccountInfo>
                Could this have something to do with the ability of C# to test for nulls
                with an integer property in this situation? But this won't translate to VB?)
                >
                This code, if it is really an int is going to cause issues in C# as well.
                It's a bug in the C# code. It would generate a compiler warning about the
                expression always being true because an int can never be null.
                >
                The only way that test would work in reality is if the int was int?...
                --
                Tom Shelton
                >

                Comment

                • xap xap

                  #9
                  Re: C# to VB translation problem


                  You could you use:
                  if item.LastChange dBy.HasValue Then
                  ... do some stuff here.
                  end if

                  *** Sent via Developersdex http://www.developersdex.com ***

                  Comment

                  Working...