Vb.net – Size of array in Visual Basic arraysvb.net I've tried this code in VB: Dim a(1) As Byte Console.WriteLine(a.Length) The output is "2". Anyone any idea why? Best Answer If you are used to C/C++/C# languages you are used that when declaring an array to initialize it with the number of elements in the array. C# : byte a[] = new byte[1] will declare a byte array with 1 element (upperBound = 0) The behavior is different in VB where, when declaring an array the parameter used in initialization represents the UpperBound of the array. VB.NET: Dim a(1) As Byte will declare a byte array with 2 elements (upperBound = 1) Related SolutionsJava – Create ArrayList from array new ArrayList<>(Arrays.asList(array)); Javascript – How to check if an array includes a value in JavaScript Modern browsers have Array#includes, which does exactly that and is widely supported by everyone except IE: console.log(['joe', 'jane', 'mary'].includes('jane')); //true You can also use Array#indexOf, which is less direct, but doesn't require polyfills for outdated browsers. console.log(['joe', 'jane', 'mary'].indexOf('jane') >= 0); //true Many frameworks also offer similar methods: jQuery: $.inArray(value, array, [fromIndex]) Underscore.js: _.contains(array, value) (also aliased as _.include and _.includes) Dojo Toolkit: dojo.indexOf(array, value, [fromIndex, findLast]) Prototype: array.indexOf(value) MooTools: array.indexOf(value) MochiKit: findValue(array, value) MS Ajax: array.indexOf(value) Ext: Ext.Array.contains(array, value) Lodash: _.includes(array, value, [from]) (is _.contains prior 4.0.0) Ramda: R.includes(value, array) Notice that some frameworks implement this as a function, while others add the function to the array prototype. Related TopicJavascript – How to append something to an arrayJavascript – How to insert an item into an array at a specific index (JavaScript)Javascript – Sort array of objects by string property valueJavascript – Short circuit Array.forEach like calling breakJavascript – Loop through an array in JavaScriptJavascript – How to check if an object is an array?Javascript – How to remove a specific item from an arrayJavascript – For-each over an array in JavaScript
Best Answer
If you are used to C/C++/C# languages you are used that when declaring an array to initialize it with the number of elements in the array.
will declare a byte array with 1 element (upperBound = 0)
The behavior is different in VB where, when declaring an array the parameter used in initialization represents the UpperBound of the array.
will declare a byte array with 2 elements (upperBound = 1)