Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.15/5 (5 votes)
See more:
what is wrong with this code?


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace enumform2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        enum Grade { Low = 1, Medium = 2, High = 4, Maximum = 8 }
        [Flags]
        enum GradeFlags { Low = 1, Medium = 2, High = 4, Maximum = 8 }

        private void button1_Click(object sender, EventArgs e)
        {


            string iGrade = (Grade.Low Grade.High ).ToString(); 
            string fGrade = (GradeFlags.Low GradeFlags.High).ToString();
            MessageBox.Show(iGrade + " - with flag : " + fGrade);




        }


    }
}


What I have tried:

from What does the [Flags] Enum Attribute Mean?[^]
Posted
Updated 10-Nov-16 12:54pm
Comments
Clifford Nelson 10-Nov-16 18:52pm    
In know that
string iGrade = (Grade.Low Grade.High ).ToString();
is not valid syntax in C# 6.0. No easy way to do what you are trying to do
[no name] 10-Nov-16 18:57pm    
Well that section of the article you linked to is talking about bitwise operations so what is it that you think is missing there?
Patrice T 10-Nov-16 22:33pm    
Harpreet05Kaur 11-Nov-16 2:31am    
Not able to understand the question at all.
Philippe Mori 11-Nov-16 15:08pm    
[Flags] attribute in mainly informational. However, in a case like the one above, ((Grade)7).ToString() would write 7 as it simply display the actual number when the value is not in the enumeration.

However, ((GradeFlags)7).ToString() would display Low, Medium, High instead.

In practice, one would use Flags when values correspond to bits and are mostly independent.

In your case, it generally does not make much sense to do that as you typically don't want to combine Grades together.

Also, it would make it harder to compute a grade average as the value are not representative. Values should really be 1, 2, 3, and 4.

1 solution

No idea of what you are trying to do. Nothing really makes sense. However, here is how you can compare:

class Program
	{

		enum Grade { Low = 1, Medium = 2, High = 3, Maximum = 4 }
		[Flags]
		enum GradeFlags { Low = 1, Medium = 2, High = 4, Maximum = 8 }

		static void Main(string[] args)
		{
			//string iGrade = (Grade.Low Grade.High ).ToString();
			//string fGrade = (GradeFlags.Low GradeFlags.High).ToString();
			//MessageBox.Show(iGrade + " - with flag : " + fGrade);

			foreach (var enumValue in Enum.GetValues(typeof(Grade)))
			{
				Console.WriteLine("{0} - with flag: {1}", (int)Enum.Parse(typeof(Grade), enumValue.ToString()), (int)Enum.Parse(typeof(GradeFlags), enumValue.ToString()));
			}

			Console.ReadLine();
		}
	}
 
Share this answer
 
Comments
forte74 11-Nov-16 18:16pm    
what does the flag attribute do ?
Clifford Nelson 11-Nov-16 19:02pm    
Indicates that an enumeration can be treated as a bit field; that is, a set of flags. https://msdn.microsoft.com/en-us/library/system.flagsattribute(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900