I apologize if the question is a bit bland, but what I'm trying to do is access a list of things from a text file like "R1", "R2", "R3". Is it possible that I can have the integer in that name as a seperate variable?
So if I have...
Dim R0 As String = "Hello"
Dim R1 As String = "Goodbye"
Dim RCount As Integer = 0
MsgBox(R & RCount) 'Returns Hello
Not without creating and evaluating an Expression Tree, but you could use an array and an index into the array.
Dim R(6) as String
R(0) = "Hello"
R(1) = "Goodbye"
Dim RCount as Integer = 0
MsgBox(R(RCount))
Related
I am trying to join two substrings into one that defines an integer variable.
Example:
Dim ACTGPA as Integer
Dim ACTGPB as Integer
Dim ACPrio as Integer
ACTGPA = 5
ACTGPB = 10
Following a lot of code I have a loop that selects the value from a recordset containing a number column called either "TGP_A" or TGP_B".
Set rs4 = db.OpenRecordset("SELECT * FROM tbl_Syllabus WHERE Mis_Name = '" & strMisName & "'")
Dim item As Variant
For Each item In rs4.Fields
If item.Name = "TGP_A" Or item.Name = "TGP_B" Then
If Nz(rs4(item.Name).Value, "") > 0 Then
If strACCon1 = "" Then
strACCon1 = item.Name
End If
End If
End If
Next item
I want to set the ACPrio value to either the value of ACTGPA or ACTGPB, depending on which item name is chosen in the loop.
Using replace I remove the _ from the item name in order to define ACTGPA (or B).
I get
"Error 13, type mismatch"
on this next line. Probably because it thinks I am trying to set ACPrio (integer) to something that is a string - but I am trying to write the integer ACTGPA (or ACTGPB).
ACPrio = "AC" & Replace(strACCon1, "_", "")
Should be the same as this:
ACPrio = ACTGPA 'eg. settings the ACPrio value to that of ACTGPA (or ACTGPB), which is 5 (or 10).
You can't use a string to reference a variable (or its value) by name.
But you can add the necessary values to a collection with their name and then access them later on by name, like in this sample:
Public Sub Sample()
Dim ACTGPA As Integer
Dim ACTGPB As Integer
Dim ACPrio As Integer
ACTGPA = 5
ACTGPB = 10
' Prepare the collection by adding the values with their name:
Dim col As Collection
Set col = New Collection
col.Add 5, "ACTGPA"
col.Add 10, "ACTGPB"
Dim strACCon1 As String
' Set for testing to "TGP_A":
strACCon1 = "TGP_A"
ACPrio = col("AC" & Replace(strACCon1, "_", ""))
Debug.Print ACPrio
' Set for testing to "TGP_B":
strACCon1 = "TGP_B"
ACPrio = col("AC" & Replace(strACCon1, "_", ""))
Debug.Print ACPrio
End Sub
The output is
5
10
And maybe you can omit the variables ACTGPA and ACTGPB at all now, if it fits to your concept and overall code.
I have written below Code in SSRS Report and calling it from expression as =Code.convertCode(Parameters!ProcessingStatus.Value,"Reject,Fail")).Value , but on previewing report I am getting the error : "Number of indices is less than the number of dimensions of the indexed array"
Public Function convertCode(ParamValues As String, findString As String) As String()
Dim SrcArray() As String
Dim FndArray() As String
Dim DstArray() As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
SrcArray() = Split(ParamValues, ",")
FndArray() = Split(FindString,",")
For k = LBound(FndArray) To UBound(FndArray)
For i = LBound(SrcArray) To UBound(SrcArray)
If (InStr(SrcArray(i), FndArray(k)) > 0) Then
ReDim Preserve DstArray(j) As String
DstArray(j) = SrcArray(i)
j = j + 1
End If
Next i
Next k
arr = DstArray
End Function
At the end of your function you have
arr = DstArray
which should be
convertCode = DstArray
Not sure if that's already the problem.
Note: Option Explicit would prevent this error.
I think the problem is that you're not declaring the first dimension in your arrays.
Dim SrcArray() As String
Dim FndArray() As String
Dim DstArray() As String
Should be:
Dim SrcArray(10) As String
Dim FndArray(10) As String
Dim DstArray(10) As String
Or whatever the number of the Array you'll need.
The error message indicates that the number of indices you have (none) is less that what you need (variables I, k, j).
I have to create a program that calculates the sum of a range of numbers entered by the user and displays in a label an expression with the numbers in range. So if I entered "10" as a starting number and "20" as an ending number, there would be a label that displays "10+11+12+13+14+15+16+17+18+19+20".
This is what I have so far. I'm not sure how to get the range of numbers and display it in a label. I'm also really new to Visual Basic (I'm taking it as a course in high school) so please dumb down your answer as much as possible :) Any help is appreciated! Thanks.
Dim intStartingNumber As Integer = Val(Me.txtStartNumber.Text)
Dim intEndingNumber As Integer = Val(Me.txtEndNumber.Text)
Dim intSum As Integer = 0
Me.lblNumbers.Text = intStartingNumber & "+" & intEndingNumber
For intStartingNumber = Val(Me.txtStartNumber.Text) To intEndingNumber Step 1
intSum = intSum + intStartingNumber
Next
Me.lblNumbersSum.Text = intSum
If you just want the total:
Dim StartNumber As Integer = Integer.Parse(txtStartNumber.Text)
Dim EndNumber As Integer = Integer.Parse(txtEndNumber.Text)
lblNumbersSum.Text = Enumerable.Range(StartNumber, EndNumber - StartNumber ).Sum()
If you really want the full text expressions:
Dim StartNumber As Integer = Integer.Parse(txtStartNumber.Text)
Dim EndNumber As Integer = Integer.Parse(txtEndNumber.Text)
Dim delimiter As String = ""
Dim expression As New StringBuilder()
For Each number As String IN Enumerable.Range(StartNumber, EndNumber - StartNumber )
expression.Append(delimiter).Append(number)
delimiter = "+"
Next number
lblNumbersSum.Text = expression.ToString()
This should work, albeit I haven't been able to test:
Dim intStartingNumber As Integer = Val(Me.txtStartNumber.Text)
Dim intEndingNumber As Integer = Val(Me.txtEndNumber.Text)
Dim intSum As Integer = 0
Dim intIndex As Integer
Dim strExpr As String
strExpr = Me.txtStartNumber.Text
'Setting up a new variable called intIndex so that intStartingNumber can stay static
For intIndex = Val(Me.txtStartNumber.Text) To intEndingNumber Step 1
intSum = intSum + intIndex
if intIndex > intStartingNumber Then
strExpr = strExpr & "+" & intIndex
End If
Next
Me.lblNumbersSum.Text = intSum
Me.lblNumbers.Text = strExpr
The idea is that you create a new variable called strExpr to hold the expression and then concatenate using & within the For loop. That way, as you add on the values arithmetically, you're also adding to the string that shows the calculation being done. I'm hoping that's what you were after.
If you get any errors, please comment below and I'll amend the script and explain.
As you are doing this to learn the basics of Basic (hah hah, never heard that one before), I will keep it simple:
' convert the input text into numbers
Dim startNumber As Integer = Integer.Parse(txtStartNumber.Text)
Dim endNumber As Integer = Integer.Parse(txtEndNumber.Text)
'TODO: optional - check that endNumber > startNumber
' we are going to put the sum and the text of the summation into
' variables; we might as well start them off with the first values
Dim sum As Integer = startNumber
Dim sumText As String = startNumber.ToString()
' now we just need to use a loop that goes from the second value to the end
For i As Integer = startNumber + 1 To endNumber
' we need to use the value i twice, once as a number...
sum = sum + i
' ... and once as a String
sumText = sumText & "+" & i.ToString()
Next
' show the results to the user
lblNumbersSum.Text = sum.ToString()
lblNumbers.Text = sumText
The default Step value for a For..Next loop is 1, so we don't need to specify that.
Instead of writing sum = sum + i, we could write sum += i, and similarly for sumText = sumText & "+" & i.ToString() we could write sumText &= "+" & i.ToString(). They are just ways of saving a bit of typing.
As Jens mentioned, it is usually better to use something called a StringBuilder to build a string in a loop, but I expect you will learn about that later. If you want to learn about it now, you could look at the Remarks section of the StringBuilder documentation.
I'm trying to copy data from text file to array, I got error Index was outside the bounds of the array.
Dim vstring(-1) As String
Dim vid(-1) As String
Dim index As Integer
Dim vText As String = ""
Dim vFileName As String = "C:\Users\suman\Documents\Visual Studio 2010\Projects\Ass3_2076004\student.txt"
Dim vAvgValue As Integer
Dim vErrorMsg As String = ""
If (Txt_IdNumber.Text).Length = 5 Then
Dim rvSR As New IO.StreamReader(vFileName)
Do While rvSR.Peek <> -1
vText = rvSR.ReadLine()
vstring = vText.Split(",")
vid(index) = vstring(0)'error
index = index + 1
Loop
Dim vstring() as String
Dim vFileName As String = "C:\Users\suman\Documents\Visual Studio 2010\Projects\Ass3_2076004\student.txt"
If Txt_IdNumber.Text.Length = 5 Then
Using rvSR As New IO.StreamReader(vFileName)
vstring = rvSR.ReadLines().Select(Function(s) s.Split(","c)(0)).ToArray()
End Using
End If
First, you should probably declare vstring as an unsized array. Like this:
Dim vString() as string
Second, Since you don't know how many lines you need, declare vid as list. Like this:
Dim vid as List(of string)
Then, before you split the string, you should make sure it actually contains a comma. Like this:
Do While rvSR.Peek <> -1
vText = rvSR.ReadLine()
If vText.Contains(",") Then
vstring = vText.Split(",")
vid.add(vstring(0))
End If
Loop
'at the end, you can convert vid from a list to an array, if you want
Dim arr() as string = vid.ToArray()
I long variable in vb.net which contains the following information,
Dim g As String = "$C:\Program Files\Cavaj Java Decompiler\cavaj.exe$C:\Users\Yoosuf\AppData\Local\Google\Chrome\Application\chrome.exe$C:\Program Files\DVD Maker\dvdmaker.exe$C:\Program Files\Adobe\Adobe Photoshop CS2\ImageReady.exe$C:\Program Files\Java\jre6\bin\javaws.exe$"
The $ symbol is used as a delimiter to separate each item from the other. I need to add the exe file name at the end of each path to a listbox. However the initial process of retrieving the variable to individual array elements is not working properly.
Dim strArr() As String = g.Split("$") 'This variable is empty
For count = 0 To strArr.Length - 1
Dim arr As String = strArr(count).Split("\")
Dim strval As String = ""
For i As Integer = 3 To arr.Length - 1
strval = arr(i)
Dim j As Integer = arr.Length - 1
strval = arr(j)
Dim result As String = strval.Substring(g.Length - 5)
result = g.Substring(g.LastIndexOf("\") + 1)
ListBox1.Items.Add(result)
Next
Next
No need to do all this work. The System.IO.Path class has methods to do this for you. You want to use either System.IO.Path.GetFileName or System.IO.Path.GetFileNameWithoutExtension. Since you've already split all the file paths, just pass those paths to either of the aforementioned methods and add the result to your listbox.
Dim strArr() As String = g.Split("$")
For Each path As String In strArr
ListBox1.Items.Add(System.IO.Path.GetFileName(path))
Next
Please refer to the code below and the associated comments. Also I have comment out some code which I feel is not required based on what you want to do.
Dim strArr() As String = g.Split("$") 'This variable is empty
For count = 0 To strArr.Length - 1
Dim arr() As String = strArr(count).Split("\") ' Split returns an array
Dim strval As String = ""
For i As Integer = 3 To arr.Length - 1
'strval = arr(i)
Dim j As Integer = arr.Length - 1
strval = arr(j)
'Dim result As String = strval.Substring(g.Length - 5)
'result = g.Substring(g.LastIndexOf("\") + 1)
ListBox1.Items.Add(strval)
Next
Next