TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET ParamArray (Use varargs Functions)

Use the ParamArray keyword to accept a variable number of arguments to a method.
ParamArray. This keyword allows a method to be called with an array of values that are specified directly (not in an explicit array). The values are placed into an array by the compiler.Array
For optimal performance, adding many overloaded methods is usually best. But ParamArray can be used in VB.NET programs where performance is not that important.
An example program. Let us examine a simple program that uses ParamArray. Notice how the ParamArray keyword comes first, then the array name with parentheses attached to it.

Here: PrintValues receives a ParamArray. We can call it with zero, one or many arguments.

Warning: For performance, avoiding ParamArray is often a better solution. An array is created for each call to a ParamArray method.

VB.NET program that uses ParamArray argument Module Module1 Sub PrintValues(ParamArray values() As Integer) ' Display Length of array. Console.WriteLine("Count: " + values.Length.ToString()) ' Loop over ParamArray values. For Each value As Integer In values Console.WriteLine(value) Next End Sub Sub Main() ' Use ParamArray method. PrintValues() PrintValues(1) PrintValues(10, 20, 30) End Sub End Module Output Count: 0 Count: 1 1 Count: 3 10 20 30
Some notes. Performance is not everything. For simplicity, using ParamArray for a varargs Function is a good plan. Often the array overhead will not matter.Function
A review. ParamArray is the same construct as "params" in C#. Other languages have similar constructs, sometimes referred to as varargs for "variadic" or "variable" argument lists.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf