- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Reply to: C# Enum Question
Collapse
You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:
- You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
- You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
- If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
Logging in...
Previously on "C# Enum Question"
Collapse
-
I didn't have the knowledge of this topic before, now i get some information of C# Enum
-
Originally posted by eek View PostSadly that's theory and not practice. While compile time (and intellisense) will pick up 99% of the issues it doesn't catch all of them. 1 prime example is the mongodb c# classes where implicit types are really useful if bob doesn't want to try and understand what the system is trying to do and simple wants to insert some records.
It falls apart however when the class library supports multiple interfaces and those implicit types have allowed bob to call an interface with a parameter set with inherent contradictions.
Granted its not an issue you may encounter in day to day usage but its blooming annoying when you discover that your data is being randomly corrupted by rogue data.
or something like
IEnumerable<ISomething<IDictionary>>
(or even worse)
The choice is yours.
Leave a comment:
-
Originally posted by DimPrawn View PostSeems pretty clear you don't understand what var means and think it is some type of variant or runtime determined type assignment.
The type is determined at compile time and not at runtime, hence any dodgy conversion attempts will be picked up at compile time.
1 recent example is the mongodb c# classes where implicit types are really useful if bob doesn't want to try and understand what the system is trying to do and simple wants to insert some records.
It falls apart however when the class library supports multiple interfaces and implicit types have allowed bob to call an interface with a parameter set full of inherent contradictions.
Granted its not an issue you may encounter in day to day usage but its blooming annoying when you discover that your data is being randomly corrupted by rogue data.Last edited by eek; 30 September 2011, 11:53.
Leave a comment:
-
Originally posted by eek View PostWhich is fine up to the point that something decides to throw a wobbly. I've no problem with generic types for throwing objects around but I do when bob then does an implicit conversion to a particular type without doing any checks beforehand to see if the conversion is plausible or doable.
The type is determined at compile time and not at runtime, hence any dodgy conversion attempts will be picked up at compile time.
Leave a comment:
-
Originally posted by Spacecadet View PostThe example here used var to do something you wouldn't normally do - that is loop through an enum collection. Although the Var type can be replaced with "lights"
Var and object types have their use in writing generic methods which can take any object or data type as an input.Last edited by eek; 29 September 2011, 09:45.
Leave a comment:
-
To get the names you just need to use Enum.GetNames(typeof(EnumType)) which returns a string array containing each name.
eg:
Code:foreach(string name in Enum.GetNames(typeof(Lights)) { MessageBox.Show(name); }
Leave a comment:
-
Originally posted by eek View PostThats the problem with using var(s) (implicitly typed variables) which personally is lazy programming.
I really wish Microsoft remembered that the .net framework has always been really good at guessing what a type a variable is up to the point it screws up. And when it screws up it will do it randomly in style.
Personally all I want to see is code written in such a way that so that it copes with most potential errors. Everytime I see var being used I do wander / how will it blow up in my face.
Var and object types have their use in writing generic methods which can take any object or data type as an input.
Leave a comment:
-
Originally posted by Spacecadet View PostThanks Oliverson
Had to change it slightly though as I was just getting the int value:
Code:foreach (var light in Enum.GetValues(typeof(Lights))) { String s = light.ToString(); MessageBox.Show(s); }
I really wish Microsoft remembered that the .net framework has always been really good at guessing what a type a variable is up to the point it screws up. And when it screws up it will do it randomly in style.
Personally all I want to see is code written in such a way that so that it copes with most potential errors. Everytime I see var being used I do wander / how will it blow up in my face.
Leave a comment:
-
Thanks Oliverson
Had to change it slightly though as I was just getting the int value:
Code:foreach (var light in Enum.GetValues(typeof(Lights))) { String s = light.ToString(); MessageBox.Show(s); }
Leave a comment:
-
Originally posted by Spacecadet View PostQuick question about Enums
I need to convert the "integer" value of an enum to a string value.
Is there a simple way of doing this
e.g. if
Code:public enum test {val1, val2, val3} public string str
{
public enum Lights
{
Red = 10,
Amber = 20,
Green = 30
}
class Program
{
static void Main(string[] args)
{
foreach (var light in Enum.GetValues(typeof(Lights)))
{
var s = ((int)light).ToString();
Console.WriteLine(s);
}
Console.WriteLine("Press Any Key");
Console.ReadKey();
}
}
}
Leave a comment:
-
C# Enum Question
Quick question about Enums
I need to convert the "integer" value of an enum to a string value.
Is there a simple way of doing this
e.g. if
Code:public enum test {val1, val2, val3} public string str
Tags: None
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- Streamline Your Retirement with iSIPP: A Solution for Contractor Pensions Sep 1 09:13
- Making the most of pension lump sums: overview for contractors Sep 1 08:36
- Umbrella company tribunal cases are opening up; are your wages subject to unlawful deductions, too? Aug 31 08:38
- Contractors, relabelling 'labour' as 'services' to appear 'fully contracted out' won't dupe IR35 inspectors Aug 31 08:30
- How often does HMRC check tax returns? Aug 30 08:27
- Work-life balance as an IT contractor: 5 top tips from a tech recruiter Aug 30 08:20
- Autumn Statement 2023 tipped to prioritise mental health, in a boost for UK workplaces Aug 29 08:33
- Final reminder for contractors to respond to the umbrella consultation (closing today) Aug 29 08:09
- Top 5 most in demand cyber security contract roles Aug 25 08:38
- Changes to the right to request flexible working are incoming, but how will contractors be affected? Aug 24 08:25
Leave a comment: