Storing strings in arrays

TechGuyChris

New member
Joined
May 21, 2013
Messages
2
Programming Experience
Beginner
Hi, i'm a beginner programmer. I just learned about arrays and I have a question related to something i'm trying to do because the book I have does not cover how to do this.
What I am attempting to do is build an application just for fun that will store what you type into a textbox into an array element.

The problem is that the book only covered examples on how to store values into an element by intilializing them explicitily in the code. I have included two sample examples of what the book has done. (I just renamed the string values)

Example1: Dim CustomerName() as String = {"Chris", "Beyonce", "JustinBieber", "AvrilLavigne"}

Example2: Dim CustomerName(4) as String

CustomerName(0)="Beyonce"
CustomerName(1)="Chris"

I want to store a name that you enter into a textbox into the array. How do do you do this? BTW i dont intend on publishing this app. Its just for play to test out VB stuff.

TechGuyChris
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
Your second example shows how to assign a string to an array element. In that case it is a literal string value, but it could also be the string value returned by the TextBox controls Text property. ex:
CustomerName(1) = InputTextBox.Text

I understand you're at the absolute start of your learning, but arrays aren't used like that these days. Since their size can't be changed they are most often used for fixed number of elements. When you have an dynamic number of elements to assign you would use a collection/list. ex:
Private customers As New List(Of String)

customers.Add(InputTextBox.Text)
 

AceInfinity

Active member
Joined
Dec 1, 2012
Messages
32
Programming Experience
10+
By MSDN standards, you shouldn't be declaring arrays like that with () around the variable identifier, instead of the type.
VB.NET:
Dim CustomerName() as String = {"Chris", "Beyonce", "JustinBieber", "AvrilLavigne"}

You should be doing String() instead of CustomerName().
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
By MSDN standards, you shouldn't be declaring arrays like that with () around the variable identifier, instead of the type.
VB.NET:
Dim CustomerName() as String = {"Chris", "Beyonce", "JustinBieber", "AvrilLavigne"}

You should be doing String() instead of CustomerName().
It would make more sense to use Option Infer and don't specify the type.
Also, that notation style can't be used if you want to specify array bounds.
 

AceInfinity

Active member
Joined
Dec 1, 2012
Messages
32
Programming Experience
10+
It would make more sense to use Option Infer and don't specify the type.
Also, that notation style can't be used if you want to specify array bounds.

It would make more sense to use Option Infer and don't specify the type.

I'm confused... Usually being Explicit is much better than being Implicit.

Also:
VB.NET:
Dim x As Integer() = New Integer(4) {}

Scroll down the the Arrays portion: Visual Basic Coding Conventions

Cheers,
~Ace
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
AceInfinity said:
I'm confused... Usually being Explicit is much better than being Implicit.
Maybe we put different things into explicit/implicit. Option Infer lets compiler assign the type to the variable based on the assigned result, it's not like there is an implicit conversion going on or that you loose any type information, the variable is strongly typed and the type is the same as if you would declare it. By explicly declaring it you are in effect only wasting time and overheating your keyboard. By coding the type you are also adding lots of "characters" to the code, which can make it harder to read, it is called "code clutter" and is superfluous information. Such explicitness can also lead to bad coding practices like using one code line to declare the variable and type, and another code line to assign a value to it, sometimes that can even lead to scope problems. By pursuing Option Infer you would more than often declare the variable when it is needed. Option Infer is also very useful when dealing with Linq/lambdas/delegates/generics where type information may get extremely complex, and it is required for working with anonymous types in Linq.
 

AceInfinity

Active member
Joined
Dec 1, 2012
Messages
32
Programming Experience
10+
Yes :) I realize that, but Explicit and Infer are already on by default. What I meant was that it's usually better to be explicit with your code though; readability. For obvious things like LINQ, you can probably leave out the fact that it's an IEnumerable result or IGrouping for instance, but it should not be a reason to avoid proper array declaration.

Such explicitness can also lead to bad coding practices like using one code line to declare the variable and type, and another code line to assign a value to it, sometimes that can even lead to scope problems.

I come from C# and C++ as well so that's a habit for code maintainability. :)

Option Infer is also very useful when dealing with Linq/lambdas/delegates/generics where type information may get extremely complex, and it is required for working with anonymous types in Linq.

I agree, but for the purposes outlined in this thread, I think it should be explicitly defined in the code to keep things readable for the programmers intentions. The availability of type inference, in my opinion, doesn't mean that we should start using it for everything. That's just the way I see it. This is a string array, not an IEnumerable(Of IGrouping(Of .....

cheers.gif
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
Also:
VB.NET:
Dim x As Integer() = New Integer(4) {}

Scroll down the the Arrays portion: Visual Basic Coding Conventions

Cheers,
~Ace
The basic notation is clear:
Put the array designator on the type, not on the variable
but it is also something that has changed recently, for example in 2005 and 2008 version of that article it says:
Put the array designator on the variable, not on the type

As for your example, doesn't that remind more of this:
don't said:
Dim letters2() As String = New String() {"a", "b", "c"}
do said:
Dim letters4 As String() = {"a", "b", "c"}
There is also an error in that article:
don't said:
Dim letters3() As String = {"a", "b", "c"}
do said:
Dim letters5() As String = {"a", "b", "c"}
 

AceInfinity

Active member
Joined
Dec 1, 2012
Messages
32
Programming Experience
10+
The basic notation is clear:

but it is also something that has changed recently, for example in 2005 and 2008 version of that article it says:


As for your example, doesn't that remind more of this:


There is also an error in that article:

Lots of things have changed since 2005 and 2008 though. Why stick to those old standards for the current world in VB.net? The reason they have changed and are suggesting new practices is because I would assume like most VB6 habits, these are considered bad practice to be using in VB.net, for the reason that they may not be guaranteed to be supported in newer VB.net standards. Thus you should stick with newer coding conventions, even if they deviate from the older ones.

As for your example, doesn't that remind more of this:

It's not though:
- I don't have the parenthesis around my variable.

And using the initializer like in their "do":
VB.NET:
Dim letters4 As String() = {"a", "b", "c"}

This assumes that we can immediately set our range of elements. I'm only wanting to set the size of the array for future use.

edit: Above, he was doing all of what not that page says to not do:
VB.NET:
Dim CustomerName() as String = {"Chris", "Beyonce", "JustinBieber", "AvrilLavigne"}
VB.NET:
Dim CustomerName(4) as String
CustomerName(0)="Beyonce"
CustomerName(1)="Chris"
 
Last edited:
Top Bottom