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.
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).
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.
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 from
Class` at first glance, but that's what it is.
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)
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.
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).
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.
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
.
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.
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.
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>
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