I have a list of declared string variables that are name codes who's field values are full names. Can I get a specific variable's field value to display in a textbox if I know what the variable name is?
Example variables
Dim GLAN01 As String = "Langer Georg"
Dim BEDW01 As String = "Edwards Brian"
Dim MRIG01 As String = "Riggins Michael"
Not sure I understand the question correctly. How about this pattern? :
textBoxName.Text = variableNameToDisplay
for example :
textBox1.Text = GLAN01
to display Langer Georg text in textBox1
UPDATE :
If I understand what you are after correctly, one possible way is using dictionary to store mapping of code name to full name (instead of using multiple variables). With dictionary you can get full name string given any code name string easily, for example :
Dim dict As New Dictionary(Of String, String) From _
{{"GLAN01", "Langer Georg"},
{"BEDW01", "Edwards Brian"},
{"MRIG01", "Riggins Michael"}}
textBox1.Text = dict("GLAN01")
For reference, in case you are not familiar yet with collection initializer syntax as in above example :
MSDN : Collection Initializers (particularly "Nesting Collection Initializer" section)
Related
I am making a LIST to organize and manipulate arrays that represent lines off a spreadsheet. I've created a custom class for the arrays, and will call them out as objects.
My question is, can I use a value stored in a variable as the name of the object? If so, what would the syntax look like?
dim FileName as String
FileName = 123456.csv
Public Class List_Array
public variable1 as string
public variable2 as string
public variable3 as string
public variable4 as string
End Class
dim File_Name as List_Array = NEW List_Array
This is the coding as I understand it, but I keep thinking this will only create one Object over and over again with the same name as the string variable.
If not, how can I differentiate the different objects as I call them? There will be thousands of objects to reference, so using an unnamed object will not work so well.
If you want to store a list of named objects, what you need is a Dictionary. Dictionary objects store a list of key/value pairs. In this case, the "key" is the name that you want to assign to the object, and the value is the reference to the object itself. It's a generic class, which means when you use the Dictionary type, you must specify the types that you want it to use for it's keys and values. For instance Dictionary(Of String, MyClass) will create a list that uses String objects for its keys and MyClass objects for its values.
Here's an example of how you could use a dictionary to store a list of people with their ages:
Dim d As New Dictionary(Of String, Integer)()
d("Bob") = 30
d("Mary") = 42
Then, when you want to read a value, you can do it like this:
Dim age As Integer = d("Bob")
The Dictionary will only allow one item per key. It uses a hash table to index the values by their keys, so it's very fast get any item by its key.
Edit
Based on your comments below, here's a more pertinent example to show what I mean. Let's say you have a CSV file containing a list of people. So you create a class that stores all of the information about one person (one line of the CSV file), like this:
Public Class Person
Public Property Id As String
Public Property Name As String
Public Property Title As String
End Class
Then, you create a method which parses a single line from the CSV file and returns all of the fields from that line as an array of strings, like this:
Public Function ParseCsvLine(line As String) As String()
' ...
End Function
Then, you could load all of the people into a list, like this:
Dim persons As New List(Of Person)()
For Each line As String In File.ReadAllLines("People.csv")
Dim fields() As String = ParseCsvLine(line)
Dim person As New Person()
person.Id = fields(0)
person.Name = fields(1)
person.Title = fields(2)
persons.Add(person)
Next
Now you have all of the Person objects loaded into one list. The problem is, though, that the list is not indexed. If, for instance, you needed to find the person with an ID of 100, you'd need to loop through all of the Person objects in the list until you found one with that value in it's Id property. If you want to index them by ID so that you can find them more easily/quickly, you can use a Dictionary, like this:
Dim persons As New Dictionary(Of String, Person)()
For Each line As String In File.ReadAllLines("People.csv")
Dim fields() As String = ParseCsvLine(line)
Dim person As New Person()
person.Id = fields(0)
person.Name = fields(1)
person.Title = fields(2)
persons(person.Id) = person
Next
Then, when you need to get a person from the dictionary, you can easily access it by ID, like this:
Dim person100 As Person = persons("100")
I have a ComboBox which I assign to a variable:
Dim var as String = ComboBox1.SelectedValue
Dim name As String = var.Split(",")
This gives me the error
Value of type '1-dimensional array of string' cannot be converted to String
Any ideas as to where I'm going wrong?
Split returns an array of strings. Your variable needs to be changed to an array, not just a single string.
My VB is a bit rusty, but I think you have to make name an array:
Dim name() As String = var.Split(",")
name needs to be declared as an array.
dim name() as string = var.split(",")
The split() method will break up the string based on the given character and put each newly created string into an array and return it.
This is what your error message is telling you:
Value of type '1-dimensional array of string' cannot be converted to String
The method returns an array of string, but your trying to put it into just a string!
EDIT: In response to your answer...
So far you've managed to split the string yourself with the split method. To output this to your message box, you need to concatenate the two elements in the proper order:
msgbox(name(1) & " " & name(0))
Notice I indexed the array twice! Element 1 is the first name, element 0 is the last name. Remember you got this name in lname,fname format. Passing the array itself doesn't make sense! Remember, a datatype is not equal to an array of that type, they are two different things. Therefore a string is not compatible with a string array. However, each individual element of the array is a string, and so each of those are compatible with the string type (because they're the same thing)!
Dim var As String = ComboBox1.SelectedValue
Dim temp() As String = Split(var, ",", -1, CompareMethod.Binary)
Dim name As String = temp(0)
Or maybe "name" isn't an array and the goal is to populate "name" with everything up until the first comma, in which case the fix would be:
Dim name as String = var.Split(",")(0)
Note: assumes that var is not Nothing.
This is probably a dumb question but I have to call one of 30 or so global variables by constructing the variable name to be called from another information. However when i do that it treats it as a string. Any ideas on how to make this work?
eg something like this:
Public gsNewYork As String
public sub Getinfo
dim lslocation as string
dim a as string = "New"
dim b as string = "York"
lslocation = "gs" + a + b
lblLabel.text = lslocation
Any ideas on how to construct the variable name and have it identified as the variable name?
The most common solution to this problem is to store your variables in a Dictionary, where the strings you build are the key.
I have a user-defined structure in a list that I am trying to change the value for in an individual element within the list of structures. Accessing the element is not a problem. However, when I try to update the value, the compiler complains:
"Expression is a value and therefore cannot be the target of the
assignment"
For example:
Public Structure Person
Dim first as String
Dim last as String
Dim age as Integer
End Structure
_
Public Sub ListTest()
Dim newPerson as Person
Dim records as List (Of Person)
records = new List (Of Person)
person.first = "Yogi"
person.last = "bear"
person.age = 35
records.Add(person)
records(0).first = "Papa" ' <<== Causes the error
End Sub
As the other comments said, when you refer to records(0), you get a copy of the struct since it is a value type. What you can do (if you can't change it to a Class) is something like this:
Dim p As Person = records(0)
p.first = "Papa"
records(0) = p
Although, I think it's just easier to use a Class.
There are actually two important concepts to remember here.
One is that, as Hans and Chris have pointed out, Structure Person declares a value type of which copies are passed between method calls.
You can still access (i.e., get and set) the members of a value type, though. After all, this works:
Dim people(0) As Person
people(0).first = "Yogi"
people(0).last = "Bear"
people(0).age = 35
So the other important point to realize is that records(0) accesses the List(Of Person) class's special Item property, which is a sugary wrapper around two method calls (a getter and setter). It is not a direct array access; if it were (i.e., if records were an array), your original code would actually have worked.
I had the same problem, and I fixed it by adding a simple Sub to the structure that changes the value of the property.
Public Structure Person
Dim first as String
Dim last as String
Dim age as Integer
Public Sub ChangeFirst(value as String)
me.first = value
End Sub
End Structure
I am refactoring some legacy code. The app was not using querystrings. The previous developer was hard coding some variables that the app uses in other places.
Like this using VB.NET
so.Cpage = "ContractChange.aspx"
My question is can I programatically set this value and include the current querystring?
I want so.Cpage to be something like ContractChange.aspx?d=1&b=2
Can I do this with the request object or something? Note, I don't need the domain.
To get the current query string you would simply do something like the following:
Dim query as String = Request.QueryString("d")
This will assign the value of the "d" querystring to the string variable "query". Note that all query string values are strings, so if you're passing numbers around, you'll need to "cast" or convert those string values to numerics (be careful of exceptions when casting, though). For example:
Dim query as String = Request.QueryString("d")
Dim iquery as Integer = CType(query, Integer)
The QueryString property of the Request object is a collection of name/value key pairs. Specifically, it's of type System.Collections.Specialized.NameValueCollection, and you can iterate through each of the name/value pairs as so:
Dim coll As System.Collections.Specialized.NameValueCollection = Request.QueryString
Dim value As String
For Each key As String In coll.AllKeys
value = coll(key)
Next
Using either of these mechanisms (or something very similar) should enable you to construct a string variable which contains the full url (page and querystrings) that you wish to navigate to.
Try this:
so.Cpage = "ContractChange.aspx?" & Request.RawUrl.Split("?")(1)
In VB.Net you can do it with the following.
Dim id As String = Request.Params("RequestId")
If you want to process this in as an integer, you can do the following:
Dim id As Integer
If Integer.TryParse(Request.Params("RequestId"), id) Then
DoProcessingStuff()
End If
try this
Dim name As String = System.IO.Path.GetFileName(Request.ServerVariables("SCRIPT_NAME"))
Dim qrystring As String = Request.ServerVariables("QUERY_STRING")
Dim fullname As String = name & "/" & qrystring
Not sure about the syntax in VB.NET but in C# you would just need to do
StringId = Request.QueryString.Get("d");
Hope this helps.