Clear List in Session Object - vb.net

Im very new to Vb..
I get the Following error 'Option Strict On disallows late Binding' when I try to clear a list in my seesionObject like shown.
Private Sub ClearSessionList()
Dim context As Object = System.Web.HttpContext.Current.Session("MySessionobject")
context.MyListProperty = New List(Of String)
End Sub
The error persist when i try following casts aswell
DirectCast(context.MyListProperty, List(Of String))
CType(context.MyListProperty, List(Of String))
I assume my Casts are wrong in some way, anyone that can point out the error for me and show how I can clear the List?

Always use option strict, then you couldn't access MyListProperty until you haven't casted context from Object to it's actual type. Otherwise VB.NET will try to cast it for you, sometimes with weird results.
So presuming the type of it is MySessionObject:
Dim context As Object = System.Web.HttpContext.Current.Session("MySessionobject")
Dim myObj As MySessionObject = DirectCast(context, MySessionObject)
myObj.MyListProperty.Clear()

Related

'Public member 'Find' on type 'MongoCollectionImpl(Of BsonDocument)' not found.'

I am trying to find a particular user from a mongodb collection that matches the given id. Folliwng is my VB.Net code. However, I keep getting the error 'Public member 'Find' on type 'MongoCollectionImpl(Of BsonDocument)' not found.'
Public Function GetCollectionByName(ByVal collectionName As String)
Dim db As IMongoDatabase = DBcontext()
Dim collection As IMongoCollection(Of BsonDocument)
collection = db.GetCollection(Of BsonDocument)(collectionName)
Return collection
End Function
Public Function GetUser(ByVal id As String)
Dim filter = Builders(Of BsonDocument).Filter.Eq(Of String)("ID", id)
Dim collection = GetCollectionByName("Users")
Dim list = collection.Find(filter).ToList()`<<<<<<<<<<<<<<<<<<<<<<<<<<<<<ERROR here
Return list
End Function
Firstly, you must have Option Strict Off for that code to even compile. That's bad. You should immediately turn Option Strict On in the project properties and address all the issues it raises. One of those will be the fact that your GetCollectionByName has no return type declared. That means that here:
Dim list = collection.Find(filter).ToList()
that collection variable is implicitly type Object and you are relying on late binding when calling that Find method because the Object class has no such method. As a result, you get no help from Intellisense and Intellisense would have told you what members were and were not available if you were doing this properly.
Regardless, you still could have made it work if you had actually read the documentation for the types you're using to see what members they have. Here is the documentation for the interface you're using in that GetCollectionByName method and, I don't know about you but I don't see any Find method listed there. There is a FindSync method, so maybe that's what you actually want. If you had Option Strict On and used proper types every where, Intellisense would have shown you that.
You should also turn Option Strict On in the IDE options, so that it is On for all future projects.
I had a look at some documentation for the MongoCollectionImpl for Java and there appears to be a find method there but that doesn't necessarily mean that the same method is available in .NET and you aren't working directly with that class anyway. You are working with the IMongoCollection so you should only be working with members of that interface. Basically, your code would need to look more like the below with Option Strict On:
Public Function GetCollectionByName(ByVal collectionName As String) As IMongoCollection(Of BsonDocument)
Dim db As IMongoDatabase = DBcontext()
Dim collection As IMongoCollection(Of BsonDocument)
collection = db.GetCollection(Of BsonDocument)(collectionName)
Return collection
End Function
Public Function GetUser(ByVal id As String) As List(Of BsonDocument)
Dim filter = Builders(Of BsonDocument).Filter.Eq(Of String)("ID", id)
Dim collection = GetCollectionByName("Users")
Dim list = collection.FindSync(filter).ToList()
Return list
End Function
You may want to declare the GetUser method as type IList(Of BsonDocument) if you want to work with interfaces. You probably ought to rename that method or change the implementation too. If a method is returning a list then the name should not indicate that it returns a single item.

Public member 'Where' on type 'PlcBlockUserGroupComposition' not found [duplicate]

Using VB.NET, I'm trying to clean up a code base following ReSharper's guidelines. I currently have the following code:
'oSearchInput is defined outside this question
Dim oSearchRoutines As New SearchClient
Dim oSearchResults As List(Of SearchResult)
oSearchRoutines = 'WcfCallThatReturnsSearchClient
oSearchResults = oSearchRoutines.getSearchResults(oSearchInput).ToList
Now this works completely fine, but ReSharper warns that As New SearchClient has 'Value assigned is not used in any execution path'. So I removed that part to get this code:
'oSearchInput is defined outside this question
Dim oSearchRoutines
Dim oSearchResults As List(Of SearchResult)
oSearchRoutines = 'WcfCallThatReturnsSearchClient
oSearchResults = oSearchRoutines.getSearchResults(oSearchInput).ToList
If I'm understanding this correctly, everything should work exactly the same. However, an error is thrown when calling ToList:
Public member 'ToList' on type 'SearchResult()' not found.
I'm not exactly sure why there's any difference between the two snippets I have here.
Because you're not assinging the type SearchClient in your second example, oSearchRoutines will automatically be of type Object.
An expression of type Object is mainly not allowed to use Extension methods, like for example the ToList-method. For further information see here
The following example illustrates this behaviour:
Dim x As Object
Dim y As String = "ABC"
x = y
Dim a As List(Of Char) = y.ToList() 'This will work
Dim b As List(Of Char) = x.ToList() 'This will throw a System.MissingMemberException
The message Value assigned is not used in any execution path, appears because you're declaring oSearchRoutines with New in your first example.
This is unnecessary because you're assinging a new value to it on the line...
oSearchRoutines = 'WcfCallThatReturnsSearchClient
...before using it anywhere.
Thus you can just declare it without the keyword New
Dim oSearchRoutines As SearchClient
Related question: VB.NET: impossible to use Extension method on System.Object instance

variable is referenced from scope, but not defined LINQ expression tree

I'm trying to get this LINQ expression:
Result = Result.Where(Function(Row) _WhereExpressions(0).InElements.Contains(Convert.ToString(Row(0))))
I have this code for it:
convertMethod = GetType(System.Convert).GetMethod("ToString", New Type() {GetType(Object)})
containsMethod = GetType(System.Collections.Generic.List(Of String)).GetMethod("Contains", New Type() {GetType(String)})
Dim listParameter = Expression.Parameter(GetType(List(Of String)), "_WhereExpressions(0).InElements")
expr = Expression.Call(whereMethod, Result.AsQueryable.Expression,
Expression.Lambda(Expression.Call(listParameter, containsMethod,
Expression.Call(convertMethod, Expression.ArrayAccess(rowParameter, Expression.Constant(index)))), rowParameter))
I get the desired expression, but if I compile, I get the error:
variable '_WhereExpressions(0).InElements' of type 'System.Collections.Generic.List`1[System.String]' referenced from scope '', but it is not defined
The _WhereExpressions(0).InElements is of course declared.
How can I fix it?
Thanks.
EDIT: here are all the declarations:
Dim whereMethod = GetType(Queryable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Where").MakeGenericMethod(GetType(Object()))
Dim convertMethod As MethodInfo = Nothing
Dim containsMethod As MethodInfo = Nothing
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
The _WhereExpressions(0).InElements is a simple list of string, like this here:
Dim idlist As New List(Of String)
idlist.Add("1")
idlist.Add("2")
I read the linked post, but I can't really figure out, how I should solve my problem.
Expression trees have a lot capability, but looks a bit difficult for me.
EDIT2:
This is an example, what exactly I would like to achieve. Just copy and paste in vs:
Dim dt As New DataTable
dt.Columns.Add("f1", Type.GetType("System.String"))
dt.Columns.Add("f2", Type.GetType("System.Int32"))
For i = 0 To 100
dt.Rows.Add(i.ToString, i * 2)
Next
Dim indexes As New List(Of Integer)
indexes.Add(0)
indexes.Add(1)
Dim lst As New List(Of String)
lst.Add("10")
lst.Add("11")
Dim datarows As New List(Of DataRow)
For i = 0 To dt.Rows.Count - 1
datarows.Add(dt.Rows(i))
Next
Dim result As IEnumerable(Of Object())
result = datarows.Select(Function(row) indexes.Select(Function(index) row(index)).ToArray)
'I would like this as an expression:
result = result.Where(Function(row) lst.Contains(Convert.ToString(row(0))))
EDIT3: I got it:
Dim lst As Expression = Expression.Constant(list, GetType(System.Collections.Generic.List(Of String)))
It is hard to replicate code without knowing the full variables. I think your mistake though is in your understanding of Expression.Parameter. This is mainly used as a way to pass explicit parameters into the lambda: In your example, Row is a good example for when Expression.Parameter should be used. _WhereExpressions isn't an explicit parameter, it is a variable that I assume is in scope that you want to create a closure around.
You should also note that the second variable of the Expression.Parameter method is optional, and for debug purposes only: If you changed it to say: Expression.Parameter(GetType(List(Of String)), "Nonsense.nonsense"), you would probably see the error message change accordingly.
It sounds like you're trying to introduce a closure around _WhereExpressions. Using raw expression trees, this is fairly hard to do. The closest thing to an easy way to do this is to wrap Expression.Constant around _WhereExpressions.InElements. However, you're going to run into trouble if you're executing the compiled expression when _WhereExpressions is out of scope. See Issue with closure variable capture in c# expression.

Defining the type of a List in vb.net at runtime

I'm creating a class at run time using typebuilder and after I create this class I want to define its type for a list like
dim fooList as new List(of DynamicClassName)
Since this doesn't exist at compile time of course it throws an error. When I generate this type I return the type so I can't do something like
dim newType = createNewType(foobar)
dim fooList as new List(of getType(newType))
How do I assign the type of a List at runtime?
You can create a List(Of T), but AFAIK you won't be able to cast it to a typed object. I've used the String type in the following example.
Dim list As Object = Activator.CreateInstance(GetType(List(Of )).MakeGenericType(New Type() {GetType(String)}))
Debug.WriteLine((TypeOf list Is List(Of String)).ToString())
Output
True
So in your case it would look like this:
Dim newType = createNewType(foobar)
'Creates a List(Of foobar):
Dim list As IList = Ctype(Activator.CreateInstance(GetType(List(Of )).MakeGenericType(New Type() {newType})), IList)
'Creates a BindingList(Of foobar):
Dim bindingList As IBindingList = Ctype(Activator.CreateInstance(GetType(BindingList(Of )).MakeGenericType(New Type() {newType})), IBindingList)
This does not answer your question, but may solve your problem.
Another option would be to use an ArrayList (for which you don't have to assign a type). You can see the details here: http://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx.
Here is a basic example:
Dim anyArrayList as new ArrayList()
anyArrayList.add("Hello")
anyArrayList.add("Testing")

problems iterating through generic list of string

I have the follow code in my program where I hit a SQLCe database to append the results into a list. That part works, but instead of exiting the function 'QueryDB' it goes to the else statement and runs the function again, which will return a null value. I designed it this way becuase I wanted to check to make sure the database is open before I try to execute the SQL statement, and if it's not open, call the method to open it and run through the function again.
Public Function QueryDB(ByVal strSQL As String) As List(Of String)
Dim reader As SqlCeDataReader
Dim results As New List(Of String)
Using cmdAdd As New SqlCeCommand(strSQL, conn)
If Me.checkConnection Then
reader = cmdAdd.ExecuteReader()
Do While reader.Read
results.Add(reader.GetString(0))
Loop
Return results
Exit Function
Else
connectPlansdb()
QueryDB(strSQL) 'does not exit function when done and goes through the function again
End If
End Using
End Function
The second problem I have is when I try to populate the list into a combo box in my form class where I call out to the database and use the returned list to populate my combo box. I can't seem to figure out how to get the code to deal with the list.
Private Sub cmbInvestmentStrategy_DropDown(sender As System.Object, e As System.EventArgs) Handles cmbInvestmentStrategy.DropDown
Dim strat As New clsInvestmentStrategies()
Dim invStrat As New List(Of String)
invStrat = strat.getInvestmentStrategies() 'String cannot be converted to List(pf String)
cmbInvestmentStrategy.Items.Add(invStrat) 'Error 3 Value of type 'System.Collections.Generic.List(Of String)' _
'cannot be converted to '1-dimensional array of Object'.
End Sub
Any help would be greatly appreciated.
Thanks!
Your QueryDB method has a big flaw. If the database is unavailable (connectivity problems, database offline or wrong connection string), there will be an infinite loop. Your query DB method should just query the database. You could wrap it in another method responsible for connecting to the database, but you don't want to retry database connection infinitely.
As for your second method:
invStrat = strat.getInvestmentStrategies() 'String cannot be converted to List(Of String)
Error is pretty clear here. getInvestementStrategies returns a single String and cannot be converted to a list. It should return a List(Of String), or at least some collection of strings, I suppose?
cmbInvestmentStrategy.Items.Add(invStrat) 'Error 3 Value of type 'System.Collections.Generic.List(Of String)' _
'cannot be converted to '1-dimensional array of Object'.
Items.Add will add a single element to the combobox. You should loop through the invStrat values, and call Add for every item. Alternatively, you could use the AddRange method, but this method expects an Array, not a List.