Array.IndexOf always returns -1 - asp.net

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.

Related

ASP.NET determine number of digits after decimal point on GridView.RowDataBound

I am binding data to my GridView control and in one of the columns I have a price column which is of type double.
I also have a key in the web.config file that tells me how many digits to display after decimal point.
I want to display the data bound field according to that key in the web.config.
I saw this post on how to set the number of digists after decimal point: DataBinding Eval To 2 Decimal Place Doesn't Show 0
but I need a way of doing that with a variable that tells how many decimal places to use.
I tried this:
Private Sub grdData_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdData.RowDataBound
Dim numOfDigitsAfterDecimal = Integer.Parse(ConfigurationManager.AppSettings("numOfDigitsAfterDecimal"))
Dim d As String = ""
For Z As Integer = 0 To numOfDigitsAfterDecimal - 1
d = d + "#"
Next
e.Row.Cells(3).Text = String.Format("{0:0.{1}}", Double.Parse(e.Row.Cells(3).Text), d)
End Sub
but it does not work, however if I do:
e.Row.Cells(3).Text = String.Format("{0:0.##}", Double.Parse(e.Row.Cells(3).Text))
it does work and displays 2 decimal places.
you can use N0 or N1 or N2 and so the number will show the decimal place. check this article
Example :
number.ToString("N3")
// N: 1,054.32
// N0: 1,054
// N1: 1,054.3
// N2: 1,054.32
// N3: 1,054.322

Select number from a string classic asp

I need to get a 4/5 digit number which always comes directly after a #
For example it'll be "Item Title (#1234)" however it's not always in the same place or at the end.
Not sure how I go about doing that
You need to manually parse the string.
I've made a quick function for this:
'//return the number after delimeter string, if exists (first occurance only)
'//in case no number exists, returns Empty value
Function FindNumberAfter(rawValue, delimeterString)
Dim index, x, curChar
Dim sBuffer
FindNumberAfter = vbEmpty
index = InStr(rawValue, delimeterString)
If index>0 Then
For x=index+Len(delimeterString) To Len(rawValue)
curChar = Mid(rawValue, x, 1)
If IsNumeric(curChar) Then
sBuffer = sBuffer & curChar
Else
Exit For
End If
Next
If Len(sBuffer)>0 Then FindNumberAfter = CLng(sBuffer)
End If
End Function
Usage in your case:
Response.Write(FindNumberAfter("Item Title (#1234)", "#"))

Check Integer Value on Asmx

i have sample web method on my services,
<WebMethod()> _
Public Function AddThis(ByVal x As Integer, ByVal y As Integer) As Integer
Dim mySum As Integer
If Not IsNumeric(x) Then
Return 0
End If
mySum = x + y
Return mySum
End Function
when i debug it, suddnely i made mistake about x or y value , and it give me an error :
System.ArgumentException: Cannot convert gf to System.Int32.
Parameter name: type ---> System.FormatException: Input string was not in a correct format.
i try to check the value :
If Not IsNumeric(x) Then
Return 0
End If
but keep getting the error, is it possible to check the value first before run the services?
At the point you check "IsNumeric(x)", X will always be numeric.
You expect an Integer as parameters, so the error will occur the moment you call the function AddThis.
You will have to check X and Y before calling this function. Or if that is not possible, use something like this:
Public Function AddThis(ByVal x As String, ByVal y As String) As Integer
Dim mySum As Integer
If Not IsNumeric(x) Or Not IsNumeric(y) Then
Return 0
End If
mySum = Integer.Parse(x) + Integer.Parse(y)
Return mySum
End Function

Is there a way to access an entire row with the GetRows() method?

I have used the GetRows() method to put a recordset into a 2D array. I can access individual array items like this:
x = rows(colNumber, rowNumber)
Now I want to take a whole row / dimension from this array and pass it into another function.
Is there a method for doing this? I haven't been able to find one. Looking for something like this:
entireSingleRow = rows(*, rowNumber)
There isn't such construct in the language, but the following "helper" will do"
REM 'Returns 1-D array of all "columns" in a "row" with index "rowNumber"'
REM 'from the 2-D "from2DArray" array (column, row)'
Function GetCols(ByRef from2DArray, ByVal rowNumber)
Dim cols : cols = UBound(from2DArray, 1)
Dim i
Dim result()
For i = 0 To cols
Redim Preserve result(i)
result(i) = from2DArray(i, rowNumber)
Next
GetCols = result
End Function
entireSingleRow = GetCols(rows, rowNumber)

ASP.NET Reverse Loop

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

Resources