Function Default value for argument in Visual Basic .NET

Find this useful? Support us: Star on GitHub 6
Category: Error handling | Language: Visual Basic .NET

Sure! In Visual Basic .NET, you can set default values for arguments in a method or function. This means that if a value is not passed for that argument when calling the method or function, it will use the default value instead. Here's an example:

Function MultiplyNumbers(ByVal num1 As Integer, Optional ByVal num2 As Integer = 1) As Integer
    Return num1 * num2
End Function

Console.WriteLine(MultiplyNumbers(5)) 'Output: 5
Console.WriteLine(MultiplyNumbers(5, 3)) 'Output: 15

In this example, the MultiplyNumbers function takes two arguments: num1 and num2. num2 is optional and has a default value of 1. When we call the function with just one argument (MultiplyNumbers(5)), the function will use the default value of 1 for num2. When we call the function with two arguments (MultiplyNumbers(5, 3)), the function will use the passed value of 3 for num2.

The Optional keyword is used to make an argument optional and the default value is set using the = operator. You can also set default values for arguments in a sub procedure in the same way.

I hope that helps!