I have a session variable that i create dynamically, so lets say for instance i have the following session variables
Session["area1"]
Session["area2"]
Session["area3"]
Session["area4"]
And in each of those sessions i have a List in there
then i have this in order to get what the name will be and use it in my code
string areaName = "area" + Session["area"];
Session["area"] is a session variable that increases it self based on how many times a button is clicked
Now if i try to pass areaName to a function that is requiring a List as a parameter it doesn't let me do it, even though the value in that session variable is a list
I am only using "areaName" to be able to get the name of the session
How can i use it in order to pass it to function that is requiring a List type?
If you know it contains a list, then do the cast. For instance
myMethod(Session["area1"] as List<string>);
By calling the following:
string areaName = "area" + Session["area"];
You are saying, take the string "area", and append to that the variable from Session (which is an object by default), which will call it's ToString() method. Your end result here, is a string, not a list. (And not a meaningful one at that).
All you need to do is take the Session value, and cast it to the type that you know it is.
method((List<string>)Session["area"])
or
method(Session["area"] as List<string>)
The reason you can't just pass in Session["area"] without first casting, is because session returns your variable as an object. Sure, it may be a list in memory, but the system treats it the same as any other object (Look up Polymorphism for more info), which is not the List that it expects. Thus, you will get a compile time error, unless you cast it to the correct type.
Related
I actually solved my problem before posting, but I wonder if there are any better solutions?
Also if there is somewhere where there is a way to use list as-is?
I am writing a simple get endpoint if F# which needs to accept a list of strings as an argument.
I take that list as the input to a query that runs as expected, I am not worried about that part.
The problem I am facing is as follows (minimal implmenetation):
When I define the endpoint as:
[<HttpGet>]
member _.Get() =
processStrings [ "test"; "test2" ]
it returns as expected.
When I change it to:
[<HttpGet>]
member _.Get([<FromQuery>] stringList: string list) = processStrings stringList
I get an error:
InvalidOperationException: Could not create an instance of type 'Microsoft.FSharp.Collections.FSharpList`1[[System.String, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Record types must have a single primary constructor. Alternatively, give the 'stringList' parameter a non-null default value.
Which doesn't make much sense to me, as I am using a list of strings, which in C# at least defaults to an empty list.
So I assume this comes down to how C# and F# interpret these signatures, but I am not sure how to resolve it.
I tried this signature and received the same error as above....
member _.Get( [<Optional; DefaultParameterValue([||]); FromQuery>] stringList: string list) = processStrings stringList
In the end using the following did solve the problem.
member _.Get( [<Optional; DefaultParameterValue([||]); FromQuery>] stringList: string seq) = processStrings stringList
I assume it was solved because seq is just IEnumerable, and then presumable list isn't just List from C# (mutable vs immutable). But is there a way to use an F# list in [FromQuery] parameters? Would [FromBody] have just worked? (No is the tested answer) Is there a way to add a type provider for an endpoint like this?
Is there something else I am missing here? Mine works now, but I am curious to the implications of the above.
I have not tested this, but I would assume that ASP.NET does not support F# lists as arguments. I would guess that taking the argument as an array would be much more likely to work:
[<HttpGet>]
member _.Get([<FromQuery>] stringList: string[]) =
processStrings (List.ofArray stringList)
If an object is created inside a function and the function returns that type of oject how is the memory handled.
Example:
Public Function GetEmployee(employeeid as integer) as employee
Dim oEmployee as new employee
oEmployee.FirstName="Bob"
...
...
return oEmployee
end function
Does the variable that receive the object still a pointer to the memory location that was used inside the function?
What about when you do a oEmployee2=oEmployee
Is oEmployee2 just a pointer? And any changes to oEmployee will now affect the other. Just trying to understand it from a memory perspective and how that scope works
Thanks
Assuming employee is a reference type (e.g. any class) the method will return a reference (similar in concept to a pointer in unmanaged languages) to the object instance (usually on the heap). Since only one object instance exists, all changes to it will affect the instance.
If employee is a value type (e.g any struct or primitive type) a separate copy of the instance is returned.
Assuming oEmployee is a reference type (not a struct), if you pass it as an argument, then you are passing the reference. In .NET you should think in terms of Reference types vs Value types.
This article really helped me understand how memory is allocated when I was starting out.
http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx
I define a variable which is dynamicaly changes based on the user interactions, for example ID of an object sets to variable when user touches on it. After the ID sets I call a function in a custom component which is related to that object. Like this;
activeObject.videoPlay(event) ---> if the activeObject is video1 ---> video1.videoPlay(event) function will be called.
I tried several variable types when defining the variable activeObject, such as String , Array but didnt work out. By the way the data set to variable is String. When I use String type it gives this error;
Error #1061: Call to a possibly undefined method videoPlay through a reference with static type String.
Is there any way to use a string as a dynamic variable?
Is there any way to use a string as a dynamic variable?
Bracket notation -- obj["dynamicPropertyName"] as Type or in your case (activeObject['videoPlay'] as Function).apply(abc, [event]); You'll obviously want null object checks etc.
List<Foo> fooList = Session["foo"] as List<Foo>;
fooList.Add(bar);
Does the call to Add() change the data that's in the session? Put another way: when I next pull "foo" from the Session, will the list contain bar?
Yes the session will be changed as a List<T> is a reference type. All that this fooList variable represents is a pointer to the real object and all that Session["foo"] represents is also a pointer to the same object. So changing fooList will affect the real object that the session is also pointing to. The behavior will be different if you store value types in session.
I'm using a compiled .dll provided by someone else -- I know little about it, other than it has a session variable that I must access in a way that is strange to me. Not sure what to call it -- have googled for words that I thought might be right, but so far no success. Here is what it looks like:
Session("receipt").username
It's the .username part that I don't understand. What is it? How is it created?
Thanks for any help.
Session is probably a global object which has a default property which returns a SessionItem object. The SessionItem object is loaded from the browser-session (probably) by the Session object. The SessionItem object has a property username, which is a value stored somewhere in the browser-session.
Some code to clear things up:
Public Class Session
Private Items As SessionItemCollection
Default Public ReadOnly Property SessionItem(ByVal id As String) As Object
Get
Return Me.Items.Find(id)
End Get
End Property
End Class
And you calling the code (Which searches for the "receipt" item in the SessionItemCollection Items from Session):
Session("receipt")
My first guess (since there isn't much other code to go off of) is that the object being stored in the session variable and accessed via Session("receipt") is of a class that contains a property or member called username which you are accessing in that fashion.
The basic idea is that Session("receipt") will pull back whatever this object is (for the sake of instruction we will say it is a receipt object) and the .username is referencing the username property of that receipt object.