calling list item where var = value - asp.net

is there a way of calling a specific list of lists without having to check (or loop) them all?
it is easier to understand with an example....
lets say
callList(5).key = "1234"
callList(5).callOpened = "11/26/13"
now i want to do something like
textbox_callOpened.text = callList(where key = "1234").callOpened
i also need to know what index that was at for there are many more items that i need to output too.

You can use LINQ. Add Imports System.Linq at top of the file and use First method with lambda expression as a predicate to get what you need:
' that gives you item matching your predicate '
Dim item = callList.First(Function(x) x.Key = "1234")
' you can use it to set the property '
textbox_callOpened.text = item.callOpened

pseudo code, didn't test but should work
dim something = callList.firstordefault(function(d) d.key = "1234")
if something is not nothing then
textbox_callOpened.text = something.callOpened
else
'cant find an element with key 1234
end if

Related

How to autofill OpenOffice Math formula editor in OOo Calc?

I am using OpenOffice Calc spreadsheet formulas with psuedo-random numbers to generate arrays of arithmetic problems which I can easily update to creating new worksheets (I'm a teacher)
Problems are output as formula mark-ups in string form. OOo Math formulas use these string commands typed into the editor to display nicely formatted maths expressions.
I can do this next step manually:
1) go to source cell and copy string mark-up to clipboard
2) select target cell and clear existing contents and objects
3) create new Math object anchored to target cell
4) open Math editor window and paste in mark-up string
5) exit Math editor window and return cursor to source cell
Result: a nice maths expression of given arithmetic problem.
I need to be able to do this for entire columns of source cells on various sheets.
...even better, to then add a listener to dynamically update as sources are updated.
I found code here: Cell content inside formula that achieves this for a fixed pair of cells, but despite all my best efforts, I have had to admit defeat - generalising this code is simply beyond my expertise!
The absolute ideal would be a macro function that I could call like a spreadsheet function; with input arguments (sourceCell, targetCell, listenerON/OFF) that could run the above algorithm and dynamically update if required.
Can anybody help me? A solution like this, or any kind of workaround would be immensely helpful.
UPDATE 2016/10/27
Thank you Jim K, that did work, but use of the dispacher comes with a whole host of difficulties I hadn't foreseen.
I just found Charlie Young's post in the OpenOffice forum which makes use of the API. I have included my adaptation of his code below.
Can anybody help me to integrate it into a function in a similar way as I've described? I don't know how to solve placement of the Math object in to the target cell.
The API code is great as it will create a new Math object each time the code is updated. Existing ones do need to be deleted though.
I think the limitation of not being able to delete existing objects from within a function is going to persist. Would this be the case even if done by a subroutine called by the function?
function InsertFormula(paraFromCell, paraToCell)
Dim oDoc As Object
Dim oSheet As Object
Dim oShape As Object
oDoc = ThisComponent
oSheet = oDoc.Sheets(0)
oShape = oDoc.createInstance("com.sun.star.drawing.OLE2Shape")
oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oSheet.Drawpage.Add(oShape)
oShape.Model.Formula = paraFromCell
oShape.setSize(oShape.OriginalSize)
end function
NEXT UPDATE
I've been managing to solve my own problems quite quickly now...
I've decided to go with a sub, not a function, so I can access the sheet to delete existing objects. Code is attached - Source cells are in Column C and target cells in matching rows of Column A. So far I am only able to send objects to $A$1.
How do I anchor each new object to a specific cell?
REM ***** BASIC *****
Sub InsertThisFormula
Dim oDoc As Object
Dim oSheet As Object
Dim oShape As Object
Dim sourceCell As Object
Dim targetCell As Object
oDoc = ThisComponent
oSheet = oDoc.Sheets(1)
Dim n As Integer
n = 1 'number of rows of formulas
for i = 0 To n-1
rem loop through cells
sourceCell = oSheet.getCellByPosition(2, i)
targetCell = oSheet.getCellByPosition(0, i)
rem clear target cell object/s
targetCell.ClearContents(128)
oShape = oDoc.createInstance("com.sun.star.drawing.OLE2Shape")
oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oSheet.Drawpage.Add(oShape)
oShape.Model.Formula = sourceCell.string
oShape.setSize(oShape.OriginalSize)
Next i
End Sub
Starting from Mifeet's example, add this to My Macros:
rem ----------------------------------------------------------------------
rem Creates a math formula from text
Function InsertFormulaFromCell(paramCellFrom, paramCellTo)
dim document as object
dim dispatcher as object
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem go to cell containing markup and copy it
dim fromCellArgs(0) as new com.sun.star.beans.PropertyValue
fromCellArgs(0).Name = "ToPoint"
fromCellArgs(0).Value = paramCellFrom
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, fromCellArgs())
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
rem go to cell where I want the formula displayed
dim toCellArgs(0) as new com.sun.star.beans.PropertyValue
toCellArgs(0).Name = "ToPoint"
toCellArgs(0).Value = paramCellTo
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, toCellArgs())
rem open Star.Math
oDesk = createUnoService ("com.sun.star.frame.Desktop")
dispatcher.executeDispatch(document, ".uno:InsertObjectStarMath", "", 0, Array())
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem paste clipboard using Array() as place-holder for variable name
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())
rem exit Star.Math
dispatcher.executeDispatch( _
document, ".uno:TerminateInplaceActivation", "", 0, Array())
InsertFormulaFromCell = "Math Formula updated " & Now()
End Function
To run it, put this formula in cell C5:
=INSERTFORMULAFROMCELL("$C$3","$C$20")
Now when the values get updated, it creates another formula.
Note: I could not get the .uno:Delete section of Mifeet's code to work, perhaps because functions are not supposed to access other cells. This may require manually deleting the formulas before creating new ones.
(Posted solution on behalf of the OP).
This is now solved. After a lot of searching, I found what I needed! Simple really. Future improvements might be to resize cells appropriately. Happy for now. Thanks to Jim K and the rest of the Stack Overflow community!
Complete macro below:
REM ***** BASIC *****
Sub InsertThisFormula
Dim oDoc As Object
Dim oSheet As Object
Dim oShape As Object
Dim sourceCell As Object
Dim targetCell As Object
oDoc = ThisComponent
oSheet = oDoc.Sheets(1)
Dim n As Integer
n = 6 'number of rows of formulas
for i = 0 To n-1
rem loop through cells
sourceCell = oSheet.getCellByPosition(2, i)
targetCell = oSheet.getCellByPosition(3, i)
rem clear target cell object/s
targetCell.ClearContents(128)
oShape = oDoc.createInstance("com.sun.star.drawing.OLE2Shape")
oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oSheet.Drawpage.Add(oShape)
oShape.Model.Formula = sourceCell.string
oShape.setSize(oShape.OriginalSize)
oShape.Anchor = targetCell
oShape.MoveProtect = True
Next i
End Sub

