To become a VB.net programmer from a Java shop

1. To become a VB.net programmer from a Java shop

I'm basically a Java shop, but I decided to write a program on VB.net for a certain project. I thought VB.net was Bimyo, but I thought that it wasn't much different from Java, so I wrote an article about the difference as a memorandum. It is a so-called poem. If you have the opportunity to become a VB.net programmer from a Java shop, I hope it helps a little.

2. Differences between Java and VB.net

I would like to explain the difference from VB.net from the perspective of a Java shop. If you want to see the code from the beginning, please refer to [3. Sample](# 3-Sample).

2.1. VB.net also has a Class

VB.net is an object-oriented language because it is part of the .Net Framework. People from VB6 seem to use it less often, but you can also define Class in VB.net. When using it, new is used to create an object.

2.2. Naming conventions (methods, properties) are LowerCamelCase for Java and UpperCamelCase for VB.net

Both are based on CamelCase, but VB.net is also based on ʻUpperCamelCasefor methods and properties. From a Java shop, it's hard to tell the difference fromClass` at first glance, but that's what it is.

2.3. Java package is Namespace

VB.net also has the concept of packaging. Enclose it in a block called Namespace. Java packages are tied to the directory structure, but VB.net's Namespace has nothing to do with the directory structure.

Also, if you use names with Class name and Namespace name, an error will occur in the build. In Java, the package is lowercase and the Class name is ʻUpperCamelCase`, so it can be judged, but I think that VB.net cannot be judged by the same naming convention. (maybe)

2.4. Java imports are Imports

Java's ʻimport specifies Class (excluding static imports), while VB.net's ʻImports specifies Namespace. It feels strange because Java does not specify a package for ʻimport. (. * Can be seen visually) Even in VB.net, when setting an alias, specify the end Class`, but in this case, the description is different, so you can see it visually.

2.5. Methods with a return value of void are Sub

In VB.net, the method of defining a method differs depending on the presence or absence of a return value. If there is no return value, that is, if it is void in Java, it is defined as Sub (subroutine).

2.6. The method for which the return value exists is Function

If there is a return value, VB.net defines it as a Function. If you want to change the presence or absence of the return value, VB.net is a bit of a mess.

2.7. Java static is Shared

When implementing a little utility function in Java, I think that it is usually a static method. In VB.net this is not static but Shared.

2.8. Java annotations are VB.net attributes

VB.net also has a function equivalent to Java annotation. That is the attribute. Input checking attributes similar to Java's Bean Validation are provided as a basic function. For more information, see Microsoft Page.

2.9. VB.net also has generics (, type parameters)

I think that generics (, type parameters) are a common feature in Java these days. Of course, VB.net also has this feature as a language specification.

3. Sample

As a sample, I would like to realize the same thing as input checking using Java Bean Validation with VB.net. Since I set it to Sub with no return value, the above-mentioned Function does not appear, but I tried to pack everything else. As with Bean Validation, input checking using attributes is simple, easy and convenient.

User.vb


Imports System.ComponentModel.DataAnnotations

Namespace Com.Example.Demo
    Public Class User
        <Display(Name:="User ID")>  _
        <Required(ErrorMessage := "{0}Is a required entry.")> _
        Public Property UserId As String
        
        <Display(Name:="name")> _
        <Required(ErrorMessage := "{0}Is a required entry.")> _
        Public Property Name As String

        <Display(Name:="age")> _
        <StringLength(3, MinimumLength :=1 ,ErrorMessage := "{0}Is{2}More than a digit{1}Please enter within digits.")> _
        Public Property Age As String
    End Class
End Namespace

ValidateService.vb


Imports System.ComponentModel.DataAnnotations
Imports System.Collections.Generic

Namespace Com.Example.Demo
    Public Class ValidateService
        Public Shared Sub Validate(Of T)(target As T)
            Dim validationResults As New List(Of ValidationResult)
            Dim vc As New ValidationContext(target, Nothing, Nothing)
            Dim isValid As Boolean = Validator.TryValidateObject(target, vc, validationResults, true)
            If validationResults.Count > 0 Then
                Console.WriteLine("Input value check[NG]")
                For Each err As ValidationResult IN validationResults
                    Console.WriteLine("{0} : {1}", err.MemberNames(0), err.ErrorMessage)
                Next
            Else
                Console.WriteLine("Input value check[OK]")
            End If
        End Sub
    End Class
End Namespace

App.vb


Namespace Com.Example.Demo
    Public Module App
        Public Sub Main()
            Console.WriteLine("// ------- Validate 1 -------")
            'Input data
            Dim input01 As new User()
            'Input value check
            ValidateService.Validate(Of User)(input01)

            Console.WriteLine("// ------- Validate 2 -------")
            'Input data
            Dim input02 As new User()
            input02.UserId = "123456"
            input02.Name = "TARO TOYOSU"
            input02.Age = "1234"
            'Input value check
            ValidateService.Validate(Of User)(input02)

            Console.WriteLine("// ------- Validate 3 -------")
            'Input data
            Dim input03 As new User()
            input03.UserId = "123456"
            input03.Name = "TARO TOYOSU"
            input03.Age = "123"
            'Input value check
            ValidateService.Validate(Of User)(input03)

        End Sub
    End Module
End Namespace

Java compiles with javac, while VB.net compiles with vbc. Of course, just as Java has build tools like Maven and Ant, VB.net also has a build tool called MsBuild.

build.bat


@echo off
set DOTNET_HOME=C:\Windows\Microsoft.NET\Framework64\v4.0.30319
set path=%path%;%DOTNET_HOME%
vbc /out:DemoApp.exe /r:System.ComponentModel.DataAnnotations.dll ^
/r:System.ServiceModel.Web.dll ^
App.vb User.vb ValidateService.vb

Modify the environment variable of DOTNET_HOME according to your own environment. If you store all the files in the same directory and execute build.bat, DemoApp.exe will be created.

Execution result


C:\demo>build.bat
Microsoft (R) Visual Basic Compiler version 14.7.2558
for Visual Basic 2012
Copyright (c) Microsoft Corporation.  All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to Visual Basic 2012, which is no longer the lates
t version. For compilers that support newer versions of the Visual Basic programming language, see http://go.microsoft.com/fwlink/?LinkID=533241


C:\demo>DemoApp.exe
// ------- Validate 1 -------
Input value check[NG]
UserId :User ID is required.
Name :The name is required.
// ------- Validate 2 -------
Input value check[NG]
Age :Please enter the age between 1 and 3 digits.
// ------- Validate 3 -------
Input value check[OK]

C:\demo>

4. Finally

This time, I explained the difference (part) between Java and VB.net from the perspective of a Java shop. Since VB.net is part of the .Net Framework, an object-oriented language, you'll find that it's not much different from Java, the same object-oriented language. C # is even closer to Java, but (unfortunately there are still many projects that love VB ** so I hope it helps you when you decide to use VB.net.

Recommended Posts

To become a VB.net programmer from a Java shop
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Connect to Aurora (MySQL) from a Java application
From Java to Ruby !!
How to jump from Eclipse Java to a SQL file
[Java] How to erase a specific character from a character string
Migration from Cobol to JAVA
New features from Java7 to Java8
Connect from Java to PostgreSQL
From Ineffective Java to Effective Java
[Java] How to convert a character string from String type to byte type
How to store a string from ArrayList to String in Java (Personal)
How to make a Java container
Run a batch file from Java
Access Teradata from a Java application
[Java] How to create a folder
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
The road from JavaScript to Java
[Java] Conversion from array to List
How to make a Java array
Introduction to Scala from a Java perspective, decompiled and understood (basic)
How to make a Java calendar Summary
Convert from java UTC time to JST time
[Introduction to Java] How to write a Java program
A program that calculates factorials from 2 to 100
How to make a Discord bot (Java)
Connect from Java to MySQL using Eclipse
Try running a Kubernetes Job from Java
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
How to print a Java Word document
[Java] Flow from source code to execution
Introduction to monitoring from Java Touching Prometheus
Precautions when migrating from VB6.0 to JAVA
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
If a person from Java learns PHP
It took a month from application to taking the Oracle Java SE 11 Silver
GetInstance () from a @Singleton class in Groovy from Java
[Java] From two Lists to one array list
Upsert from Java SDK to Azure Cosmos DB
Transform from a normal class to a lambda expression
Two ways to start a thread in Java + @
Run R from Java I want to run rJava
How to get a heapdump from a Docker container
How to display a web page in Java
Code to escape a JSON string in Java
I did Java to make (a == 1 && a == 2 && a == 3) always true
Migrate from Java to Server Side Kotlin + Spring-boot
Try to create a bulletin board in Java
I tried hitting a Java method from ABCL
Programming beginners learn PHP from a Java perspective-variables-
How to get Class from Element in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
How to convert a solidity contract to a Java contract class
A story about trying to operate JAVA File
Create Scala Seq from Java, make Scala Seq a Java List