Returning object from a function - asp.net

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

Related

Passing string list to F# asp.Net api

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)

Fortran Derived Type - Public Pointer to a Private Array

I'm trying to define a Fortran derived type that has a private allocatable array. However, I would like to be able to access the array via a public pointer for use in other modules. E.g.
type,public :: test
private
real,allocatable :: a(:,:,:)
contains
real,pointer,dimension(:,:,:),public :: point => a
end type test
I just get a compiler error when attempting it like the above.
Is this possible without writing a subroutine that does the pointing for me?
No.
The syntax error is perhaps because you have the pointer component in the type bound procedure part of the type definition (after the contains), not in the component part (before the contains).
Beyond syntax, there are some problems with what you want to do:
You cannot associate a pointer with a component of a type definition. Pointers can be associated with components of objects (a subobject). Similarly, you cannot associate a pointer with something that doesn't have the target attribute. Types and components of types can't have the target attribute. Variables of that type, or objects pointed at by pointer components of an object may have the target attribute.
You cannot associate a pointer with something that isn't allocated. If something isn't allocated then there isn't anything to point at.
An initializer for a pointer component cannot refer to something that is allocatable. In addition to the target attribute the thing that it refers to must have the SAVE attribute. As the case with the TARGET attribute, variables have the save attribute, not type or component definitions.
Associating a pointer with a component of an object may defeat the point of making the component private in the first place. This leads to the question - what are you trying to do?

Qt Script constructor function

I am reading through the Qt scripting documentation and came across this passage.
Note that, even though it is not considered good practice, there is
nothing that stops you from choosing to ignore the default constructed
(this) object when your function is called as a constructor and
creating your own object anyway; simply have the constructor return
that object. The object will "override" the default object that the
engine constructed
I am confused as to what this means. What it means by 'this' object and the constructor object. Does this mean it is favored to have a this object rather than having a constructor?
Could some please explain.
Let's take the example from the Qt documentation:
function Book(isbn) {
this.isbn = isbn;
}
The constructor Book() adds an isbn property to the this object, which is returned automatically (i.e. without an explicit return statement). However, you are free to return your own object from a constructor, e.g. you could do
function Book(isbn) {
return {isbn : isbn};
}
In the latter case, you ignore the this object, create a new object and return it instead.

Passing reference types by ref - Flex/Actionscript

I know that in C# when you pass an object (non primitive) to a method the following is true:
A reference to the object is passed
Changes made to the object in the method are reflected outside of the method.
Also, you can pass a reference to a reference in C# e.g this.changeObject(ref myObject);, in which case:
Changes to the object and also to the ref are reflected outside of the method e.g. myObject = new new List(); would change the passed objects referred location.
My question:
Is this possible to do in Flex/Actionscript - can a ref keyword be used?
No, you cannot. ActionScript doesn't have a ref keyword or a similar (double pointer like) concept. You always pass object references to functions (except for primitives) and modifications are reflected back.

Is the ASP.NET session data changed?

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.

Resources