What do YOU name a List object that holds every letter in a String - naming-conventions

I'm trying to get better at naming my variables and methods and this situation keeps coming up.
Take for example this code:
String word = "Hello";
List<String> wordList = new ArrayList<>();
The List I created is simply going to contain every letter inside the word variable, but by simply appending "-List" to the word, this could lead to confusing code.
I would simply like to know what you'd name the List Object instead of "wordList". Or maybe you would use "wordList"?
A broader question would be something like:
What would you name an object that was formatted into an object of a different class but still held the same "information" as the original object?

Related

How to dynamically pass a variable (of string type) value to another variable name in vb.net?

I am a former VFP programmer and was amazed by some powerful technics of VFP such as declaring dynamically a variable and assign to it a name from another string variable. I am looking for how to do the same in vb.net. I search but most solutions suggest array or list where I could not use the specific meaningful name of the variables.
I have a list of many variables in a table and for each variable I would like to dynamically declare a variable that have the name of the variable and assign to to it the variable value. Below is just 5 % of the full list
partial list of the variables
I can declare all the variable one by one but I would prefer a shorter way if any.
How could you assist me?
I have not tried anything.
You can't dynamically create variables in a strongly typed language. (You could create a dynamically typed variable, but that's not what you're looking for.)
Take a look at the Dictionary class, which is a collection of pairs (name and value), like a classic "hash array" or an object in JavaScript.
Edit. When you create a dictionary in VB.Net, you specify the datatype of the key (usually a String) and the datatype of the value. If all your values are Integers, yo do something like: Dim myDict As New Dictionary (Of String, Integer). If you really need to store different classes of objects (using the same dictionary), you may do: Dim myDict As New Dictionary (Of String, Object) (but you'll lose type safety).

Change selected ComboBox item, from text file string not working

Ok.
So I am working on a project, irrelevant, and I have a bunch (8) of ComboBoxes (they are in DropDownList mode) and there is 8 save files. I have them being imported and converted to strings:
Using class2 As New StreamReader(path & "SaveData/classdata/classdata2.NIC")
Dim fdcount1 As String
fdcount = class2.ReadToEnd()
MessageBox.Show(fdcount1)
hr2choice.SelectedItem = fdcount1
End Using
I already tested this, and it seems to be working.
(Test code I used:)
MessageBox.Show(fdcount1)
and it showed the value ("DiVita")
Despite this, when I tried setting the ComboBox value to this, it did not seem to work.
The ComboBox does have this value in it, and if I try this, it works:
hr2choice.SelectedItem = "DiVita"
For whatever reasons though, it does not work when I try doing it directly from the string.
Thanks for any help with this!
Nic
To answer this, I have to assume that the data in the text file is formatted as one line for each piece of data.
There seems to be a couple of issues with your code. fdcount is just declared as a string where it should be an array to make it easier to access each line that is read from the file. fdcount1 has no relationship to fdcount - it is a completely separate entity, so the data in fdcount1 is coming from somewhere else.
Rather than the above code, It's easier to use this
Dim fdcount() As String
fdcount = File.ReadAllLines("SaveData/classdata/classdata2.NIC")
MessageBox.Show(fdcount(1))
Note that fdcount is declared as an Array of String. The 2nd line does all the opening, reading into the array, and closing of the file.
You can then access each element of the array as shown in the 3rd line.

VBA - Is there a way to get a Constant value from a variable String?

I have created few Constants (VBA identifiers declared with the key word Const) that look like Rg_Euro, Rg_Usd, Rg_Cad, ... to define specific regions into my workbook.
As I have these "extensions" (Euro, Usd, Cad, ...) in the DataBase that I'm working with, I tried to get the values of my constant by creating a string like this : Str = "Rg_" & extension(i)
But I can't seem to find a workaround to call the Constant and get its value from it... I'm googled it but didn't found what I was looking for and I'm starting to think that it might not be possible directly...
I thought of a User Defined Function with a Select Case on the String to return the right value, but it is just going to add another function, so I'm looking for a more direct solution if there is one!
I'm not a pro and my answer it's only what I've done to solve a similar problem, anyway, I hope it helps:
You can add controls (for example Textbox) named as your constants and set the value you need, then you'll be able to catch any value with this:
Me.Controls("RG_" & extension(i)).text

When declaring an object, how to use variable as the name?

Can I use the value inside of a variable to name an object? If so, what is the syntax for that declaration?
Every object has to be given a distinct name. Dim XXXX as NEW_ARRAY is named XXXX. Since I will have 10,000 objects, I would like to automate the creation of those objects using a loop. But, if the object creation loop uses the same name over and over again, I understand that the object would overwrite itself 9999 times. There would only be one instance of that object.
I would like to use the value of a variable as that distinct name. However, I think that typing in a name of a variable in the name position while declaring the object would only overwrite the first object over and over again.
Is there a specific syntax that puts the VALUE of a previously declared variable as the name of an object?
So, XXXX= 1111
Dim "XXXX" as NEW_ARRAY would be named 1111
Then XXXX=2222
Dim "XXXX" as NEW_ARRAY would be named 2222
Then XXXX=3333
Dim "XXXX" as NEW_ARRAY would be named 3333.
Object doesn't have name, variable has. #StevenDoggart already elaborate about that for you.
If the question was "Can I use the value inside of a variable to name a new variable?" Short answer is "no, you can't". There is no point to have such specific feature in .NET.
You can achieve similar behavior using dictionary, as suggested in many posts asking about dynamic variable name in .NET. You can see dictionary key as variable name, and dictionary value as variable value. As far as I can see, what you can do given such dynamic variable name feature exists in .NET, can be done equally well using dictionary.

Array in VB .Net

I have an array Newstr(20) When I fill it, I need to know how many of its indexes has been filled ? and find out the values.
I need to know how many of its indexes has been filled ?
Arrays don't keep that information. They only know how many spots you allocated. You have to track how many you assigned yourself. More than that, if you're working with a collection where you don't know how many items there will be, arrays are really the wrong choice in the first place. You should use a List(Of T) instead.
You could populate the array with a known string, then test for that string to see how many elements in your array are filled.
I would - however - suggest using an array list. You can get the number of elements added to the list from the Count property. This is the MSDN entry for Array Lists.
In order to find which of the elements have been filled, you can use a LINQ construction like this:
Dim input() = New String() {"abc", "def", "ghi", "", Nothing}
Dim output = input.Where(Function(i) Not String.IsNullOrEmpty(i)).ToArray
When you run this code, the output array will contain "abc", "def" and "ghi".
You can modify the selector of the Where to suit your preference if you're coding for a different type of array.
For instance the selector for Integer? will be:
input.Where(Function(i) (Not i Is Nothing) Or (i <> 0)).ToArray
Of course, you'll have to be coding in .NET 3.5+ in order to get access to LINQ.