Instantiate Local Variable By Value? - asp.net

I sort of understand why this is happening, but not entirely. I have a base class with a Shared (Static) variable, declared like so:
Public Shared myVar As New MyObject(arg1, arg2)
In a method of a derived class, I set a local variable like so:
Dim myLocalVar As MyObject = myVar
Now when I do something like myLocalVar.Property1 += value, the value in Property1 persists to the next call of that method! I suppose I get why it would be happening; myVar is being set by reference instead of by value, but I've never encountered anything like this before. Is there any way (other than my workaround which is to just create a new object using the property values of myVar) to create myLocalVar by value?

When you create myLocalVar you are creating a new reference to the same shared object. If you truly want a local copy of the shared instance you will need to create a true copy.
This is done by either cloning the instance or with a copy constructor on the type that allows you to create a copy of the instance. This is not as simple as it sounds, however, due to the differences between deep and shallow copying and a cloned or copied instance could create similar problems for you if the property you are accessing is simply a shallow-copied reference to the same instance that the property on the original instance is referencing.
The best thing I to do in this case is to create a local copy of only the parts of the shared instance that you need, rather than copying the entire object graph. This means create a local copy of whatever type Property1 is and using that.

Related

Qt Proxy with type of source object

I have class, say MyClass, inherited from QSqlRelationalTableModel and I need to transpose it (change rows with columns).
This solution was found.
Is it possible to get transposed object of type MyClass after using the proxy? If not, are there any other ways to do it?
Thank you!
EDIT
I wanna use is like this:
MyClass* myObject = new MyClass(this, db);
TransposeProxyModel* trans = new TransposeProxyModel(this);
trans->setSourceModel(myObject);
ui->tableViewDb->setModel(trans);
ui->tableViewDb->setItemDelegate(new QSqlRelationalDelegate(ui->tableViewDb));
It is necessary to paste object of MyClass (or QSqlRelationalTableModel) into QSqlRelationalDelegate.
(edited as per question clarification)
Why do you need to pass the source model into the QSqlRelationalDelegate? If you use the stock delegate then the delegate needs to operate on top of data that it visualizes (data from TransposeProxyModel).
If you have a custom subclass of QSqlRelationalDelegate and you want to access the source data from there for any reason, then you can pass myObject as a second argument to delegate constructor, or you can access it through QModelIndex::model() methods of the index passed to delegate's methods.

How to store object types and create objects using that information only

I would like to store MetaObjects in a list like this:
myList = QList<QMetaObject>();
myList->append(MyClass::staticMetaObject);
myList->append(MyOtherClass::staticMetaObject);
I want to keep track of these object through out the application but I don't wish to allocate them just yet. By adding some information in my classes I will be able to use the MetaObject function "classInfo(int).value()". I use this when I store them in a QListWidget. When a row in the QListWidget is pressed I would like to be able to create an object of that specific kind that is stored in the list.
(Also have to add that all the classes dervies from the same baseclass)
This sample code describes a bit of what I want to do, except in his example, you add the classes as you go along.
http://lists.qt.nokia.com/pipermail/qt-interest/2012-January/037204.html
I read through the manual and when I try things like:
MyBaseClass *test = qobject_cast<MyBaseClass*>myList->at(i).newInstance();
The project compiles but when I try to print the "test" object its null. What am I doing wrong? And is this the best way of doing this?
Was also looking at MetaType, but where would i be able to store, for example a string for the menus if I'm not allowed to create the object? Would this be a nicer solution if I have a static function that returns a string?
Edit:
I now changed so the constructors are Q_INVOKABLE which solved the problem where "test == null".
But what are the downside of this solution? Should I just use a object factory (the old fashion way with a switch case)?

Returning object from a function

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

Create an array in windows workflow foundation

Is it possible to create a new array in windows workflow? More specifically, in the designer.
I've created a variable of System.Int32[], but when I use it I get a NullReferenceException.
I've tried New Int32(5), and various permutations of Dim - nothing I have tried has worked.
I was able to create the array and pass it as an in/out parameter - this works, however the workflow will need to determine the actual size of the array.
To create and instantiate an array, you have to set a default value to your variable with New Int32(FOO SIZE){} or use an Assign activity to instantiate it with the correct size in runtime
You can also use List(Of T) or any other .NET collection structure to achieve dynamic size.
Note that the value must be the right part of a set expression. So, you can google how to do it in VB.NET and you will be fine.
I assume that if you are creating the array in the designer, as you stated, it is either a workflow variable or a workflow argument. The "WF" way to do this would be to use the "Default Value" column under the "Variables" and/or "Arguments" tab.
If it is an argument then the Default Value column only works if the Direction is "In". If your argument is a property, or an Out, or In/Out direction then you would have to use the method mentioned by Davi.
If you are creating it under the "Variables" tag then using the Default value column would be the more built-in approach. The syntax in the default column would be the same syntax mentioned by Davi: New Int32(FOO SIZE) {}

Where can I manage Server Objects?

How do I link an odbc object to a stored procedure I have written. Please see example below:
The following code executes a stored procedure called DEPT_Add, yet the name of the object function is AddDepartment.
Set oDept = Server.CreateObject("JTQTMS.JTDept")
bReturn = oDept.AddDepartment(CStr(sDeptName))
My question is if I add a new stored procedure called PROPERTY_Add for example, how would I go in and add an object function called AddProperty.
The below code does not seem to be working because I cannot find out where to create and link the AddProperty method to my stored proc PROPERTY_Add.
Set oProp = Server.CreateObject("JTQTMS.JTProperty")
bReturn = oProp.AddProperty(CStr(sDeptName), CStr(sDeptDescription))
This looks to me like JTQTMS.JTDept is a class of a COM component.
The class being JTDept, and the COM component being JTQTMS.
Look in Component Services (if i've remembered correctly), and see if you can find any references to it there.
I believe what is happening is the AddDepartment method of the COM component is calling the Dept_Add stored procedure internally.
To do the same with Property__Add, you'd need to add the AddProperty method to the COM component, which in turn calls the Property_Add stored procedure.

Resources