How to port this Linq to VS 2005 - vb.net

I have following code in VS2008
If Linq.Enumerable.Contains(Of String)(selectedUsersRoles, RoleCheckBox.Text) Then
RoleCheckBox.Checked = True
Else
RoleCheckBox.Checked = False
End If
I need the above code in VS2005
What can i do instead of linq in above code? Can anybody help?

RoleCheckBox.Checked = False
For Each str As String in selectedUsersRoles
If str = RoleCheckBox.Text Then
RoleCheckBox.Checked = True
Exit For
End If
Next
If you don't wish to alter the RoleCheckBox.Checked twice (when str is actually found) then declare a boolean flag (i.e. boolFound) with an initial False value, change it to true when found, and asign RoleCheckBox.Checked = boolFound after the "for each" loop....

bool containsRole = false;
foreach(string entry in selectedUsersRoles)
{
if(entry == RoleCheckBox.Text)
{
containsRole = true;
break;
}
}
RoleCheckBox.Checked = containsRole;
The code is C# but i guess u'll get the idea.
This is for IEnumerable. If you have a list try Binary Worrier's sollution.

Related

How to trigger a command only when the for loop is over?

I have a for loop with an If condition inside, and I want to trigger a command only when the loop is over and the If condition wasn't triggered.
You'll need to keep a flag. Set it to false by default and when the if statement gets executed you set the flag to true.
Dim didIfExecute As Boolean = False
For ...
If ... Then
didIfExecute = True
End If
Next
If Not didIfExecute Then
...
End If
You could use Return for it. Return immediately exits the function. Here is a example:
Dim a As Byte
For a = 10 To 20
Console.WriteLine("value of a: {0}", a)
If a= "20" Then
CoolFunction()
Return
End If
Next
MsgBox("u will never see this msg! maybe if you edit the if condition...")
If the If condition got triggered then it will not execute the code which is under the loop. If the If condition don't get triggered then everything what is under the loop will be executed.
You could also use a Boolean for it. This would not "kill" everything that is under the Loop.
Dim a As Byte
Dim EvenTriggered As Boolean = False
For a = 10 To 20
Console.WriteLine("value of a: {0}", a)
If a= "20" Then
CoolFunction()
EventTriggered = True
End If
Next
If EventTriggered = False Then
MsgBox("u will never see this msg! maybe if you edit the if condition...")
End IF

How to make a multi line if statement in your own programming language visual basic

Ok so i am making my own programming language in visual basic and im using Arrays to make the commands. right now i have:
if Array(0) = "if" then
if array(4) = "then" then
if Array(1) = Array(3) then
if Array(5) = "println." then
console.writeline(stringtypedin)
end if
end if
end if
end if
but what i want to happen is to make the if statement and let println be on a different line. dont know if this is possible but i thought i would ask
Ok i know i have this tagged as an answer but i think i have the solution and will update you to see if it works but let me know if you think this will work
if array(0) = "if" then
if array(4) = "then" then
if Array(1) = Array(3) then
booleanvar = true
end if
end if
else
booleanvar = false
end if
if booleanvar = true then
do until array(0) = "end."
if array(0) = "println." then
console.writeline(stringtypedin);
loop
end if
with this i should be able to say
if 5 = 5 then
println. hello
end.

conversion from string to type 'Integer' is not valid in below code

Private Sub CreatFolder()
' the line below gives error like Conversion from string "C:\inetpub\wwwroot\Createfolders" to type 'Integer' is not valid
Dim directoryPath As String = Path.GetInvalidpathChars(Server.MapPath("..\Cre_Folders\File\") & txtFolderName.Text.Trim())
If Not Directory.Exists(directoryPath) Then
Directory.CreateDirectory(directoryPath)
lblsuccess.Visible = True
'lblmsg.Visible = False
lblValidName.Visible = False
lblDelete.Visible = False
lblerr.Visible = False
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Folder already existed.');", True)
'lblmsg.Text = "Directory Already Exists..."
lblValidName.Visible = False
lblDelete.Visible = False
lblsuccess.Visible = False
lblerr.Visible = False
End If
Call Treeview()
End Sub
I guess what you are getting is a compile-time error not a run-time error.
The method Path.GetInvalidPathChars() does not validate a path. It does not take a string and tell you if is good or bad. Instead it returns an array of chars that are known to be invalid in the OS being used. MSDN Example.
Also note that the returned list of chars may not be complete - See: Missing ? & *.

Getting return without gosub in my own function in vba

I've been battling with vba for a bit and surprisingly it's not getting much better.
I have written the following code so I can have a special comparison operator for an object.
Public Function myEquals(v As CCtypestore) As Boolean
If v Is Nothing Then
myEquals = False
Return
End If
If Me.Acronym = v.Acronym Then
myEquals = True
Return
End If
myEquals = False
Return
End Function
The object v I'm passing in is Nothing at the moment so I would have sort of expected the result to be a trivial False. Surprisingly instead I'm getting an error Return without gosub. Any clue why that might be?
Delete the Return statements!
In VBA, you set the return value with the line myEquals = ....
All in all, you can reduce your function to the following code:
Public Function myEquals(v As CCtypestore) As Boolean
If Not v Is Nothing Then
myEquals = (Me.Acronym = v.Acronym)
End If
End Function
Alternatively, use this:
Public Function myEquals(v As CCtypestore) As Boolean
On Error Goto ErrorHandler
myEquals = (Me.Acronym = v.Acronym)
Exit Function
ErrorHandler:
myEquals = False
End Function
Return is an old relic if you want to work with direct jumps in the code, i.e. build spaghetti code! See this example from the help file:
Sub GosubDemo()
Dim Num
' Solicit a number from the user.
Num = InputBox("Enter a positive number to be divided by 2.")
' Only use routine if user enters a positive number.
If Num > 0 Then GoSub MyRoutine
Debug.Print Num
Exit Sub ' Use Exit to prevent an error.
MyRoutine:
Num = Num/2 ' Perform the division.
Return ' Return control to statement.
End Sub ' following the GoSub statement.
Use Exit Function instead of Return.
You can also avoid those Exit Function by using a construction like
If .... Then
'some code
ElseIf...Then
'some other code
ElseIf...Then
'etc
Else
'final
End if

compare the data passed in query string

sir i m working on a project and i want to pass a value in query string and redirect it to next page .....ok
now on next page i want to make a check on this
if the value of querystring is equal to a particular value then a message show....
i have done upto here
now the prob comes if the values is not matched the code written in else is not working
i m writting my code:-----
if(request.param.count>0) then
label1.visible="true"
label1.text="hello"
else
label1.visible=false
Something like the following will test your QueryString to a string value.
If (Request.QueryString("MyString") == "hello") Then
label1.Text = "Hello";
label1.Visible = true;
Else
label1.Visible = false;
End If
In your code, you had "'s around the true and false values. You don't need these.