Question about reference types - c#

This is taken from Jon Skeet's excellent personal C# site (http://www.yoda.arachsys.com/csharp/):
StringBuilder first = new StringBuilder();
StringBuilder second = first;
first.Append ("hello");
first = null;
Console.WriteLine (second);
1) Changing the value of first will not change the value of second -
2) although while their values are still references to the same object, any changes made to the object through the first variable will be visible through the second variable.
This is taken from the same sentence. What is meant by changing the value? I assume the value of a variable (eg int x = 4, or 5, or 45, etc).
Does this mean if first points to another compatible object, it won't have an effect on two?
Everything on that page makes sense, I think it's just an issue with my interpretation of the English.
Thanks

first is a reference to an object of type StringBuilder. That is, first stores a value that can be used to refer to an object on the heap that is type of Stringuilder. second is another reference to an object of type StringBuilder and its value is initially set refer to the same object that first is referring to.
If you change the value of first what you are doing is changing what the referent is. That is, you are using first to refer to a different object. This does not impact second; its value is unaffected by changes to the value of first. (Remember: the value of first and second are references that initially have the same referent. But just like with
int x = 1;
int y = x;
x = 2;
does not change the value of y, changing the value of first does not change the value of second.
On the other hand, when first and second refer to the same object, any changes to that object will be visible through both first and second.
Think of it like this. Let's say I create a text file first.html whose contents are
Stack Overflow
and I issue the command copy first.html second.html. Then both pages can be used to refer to the same webpage; by following the link we arrive at the same referent. If changes are made to the Stack Overflow home page, then accessing the homepage through either first.html or second.html will allow me to see those changes. But if I then change the contents of first.html to be
<a href="http://www.thedailywtf.com>The Daily WTF</a>
then I can no longer use first.html to refer to the Stack Overflow homepage. Moreover, this change does not impact the value of second.html. It is still
<a href="http://stackoverflow.com>Stack Overflow</a>
Think of the contents of these files as the values of a reference type, and the ultimate desination as the referent object.

The difference between the value of the object itself and the contents of the object are not clear.
For example, it is possible to change the contents of second by calling methods on first, as in the call to Append in the example. However, setting the value of first to null does not set second to null.
You can see this easily by writing this code and stepping through it in a debugger.

Related

particleSystem.startSize vs particleSystem.main.startSize

The current solution that works is
gameObject.GetComponent<ParticleSystem>().startSize = 1
but VStudio 2017 reports that startSize is obsolete and should be replaced with main.startSize. But when I do that, I get an error:
Cannon modify the return value of ParticleSystem.main because it is not a variable.
So how should I set the start size value?
ParticleSystem.main returns a struct, not a class. Struct's are value types, in that when you move them around their values are copied into a new object. You have to first store the returned value in a variable then modify it.
var main = particleSystem.main;
main.startSize = 1;

How to get Test variable through custom code in HP UFT - ApiTest

I want to set value to the Test Variable using Custom Code.
Can you tell me how to do it as i am not able to access the variable from code.
I need to access the User Variable URL in my custom code and set the value.
Please help me if you know how to do it using Custom Code.
Thanks,
Madhan
Based on your question:
1. Click on File menu and then Settings, it will open Properties pane.
2. Click on "+" to add user variable and give a name.
3. In your custom code, type below code:
string s = "https://www.google.com/";
this.Context.TestProfile.SetVariableValue("NameOfYourVariable",s);
To retrieve the value of given variable:
string ss = this.Context.TestProfile.GetVariableValue("NameOfYourVariable");
CodeActivity5.Report("Variable is : ", ss); //(This line will print your variable value)
answer provided here is valid when the need is to set string values. The "SetVariableValue" method takes two "String" type parameters. This limits the ability to set Int32 type variables. Obviously, C# throws an error when trying to set an integer value.
Now, an integer value is particularly useful while setting values for loop iterations. I am not sure whether this is a limitation of the tool or whether my lack of knowledge. So, to work around this, i used the output property of custom code activity. To do this, create a custom code activity and create an output property of the desired type, say Int. Now, assign a value to this output property using the line:
this.ActivityName.Output.property name = property value
This is available in UFT help and can be useful while trying to pass values other than string between different activities in a flow.

What's the difference between Entity.Attributes and Entity.FormattedValues?

I'm learning how to write custom workflows and am trying to figure out where and in which format all the values I need are stored. I have noticed that I can access Entity instance data in both the Attributes and FormattedValues properties. How do I know when to use which one?
I have noticed MSDN's remark "Entity formatted values are only available on a retrieve operation, not on an update operation.".
For testing I've made two foreach-blocks iterating through both collections. Attributes gives me 65 lines and FormattedValues gives me 39. I can see that, yes, the output from FormattedValues is indeed formatted.
For example, where Attributes gives the output "Microsoft.Xrm.Sdk.OptionSetValue", FormattedValues gives me a string with the actual value.
Which values/attributes are generally excluded from the FormattedValues collection and why?
I'm not 100% sure about this, but the formatted values are the values you will be able to see on the form. In that list you will be able to find money types with the $ symbol, or the labels of the option sets. A text field shouldn't be shown since is already human-readable.
https://community.dynamics.com/crm/b/crmmitchmilam/archive/2013/04/18/crm-sdk-nugget-entity-formattedvalues-property.aspx
Refer to this article to know a little bit more about it. I rareley using that attribute list since the data is in string format. I found it really useful to retrieve the OprionSet lables.
After a quick check it'd appear that the difference between an attribute and a formatted value is that the former to the actual value stored in the DB (or at least the value that was stored there at the occasion of fetching it) while the latter serves what's shown to the user.
I haven't used formatted values but until proven otherwise, I'd say that an entity's attribute will provide you with the typed value that the regarded field is based on (be that int, DateTime and such), whereas its formatted value is the rendered, stringified representation of that value (being dependent on e.g. what form you're referring to, what language etc.)
By that logic, I'd expect the set of formatted values to be a subset to the set of attributes. Also, it should be constituted of exclusively String typed values, while the counterpart is a member of the type conversion table.
An example of the difference I can think of is an option set called picky with the currently selected option named "hazaa" and the ID of 1234. The below sample is written by heart so feel free to correct. It exemplifies the point, though: plainValue will be an integer equal to 1234, while formattedValue will be "hazaa".
int plainValue = (int)entity["picky"];
String formattedValue = (String)entity.FormattedValues["picky"];
I'd say that the attributive approach is more reliable as it'll render the actual values, while the alternative can lead to unexpected outcome. However, there's a certain convenience to it, I must add.
Personally, I'd recommend to look into the method GetAttributeValue<T>(String) or, as every cocky CRM developer would - have your own class with extension methods and use the method Get<T>(T,String) in there. That provides you with the sense of control and offers the best predictability and readability, IMAO.

