ASP.NET Reverse Loop - asp.net

I have some code and I'm trying to do my For loop in reverse and it doesn't seem to work
Session("mysession") = "1234-5678-"
Dim delimiters As Char() = New Char() {"-"C}
Dim mystring As String() = Trim(Session("mysession")).Split(delimiters, StringSplitOptions.RemoveEmptyEntries)
For x = 0 to mystring.Length - 1
'Do Something
Next
That works but shows it in the wrong order I'm trying to reverse it by simply doing this
For mystring.Length - 1 to x = 0
But I'm getting an error
Expression is a value and therefore cannot be the target of an assignment.
Any ideas?
Thanks

The syntax of your reversed loop is incorrect. It should be:
For x = mystring.Length - 1 To 0 Step -1

Related

Cannot include a cell-formula into VBA code in MS Excel

I am trying a simple VBA program, that looks like this:
Function NotWorking() As Single
If Range("C3") = 0 Then
NotWorking = "=0.6*7"
Else
NotWorking = 1.2
End If
End Function
If cell C3 has 0 as a value, the function NotWorking has to return in the current cell the result of *=0.6*7*, which is 4,2. If the value in C3 is not 0, then the current cell has to return 1,2.
In my case, things aren't the way I expected. In the current cell I type =NotWorking(). If C3 is anything but 0, I obtain 1,2, but when I appropriate 0 to C3, the current cell returns #VALUE.
Why does this code not work?
Best regards and thanks in advance!
I notice that "=0.6*7" is a string, not a number, but 1.2 is a number.
You have specified that the function returns a single, but "=0.6*7" isn't a single.
Don't try to return it from the routine, but reach out to the sheet from within the routine.
Sub NotWorking()
Dim src As Range
Dim dst As Range
Set src = Range("C3")
Set dst = Range("E3")
If src.Value = 0 Then
dst.Formula = "=0.6 * 7"
Else
dst.Value = 1.2
End If
End Sub

Finding and looping through labels

Running VS 2013 VB.net.
I have 3 labels on my aspx page labelled label1, label2 and label3.
I want to loop through each one in the code behind and assign values to them.
Here is my code
Dim X As Integer = 1
For Each obj In values
Dim myLabel As Label
myLabel = TryCast(Me.FindControl("Label" + X), Label)
myLabel.Text = Math.Round(obj, 2)
X = X + 1
Next
I know there is only 3 obj's so x will always be between 1 and 3. What am I doing wrong as I get the following.
Conversion from string "Label" to type 'Double' is not valid
IF I change ("label" + x) to ("label1") I get
Object reference not set to an instance of an object.
on the line below.
You want to concatenate strings not to calculate, in VB.NET you use & instead of +(as opposed to C#). So this should work:
myLabel = TryCast(Me.FindControl("Label" & X), Label)
You should also set Option Strict to On, without exception. Then you first have to fix a lot of compiler errors, but it will help to write much more robust code. This wouldn't compile either:
myLabel.Text = Math.Round(obj, 2)
because a Math.Round returns a Double and Label.Text is a String. You just need to use Math.Round(obj, 2).ToString() to fix it.
Here's a different approach to get your labels using Linq:
Dim myLabels = From lbl In Me.Form.Controls.OfType(Of Label)()
Where lbl.ID.StartsWith("Label")
For Each lbl As Label In myLabels
' ... '
Next
Also since your using TryCast check your variable before using it - it will be Nothing if it does not make the cast.
myLabel = TryCast(Me.FindControl("Label" & X.ToString), Label)
If myLabel IsNot Nothing Then
'safe to use variable here
End If

Array.IndexOf always returns -1

This is my code.
Public Sub SomeFucntion(ByVal test As Short)
SomeOtherFucntion(MyArray)
If Array.IndexOf(MyArray, test) <> -1
//....
End If
End Sub
test return value 10.
But i am getting IndexOf value as -1.
Inside QuickWatch in VS 2005 when i put the value as 10 instead of test i am getting the correct index.
My array is single dimensional array which now has 2,5 and 10. Since its got 10 it should ideally return 2 as the index.
The problem might be caused by the fact that your array contains integers, but the value you are looking for is a short. Consider the following example:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, value)) ' Prints -1
If the array contains integers, you need to convert your short into an integer first, for example, by using CInt:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' Prints 0
Edit: Note that the declared type has nothing to do with this. Let's declare the array as Object, since that's what you mentioned in your comment (note that the following example requires Option Strict Off, which is bad):
Dim myArray As Object = New Integer() {5}
Dim value As Object = 5S ' Short literal
Console.WriteLine(Array.IndexOf(myArray, value)) ' still returns -1
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' returns 0
Note: You can make that conversion implicit by declaring your function as Public Sub SomeFunction(ByVal test As Integer).
Dim test As Short = 5
Dim MyArray() As Short = {1, 3, 4, 5, 7, 3}
If Array.IndexOf(MyArray, test) <> -1 Then
MessageBox.Show("Index Found.")
End If
The above works. since test is declared as short, make sure MyArray is also declared as short.

chart datapoint operand usage

In the code snippet below I am attempting to compare two data points as I am trying to put all low value points in a pie chart dataset into on "Other" datapoint.
I am getting an "Cannot apply operator <> to operands of type System.....DataPoint and System.....DataPoint" on this code line " If pointValue <= minValue AndAlso Me.series.Points(index) <> colectedDataPoint "
Dim collectedValue As Double = 0.0
For index As Integer = 0 To Me.series.Points.Count - 1
Dim pointValue As Double = Math.Abs(Me.series.Points(index).YValues(0))
If pointValue <= minValue AndAlso Me.series.Points(index) <> colectedDataPoint Then
' Add point value to the collected value
collectedValue += pointValue
' Add point to supplemental series
Me.supplementalSeries.Points.Add(Me.series.Points(index).Clone())
' Remove point from the series
Me.series.Points.RemoveAt(index)
index -= 1
End If
Next
but yet if I write the same loop in C# that equivalent line is fine;
this.series.Points[index] != colectedDataPoint
anyone have a solution since the whole project is written in vb.net and i'd prefer to keep it uniform.

Can Do While Loop stop when counter reaches X

I want to edit some legacy code written in classic-ASP.
Currently I've got a subroutine declared that uses a for-next loop to output some radio buttons:
For i = 1 to Cols
response.write "blah"
...
Next
i is simply a counter, Cols is a value passed to the sub-routine. I tried editing the for-loop to be a while loop instead:
i = Start
do while i <= Cols
response.write "blah"
...
i = i + 1
loop
But I get a Response Buffer Limit Exceeded error. If I replace Cols with the value it works fine. Is this a limitation in classic-ASP?
Reason I want to use a do while loop is because currently the sub-routine is limited to looping from 1 to Cols. It would be helpful to sometimes specify the loop counts backwards (i.e. step -1) but I can't write:
if Direction = Backwards then
For i = Cols to 1 step -1
else
For i = 1 to Cols
end if
How about:
If Direction = Backwards Then
cs = 10
ce = 1
s = -1
Else
cs = 1
ce = 10
s = 1
End If
For i = cs To ce Step s
''
Next

Resources