Converting VB to Java - 'type' convert for record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jack25
    New Member
    • Jun 2013
    • 2

    Converting VB to Java - 'type' convert for record

    I have zero Java knowledge, what would be the equivalent of the following in java?

    Code:
    Public Type hdrmail1rec                             'Output rec header 1
        hdrcust As String * 20
        filler1 As String * 3
        hdrpo As String * 20
        filler2 As String * 3
        hdrdate As String * 23
        filler3 As String * 3
        hdrstat As String * 40
    End Type
    Last edited by Rabbit; Jun 4 '13, 05:23 AM. Reason: Please use code tags when posting code.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi Jack and welcome to bytes.com!

    Java doesn't have an own type for records and Strings aren't fixed length either. The closest to the above would be a Bean with String members or, if you insist on fixed lengths, char arrays. A bean is a very basic Java class with private variables as well as getter and setter functions for those variables. Of course, you could enforce a certain length in those setter functions.
    Here's a truncated example:
    Code:
    public class hdrmail1rec {
       private String hdrcust;
       // ...
    
       public String getHdrcust() {
          return hdrcust;
       }
    
       public void setHdrcust(String hdrcust) {
          if(hdrcust.length() > 20) {
             this.hdrcust = hdrcust.substring(0,21);
          } else {
             this.hdrcust = hdrcust;
          }
       }
       // ...
    }
    In theory, you could make the String variables public and do without the getters and setters; this is however considered bad practice due to security reasons (as any class could then access the variables without you having any control over it). Also, many libraries and frameworks follow the convention of having getters and setters.

    Comment

    • jack25
      New Member
      • Jun 2013
      • 2

      #3
      thanks for the info nepomuk! I am stumbling through converting a program to Java, kind of as a learning experience and it's also something that would help out at work.

      I appreciate the info!

      -jack25

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        You're welcome. Java uses a few concepts that are probably quite different from VB (though just guessing, as I never seriously developed in VB myself) so just converting it probably won't be the best method if efficiency is essential. As a learning experience of course, it can be very useful.

        Comment

        Working...