How can I make enum variable's to be incremented in a certain Arithmetic progression?
For instance :
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
Now the values will be incremented by 1. How to increment the values lets say bye 2? Something like:
enum Days {Sat=2, Sun=4, Mon=6, Tue=8, Wed=10, Thu=12, Fri=14};
Do I have to do this manually for each and every element?
Yes, in practice you would have to do it manually -- but do you really need to do so?
Enums that are not decorated with FlagsAttribute should not need to have specific values assigned to each member because the primary purpose of an enum is to distinguish among a specific set of items, and not necessarily map those items to a specific value. Depending on what you need the numbers for, it is quite possible that using a mapping solution external to the enum would be better software engineering.
Theoretically you can also use a T4 template to do this programmatically; you would be writing a little code that generates the desired source code automatically from your own viewpoint, but this is overkill for such a simple scenario.
I would recommend staying away from this approach, where data is stored with enumerated values. Instead, keep the enum simple and use it for its designed purpose: a enumeration of named values. Then store data elsewhere, using something like an interpreter or mediator pattern to access and modify the data on the fly. One approach is to use a set or dictionary where the keys are the enum values and the values are your numbers.
Related
Trying to get 0.1500 to display as 15.00. My C# Property is a decimal and my xaml code is <TextBox Text="{Binding ClosingRate, StringFormat={}{0:P2}}"/> I want the format to move the decimal point 2 places to the right without having to change the decimal value nor have a leading 0. The "P2" format gets me close, but displays 20.00 %, and I don't want the percent sign.
You cannot do it by simple StringFormat. It provides you only with some predefined options with interesting corner cases very little extensibility. What you want to do is effectively *=100 your value and that kind is not supported directly neither by bindings nor by formats since that's arithmetic operation.
The fact that P/% actually also does *=100 is a probably just a coincidence. You could hack it to skip the % but it will not help you at all if in 3 days you have another place where you need to "move" it by a different number of digits.
The solution is ... to multiply the displayed value by 100. You don't need to alter the source value - it's just a matter of forcing the UI to read a different value than the source one.
Two easiest ways are:
You can do it either in ViewModel, simply by exposing a new property i.e. ClosingRateFormatted that will be read-only and that will return ClosingRate*100 (just remember about notifying about changes to both properties now)
You could use a IValueConverter and bind to ClosingRate as usual. Converter has 2 methods, Convert and ConvertBack and you can do *=100 in one, and /=100 in the other (if needed at all), and that will alter the value passed to/from the UI, but the source will not notice.
With Converter, no changes are needed in change-notifications (contrary to the first option), but using Converters is definitely a bit harder than just making a new readonly property.
I am doing a web project, And I met a problem that, I want to give a counter for each date. For example, if two records are created on June 22, then the counter for June 22 should be 2. And if one record is created on June 23, then the counter for June 23 should be 1.
I am thinking about using a dictionary to achieve this goal. I define a ASP.NET hidden field, and then assign a dictionary to its value in code behind. The date is the key, and the counter value is the value.
The code I have so far is:
<asp:HiddenField ID="hdnRefDictionary" runat="server" />
hdnRefDictionary.Value = new Dictionary<string,int>(); // Not working since cannot convert Dictionary to string.
Thanks in advance, and Have a good day!
Cheers,
Jiang Hao
consider using Json.Net as
Dictionary<string,string> dict=new ......//any dictionary
string json=Newtonsoft.Json.JsonConvert.SerializeObject(dict);
hdnRefDictionary.Value = data;//you hidden field
on post/desialize simple deserialize
Dictionary<string,string> dict=Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string,string>(hdnRefDictionary.Value);
please note if you put complex objects in dictionary you may have to override their ToString() method and/or handle serialization/Deserialization yourself using JsonConvertor
You have to serialize it manually. You can use LINQ to do it:
string data = String.Join(";", dict.Select(i => i.Key + "=" + i.Value.ToString()));
hdnRefDictionary.Value = data;
And then write the code to deserialize it later by splitting on ";" and building the key/value pairs based on the "=".
You can also use JSON serialization to do this. I use JSON.NET utility and it makes it really easy to serialize objects. However, JSON is a little bigger of a response output (not much, but a little) than using a querystring-like storage mechanism for what you are trying to do.
You'd want to use a hidden field if you want to manipulate the data on both the client and server; if you only need access on the server, then you'd want to use ViewState like #Imadoddin said, or some other mechanism like Cache, Session, etc. This is because it never gets output to the client (well ViewState does, but is encrypted) and is protected from anybody trying to change it.
By default hidden field cannot store any thing other than string. Since Dictionary is not a string that means you have to serialize it. That's a one face on coin because you have to de-serialize it again while accessing in Dictionary object. Since it is a bit tedious work I will recommend you to use ViewState which does all the serialization and de-serialization task internally. The syntax is quite same.
ViewState["yourKey"] = AnyObject;
If still you want to use hidden field then Brian Mains' answer is perfect for you.
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.
I am thinking a library already exists for this, but I need allow my users to create a numbering format for their documents.
For example, let's say we have an RFI from and the user has a specific format the numbering sequence needs to be in. A typical RFI number looks like this for their system: R0000100. The next RFI in line would be R0000101.
Before I set out to creating a formatting engine for numbers such as these, does something already exist that can accommodate this?
Update:
I failed to save the edit to this question. Anyway, I also want to give the users the ability to create their own formats. So, I may have a form where they can input the format: R####### And also allow them to specify the starting integer: in the case 100. Also, I may want to allow them to specify how they want to increment. maybe only by 100s. So the next number may be R0000200. I know this may sound ridiculous, but you never know. That is why I asked if something like this already exists.
If you keep value and format separated, you won't need a library or such a thing.
The numbers would be simple, say, integers i, i.e. 100, 101, 102, that you manage/store however you see fit. The formatting part would simply be a matter of R + i.ToString("0000000"), or if you want to have the format as a string literal string.Format("R{0:0000000}", i).
I know, this might only be an example, but as your question stands, the formatting options, that .NET provides out of the box seem to suffice.
The incrementing of identity field values is most often handled in an RDBMS-style database. This comes with a few benefits, such as built-in concurrency handling. If you want to generate the values yourself, a simple class to get the last-issued value and increment by one would be very easy to create. Make it thread-safe so you don't get any duplicates or gaps and you'll be good to go.
I have this exported file of some weird (standard for this industry!) format, which I need to import into
our Database. The file basically looks like this:
DATRKAKT-START
KAKT_LKZ "D"
KAKT_DAT_STAMM "1042665"
DATRKAIB-START
KAIB_AZ "18831025"
KAIB_STATUS_FM 2
KAIB_KZ_WAE "E"
DATRKAIB-END
DATRKARP-START
KARP_MELD "831025"
KARP_ST_MELD "G"
DATRKARP-END
...
DATRKAKT-END
There are 56 sections with a total of 1963 different rows, so I'm really not into
creating 56 classes with 1963 properties... How would you handle this file
so that you can access some property like it were an object?
Datrkaib.Kaib_Status_Fm
Datrkarp.karp_St_Meld
Unless your programming language lets you add methods to classes at runtime or lets classes respond to calls to undefined methods, there's no way you can do this. The thing is, even if C# did let you do this, you would lose type safety and Intellisense help anyway (presumably among the reasons for wanting it to work like that), so why not just go ahead and read it into some data structure? My inclination would be a hash which can contain values or other hashes, so you'd get calls like (VB):
Datrkakt("Lkz")
Datrkakt("Dat_Stam")
Datrkakt("Kaib")("Az")
Datrkakt("Kaib")("Status_Fm")
Or if you know all the data items are uniquely named as in your example, just use one hash:
Datr("Kakt_Lkz")
Datr("Kakt_Dat_Stam")
Datr("Kaib_Az")
Datr("Kaib_Status_Fm")
You can get back Intellisense help by creating an enum of all the data item names and getting:
Datr(DatrItem.KAKT_LKZ)
Datr(DatrIrem.KAIB_STATUS_FM)
It looks like structured data - I'd run search and replace and convert it to a simple xml. and then import.
The if you want to generate a code file off of it - consider codesmith - I think it can do this.
I'd go with a List <name, list> of various object, that can be a tuple <name, value> or a named list of object.
There isn't that will automatically do this for you.
I would create a class containing all the appropriate properties (say DatrDocument), and create a DatrReader class (similar idea to the XmlDocument/XmlReader classes).
The DatrReader will need to read the contents of the file or stream, and parse it into a DatrDocument.
You may also want to write a DatrWriter class which will take a DatrDocument and write it to a stream.