Allowing null in int text box with non nullable int - c#

So there are very similar questions about this, but none seem to solve what I'm trying to do, and maybe it's not possible. I have a form with a number of text boxes bound to int properties in my model. We allow the user to clear the field, and it should remain cleared. CUrrently if they do this they get a validation error that the it can't convert "". I understand why this is, however, due to the design I can't use int?, which I know would solve my problem. Is this possible to do, without having to define a string property to bind to? I'm trying not to duplicate all my int properties as string properties, but maybe that's the road I need to take.

What you would like to do when the string is empty?
I mean do you want to have some int value in prop if textbox is empty? If yes, simply create a converter that converts null value to that value and assign to Binding

You need to be able to represent an empty int.
This is a dirty hack but if you chose a number say -999 that will never be valid you could use a custom converter to convert and convert back between "" and -999.
It will probably achieve what you want but I would not recommend it.

Related

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.

How to add a text item to a combobox in C#

I am new to C# from Delphi. In Delphi, TCombobox.Add takes a single, string parameter. Thus is it very simple to add a string item to a combobox.
C# confused me because Combobox.Items.Add takes a single, object parameter. There are some circumstances in which I cannot add a string var to the list of items. (I haven't identified a pattern yet; it might be if it's a property).
Googling was fruitless until...
I had the same problem in an WPF application, using VB.Net I got Component.Items.Add("String") to work, my guess is the string will automatically be cast to an suitable object, try to explicitly cast string in your case(not sure to what).
You can also try to assign your string to be the caption of a label and add that label to the control.
I have asked several times for this question to be deleted because it is of no value to anyone. I wrote it when I was confused and bewildered and didn't know what I was doing.
... I found http://www.codeproject.com/Questions/416859/Csharp-Filling-ComboBox-with-an-SQL-table-column, which said
comboBox1.Items.Add((string) problematicVar); //problematicVar is a string
So I have to cast to string.
I've added this self-answered question because I wish it had existed half an hour.

Values storable in Excel

Good evening!
Which types of values can be directly stored into an Excel worksheet using Range.Value2 and how do I quickly check if a particular value can?
Suppose I have an array of objects, perhaps multityped (e.g. one int, one double and one Foo stored in an object[]).
If I shall choose a range of width 3 and try to store this array using Range.Value2, this will result in an exception (of course Excel doesn't know what is a Foo).
I came up with an idea of checking each value in the array, and, if it's not storable, convert it to its string representation using ToString(). But how do I check if it's initially storable?
It would be horrible to end up doing something like that:
public bool storable<T>(T value)
{
return value is int ||
value is uint ||
value is short ||
value is byte ||
...
value is string;
}
...especially knowing that each is will cast the variable to the tested type and seriously affect performance.
On the other hand, I can't afford pre-casting each value to the string type as I sometimes want to be able to do graphs and diagrams with numeric values, not strings.
Can you tell me I am mistaken or offer me any solution to the problem?
Thank you!
I think you're going to have to do what you're unkeen to do (all the "is" checks), unless you can somehow make your input array a bit more strongly typed. Your best bet might be just to order the casts such that the most common ones get hit first.

Is there any way for Validating cell value in excel (vsto)

Suppose i have some value in excel cell,its type may be anything date,numeric or string
and i want that cell to be validated against its type...
Is there any way of doing this ?
thanks in advance..
I assume you mean that you have contents in a cell, which could be either a date, a double, or a plain string, and that you want to assert what corresponding .NET type it is. I believe there is no direct way of doing this. One approach would be to retrieve the Value2 in the cell, and try to cast it to each of these types, starting from the most restrictive one, until the cast works - i.e. DateTime (DateTime.FromOADate), then double (Convert.ToDouble), then string - and then apply the validation rule that applies to the particular type you found.
I am not quite sure what you mean by validation, though, and what that would buy you. Once you know the type of the content, what would you do with it?
I use Excel's data validation technique in a VSTO application myself. It obviously isn't VSTO but it works well enough. There are some drawbacks like you can't have multiple validations on the same cell (i.e. you have to know what your validating for)
You can directly use the excel's functionality !!!
no need of any coding for that !!
instead of hard work go for the smart work ...
In excel,
Go to the cell where you want the validation to come
Menu ->> Data --> validation
Here you can directly use the various excel validation terms.
Which you can refer here or..
http://support.microsoft.com/kb/211485
Thanx
Regards.

Categories