Results 1 to 12 of 12

Thread: Enumeration Members [Resolved]

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Resolved Enumeration Members [Resolved]

    Can one loop through an enumeration in code? Or write something to get a random member of the enumeration?

    For instance. I'm testing this treeview control. Got many borderstyles. So just want to add a button and when i click it, set the borderstyle to anoter on in the Borderstyle enumeration
    Last edited by StrangerInBeijing; May 29th, 2005 at 11:07 AM. Reason: RESOLVED

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Enumeration Members

    you can use the getNames method to get a list of the items in an enum
    Code:
    public enum testE
    	{
    		ItemOne,
    		ItemTwo,
    		ItemThree
    	}
    ...
    foreach(string s in Enum.GetNames(typeof(testE)))
                	Console.WriteLine(s);
    ...

  3. #3

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: Enumeration Members

    Thanks DE, but I'm a bit stuck again.....



    Code:
     foreach (string s in Enum.GetNames(typeof (System.Web.UI.WebControls.BorderStyle)))
     {
       TreeView1.BorderStyle = ...what do in do here????
       System.Threading.Thread.Sleep(1000);
    
     }

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Enumeration Members

    use the getNames to get a string array you can use the lenght of the array to determine the number of elements in the enum then while less than length:
    TreeView1.BorderStyle++;

  5. #5
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Enumeration Members

    sod that above this is nicer
    Code:
    foreach(string s in Enum.GetNames(typeof(BorderStyle))){
        treeView1.BorderStyle = (BorderStyle)Enum.Parse(typeof(BorderStyle),s,true);
        Thread.Sleep(1000);
    }

  6. #6

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: Enumeration Members

    Thanx a lot! I will try it out first thing in the AM.
    Right now diggin into this asp.net 2 book (why do they always change things so much...grrrrrr)

    Thanx again hey!

    cheers

  7. #7

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: Enumeration Members

    Yes, it works cool! I mean when I check the TreeView's borderstyle property when stepping through code.

    Got another question about this, but don't know if it's relevant, as this Treeview is not the one in your toolbox, but one that the team I'm in are testing.

    Q: How do you refresh the treeview after you changed the borderstyle? It does change in code, but you cannot see the change on your web page. There are no refresh method I'm used to in VB, and also I never did webpages....complete noob!

  8. #8
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Enumeration Members

    Can't help you with that one, my knowledge of ASP.Net is zero.

  9. #9

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: Enumeration Members

    welcome to the club.. ..gonna get me a normal desktop app job as soon as I get this C# under the knee!

    But thanx a lot for your help.

  10. #10

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: Enumeration Members

    It's been a while, but I wrote some stuff today that uses this.

    Works sweet.

    uuuuh....Now how do I convert a string (say "Dotted") back to the enumeration member (System.Web.UI.WebControls.BorderStyle.Dotted) ?

    Hope it's not too easy, otherwise I'm look like an ass......nevermind....allready look like an ass

  11. #11

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: Enumeration Members

    this does give me an error...

    Code:
        BorderStyle MyStyle = Enum.Parse(typeof (BorderStyle), sSel);
        TreeView1.BorderStyle = MyStyle;
    Error 2 Cannot implicitly convert type 'object' to 'System.Web.UI.WebControls.BorderStyle'. An explicit conversion exists (are you missing a cast?) c:\inetpub\wwwroot\WebPages\TreeView\BorderStyles.aspx.cs 28 27 http://localhost/WebPages/

  12. #12
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Enumeration Members

    Well Eeyore,
    The compiler asks "are you missing a cast?"
    The Parse method returns an object which must be converted to the appropriate Enum.
    Code:
    BorderStyle MyStyle = (BorderStyle)Enum.Parse(typeof (BorderStyle), sSel);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width