Replace values located close to themselves with the mean value

I'm not sure if SO is the proper place for asking this, if it's not, I will remove the question and try it in some other place. Said that I'm trying to do the following:
I have a List<double> and want to replace the block of values whose values are situated very close (say 0.75 in this example) to a single value, representing the mean of the replaced values.
The values that are isolated, or alone, should not be modified.
Also the replaced block can't be longer than 5.
Computing the mean value for each interval from 0, 5, 10.. would not provide the expected results.
It happened many times that LINQ power surprised me gladly and I would be happy if someone could guide me in the creation of this little method.
What I've thought is to first find the closest values for each one, calculate the distance, if the distance is less than minimum (0.75 in this example) then assign those values to the same block.
When all values are assigned to their blocks run a second loop that replaces each block (with one value, or many values) to its mean.
The problem that I have in this approach is to assign the "block": if several values are together, I need to check if the evaluating value is contained in another block, and if it so, the new value should be in that block too.
I don't know if this is the right way of doing this or I'm over complicating it.
EDIT: the expected result:
Although you see two axes only one is used, the List is 1D, I should have drawn only the X axis.
The length of the lines that are represented is irrelevant. It's just to mark on the axis where the value is situated.
It turns out that MSDN has already done this, and provided an in-depth example application with code:
Data Clustering - Detecting Abnormal Data Using k-Means Clustering

C# immutable int

In Java strings are immutable. If we have a string and make changes to it, we get new string referenced by the same variable:
String str = "abc";
str += "def"; // now str refers to another piece in the heap containing "abcdef"
// while "abc" is still somewhere in the heap until taken by GC
It's been said that int and double are immutable in C#. Does it mean that when we have int and later change it, we would get new int "pointed" by the same variable? Same thing but with stack.
int i = 1;
i += 1; // same thing: in the stack there is value 2 to which variable
// i is attached, and somewhere in the stack there is value 1
Is that correct? If not, in what way is int immutable?
To follow up on Marc's (perfectly acceptable) answer: Integer values are immutable but integer variables may vary. That's why they're called "variables".
Integer values are immutable: if you have the value that is the number 12, there's no way to make it odd, no way to paint it blue, and so on. If you try to make it odd by, say, adding one, then you end up with a different value, 13. Maybe you store that value in the variable that used to contain 12, but that doesn't change any property of 12. 12 stays exactly the same as it was before.
You haven't changed (and cannot change) something about the int; you have assigned a new int value (and discarded the old value). Thus it is immutable.
Consider a more complex struct:
var x = new FooStruct(123);
x.Value = 456; // mutate
x.SomeMethodThatChangedInternalState(); // mutate
x = new FooStruct(456); // **not** a mutate; this is a *reassignment*
However, there is no "pointing" here. The struct is directly on the stack (in this case): no references involved.
Although this may sound obvious I'll add a couple of lines that helped me to understand in case someone has the same confusion.
There are various kinds of mutability but generally when people say "immutable" they mean that class has members that cannot be changed.
String is probably storing characters in an array, which is not accessible and cannot be changed via methods, that's why string is immutable. Operation + on strings always returns a new string.
int is probably storing it's value in member "Value" which anyway is inaccessible, and that's why cannot be changed. All operations on int return new int, and this value is copied to variable, because it's a value type.

Categories