How to select specific index in a multidimensional array in asp.net using vb

I cannot explain well in the title so i'll explain here the scenario:
In here, i have a query that will be placed in an array.
Dim arrUsers As Object = {{sqlReader("dephead"), dropdown1}, {sqlReader("mm"), dropdown2}
I want to enable the dropdown object if it matches my if else condition:
'indexes1= the 1st indexes of each pair in the array (the sqlReaders)
'indexes2= my dropdown objects in my user control
For Each indexes1 In arrUsers
If Session.Item("EmployeeID") = indexes1 Then
indexes2.Enabled = True
End If
Next
so lets say the my session id will be "dephead", then the dropdown1 will be enabled. Thats what i would like to happen but im not familiar on arrays so kindly help me. Thanks.
This should work:
For i As Integer = 0 to arrUsers.GetUpperBound(0)
If Session.Item("EmployeeID") = arrUsers(i)(0) Then
arrUsers(i)(1).Enabled = True
End If
Next
You simply give the array two indexes instead of one. arrUsers(0)(1), for example. The first index returns the object at that index, and the second index returns the object inside the previously returned array. arrUsers(0)(1) will first grab the array at 0 index ({sqlReader("dephead"), dropdown1}), then get the object at index 1 (dropdown1).
This answered my question. The answer above helped me and i just changed the declaration of my arrays and changed some syntax.
Dim arrUsers(,) As Object =
New Object(,) {
{sqlReader("dephead"), dropdown1}, _
{sqlReader("mm"), dropdown2}, _
}
Dim bound0 As Integer = arrUsers.GetUpperBound(0)
Dim bound1 As Integer = arrUsers.GetUpperBound(1)
For i As Integer = 0 To bound0
If Session.Item("EmployeeID") = arrUsers(i, 0) Then
arrUsers(i, 1).Enabled = True
End If
Next

How to filter list in asp.net/vb.net using linq

I have a list of class
How can I filter on some condition..I am applying following it is working when value gets exact match
Dim result = obj.OfType(Of clsEmrItmMstr)().Where(Function(s) s.GenName Like txtserach.Text)
grddetails.DataSource = result
grddetails.DataBind()
where "clsEmrItmMstr" is my class name and "GenName" is field in class
Instead of the Like operator you could simply use String.Contains:
Dim result = obj.OfType(Of clsEmrItmMstr)().
Where(Function(s) s.GenName.Contains(txtserach.Text))
With Like you need * as wildcard, so this should work:
Dim result = obj.OfType(Of clsEmrItmMstr)().
Where(Function(s) s.GenName Like String.Format("*{0}*", txtserach.Text))
(assuming that you want to find all objects where the GenName contains the text entered in the TextBox)
You can use Contains function
Dim result As dynamic = obj.OfType(Of clsEmrItmMstr)().Where(Function(s) s.GenName.Contains(txtserach.Text))
grddetails.DataSource = result
grddetails.DataBind()

delete an element from an array in classic ASP

Given the following array as an example...
arr(0)(0) = 3
arr(0)(1) = name
arr(0)(2) = address
arr(1)(0) = 7
arr(1)(1) = name
arr(1)(2) = address
arr(2)(0) = 14
arr(2)(1) = name
arr(2)(2) = address
I need to delete the middle element (id=7) from the array. I understand that I need to loop through the array and move each record that isnt to be deleted into a new array. I tried like this...
Dim newArr,i
Redim newArr(Ubound(arr))
For i = 0 to Ubound(arr)
If (CStr(arr(i)(0)) <> 7 ) Then
newArr(i) = arr(i)
End if
Next
When debugging this I can see the if statement work so I know only 2 elements are copied but newArr is empty at the end of this. What am I missing. I am a PHP coder that is new to classic asp and Im used to having array functions that make this kind of thing unnecessary. Any help appreciated. Thank you.
You don't need new array, you can just reassign the items and "crop" the array:
Const removalIndex = 1
For x=removalIndex To UBound(arr)-1
arr(x) = arr(x + 1)
Next
ReDim Preserve arr(UBound(arr) - 1)
This code will remove the array item at index 1 from the main array. If you don't know in advance the index of the item to remove, you can easily find it with a simple loop over the array.
Instead of using array you can give Scripting.Dictionary a try.
It is much more flexible, and has, among others Remove method.
I suggest using Scripting.Dictionary and using it as a List/collection instead, as it allows for insertions and deletions. See here: Lists in VBScript
I don't know the definitive answer, but if I were to take a stab in the dark guess I'd suggest that since the array is two dimensional maybe you have to explicitly refer to it that way?
Dim newArr,i
Redim newArr(Ubound(arr),3)
For i = 0 to Ubound(arr)
If (CStr(arr(i)(0)) <> 7 ) Then
newArr(i)(0) = arr(i)(0)
newArr(i)(1) = arr(i)(1)
newArr(i)(2) = arr(i)(2)
End if
Next
I see some VBScript syntax issues. First:
arr(0)(0) = 3 'ERROR: Subscript out of range
arr(0, 0) = 3 'CORRECT
Next:
ReDim newArr(Ubound(arr)) 'this is 1 dimensional array
newArr(0) = arr(0) 'this will NOT work
newArr(0) = arr(0, 0) 'this will work
And finally: why you convert to String and then compare it to an Integer with:
(CStr(arr(i)(0)) <> 7)

How do you call a method from a variable in ASP Classic?

For example, how can I run me.test below?
myvar = 'test'
me.myvar
ASP looks for the method "myvar" and doesn't find it. In PHP I could simply say $me->$myvar but ASP's syntax doesn't distinguish between variables and methods. Suggestions?
Closely related to this, is there a method_exists function in ASP Classic?
Thanks in advance!
EDIT: I'm writing a validation class and would like to call a list of methods via a pipe delimited string.
So for example, to validate a name field, I'd call:
validate("required|min_length(3)|max_length(100)|alphanumeric")
I like the idea of having a single line that shows all the ways a given field is being validated. And each pipe delimited section of the string is the name of a method.
If you have suggestions for a better setup, I'm all ears!
You can achieve this in VBScript by using the GetRef function:-
Function Test(val)
Test = val & " has been tested"
End Function
Dim myvar : myvar = "Test"
Dim x : Set x = GetRef(myvar)
Response.Write x("Thing")
Will send "Thing has been tested" to the client.
So here is your validate requirement using GetRef:-
validate("Hello World", "min_length(3)|max_length(10)|alphanumeric")
Function required(val)
required = val <> Empty
End Function
Function min_length(val, params)
min_length = Len(val) >= CInt(params(0))
End Function
Function max_length(val, params)
max_length = Len(val) <= CInt(params(0))
End Function
Function alphanumeric(val)
Dim rgx : Set rgx = New RegExp
rgx.Pattern = "^[A-Za-z0-9]+$"
alphanumeric = rgx.Test(val)
End Function
Function validate(val, criterion)
Dim arrCriterion : arrCriterion = Split(criterion, "|")
Dim criteria
validate = True
For Each criteria in arrCriterion
Dim paramListPos : paramListPos = InStr(criteria, "(")
If paramListPos = 0 Then
validate = GetRef(criteria)(val)
Else
Dim paramList
paramList = Split(Mid(criteria, paramListPos + 1, Len(criteria) - paramListPos - 1), ",")
criteria = Left(criteria, paramListPos - 1)
validate = GetRef(criteria)(val, paramList)
End If
If Not validate Then Exit For
Next
End Function
Having provided this I have to say though that if you are familiar with PHP then JScript would be a better choice on the server. In Javascript you can call a method like this:-
function test(val) { return val + " has been tested"; )
var myvar = "test"
Response.Write(this[myvar]("Thing"))
If you are talking about VBScript, it doesn't have that kind of functionality. (at least not to my knowledge) I might attempt it like this :
Select myvar
case "test":
test
case "anotherSub":
anotherSub
else
defaultSub
end select
It's been a while since I wrote VBScript (thank god), so I'm not sure how good my syntax is.
EDIT-Another strategy
Personally, I would do the above, for security reasons. But if you absolutely do not like it, then you may want to try using different languages on your page. I have in the past used both Javascript AND VBScript on my Classic ASP pages (both server side), and was able to call functions declared in the other language from my current language. This came in especially handy when I wanted to do something with Regular Expressions, but was in VBScript.
You can try something like
<script language="vbscript" runat="server">
MyJavascriptEval myvar
</script>
<script language="javascript" runat="server">
function MyJavascriptEval( myExpression)
{
eval(myExpression);
}
/* OR
function MyJavascriptEval( myExpression)
{
var f = new Function(myExpression);
f();
}
*/
</script>
I didn't test this in a classic ASP page, but I think it's close enough that it will work with minor tweaks.
Use the "Execute" statement in ASP/VBScript.
Execute "Response.Write ""hello world"""
PHP's ability to dynamically call or create functions are hacks that lead to poor programming practices. You need to explain what you're trying to accomplish (not how) and learn the correct way to code.
Just because you can do something, doesn't make it right or a good idea.
ASP does not support late binding in this manner. What are you trying to do, in a larger sense? Explain that, and someone can show you how to accomplish it in asp.
Additionally, you might consider "objectifying" the validation functionality. Making classes is possible (though not widely used) in VB Script.
<%
Class User
' declare private class variable
Private m_userName
' declare the property
Public Property Get UserName
UserName = m_userName
End Property
Public Property Let UserName (strUserName)
m_userName = strUserName
End Property
' declare and define the method
Sub DisplayUserName
Response.Write UserName
End Sub
End Class
%>

Resources