newbie enum question: int index?

D

deko

In an attempt to index my imageList, I do this:

private enum NodeImage
{
imgA, imgA, imgC
}

Then I try this:

private TreeNode nodeA = new TreeNode("NODE A", NodeImage.imgA,
NodeImage.imgA);

But the new TreeNode method wants ints, so that will not compile.

Do I have to do this:

private TreeNode nodeA = new TreeNode("NODE A", (int)NodeImage.imgA,
(int)NodeImage.imgA);

Why don't I get the int index of the enum by default?

Thanks in advance.
 
D

Daniel O'Connell [C# MVP]

deko said:
In an attempt to index my imageList, I do this:

private enum NodeImage
{
imgA, imgA, imgC
}

Then I try this:

private TreeNode nodeA = new TreeNode("NODE A", NodeImage.imgA,
NodeImage.imgA);

But the new TreeNode method wants ints, so that will not compile.

Do I have to do this:

private TreeNode nodeA = new TreeNode("NODE A", (int)NodeImage.imgA,
(int)NodeImage.imgA);

Why don't I get the int index of the enum by default?

Basically because an enum is an enum, not an int. The strong typing that
makes an enum explicitly an enum and requires an explicit operation to treat
it as another type is part of what makes enums interesting and more valuable
than just constant fields, which, incidentially, is what I'd use in your
case.
 
N

Nick Hounsome

deko said:
In an attempt to index my imageList, I do this:

private enum NodeImage
{
imgA, imgA, imgC
}

Then I try this:

private TreeNode nodeA = new TreeNode("NODE A", NodeImage.imgA,
NodeImage.imgA);

But the new TreeNode method wants ints, so that will not compile.

Do I have to do this:

private TreeNode nodeA = new TreeNode("NODE A", (int)NodeImage.imgA,
(int)NodeImage.imgA);

Why don't I get the int index of the enum by default?

Because NodeImage is an enumeration not an int and C# takes its type safety
seriously.

Derive your own class from TreeNode and give it a constructor taking your
enum. There are many other good reasons for deriving your own TreeNode
anyway.
 
D

deko

Derive your own class from TreeNode and give it a constructor taking your
enum. There are many other good reasons for deriving your own TreeNode
anyway.

How does that make the enum an int?

Also, I tried using a derived TreeView, but the Designer generates all the
Drag/Drop code in the host form class. So the derived TreeView is there,
but all the code for it is in the host form's class. That's why the enum
was in the form's class - I didn't want to mess with the generated code.
 
N

Nick Hounsome

deko said:
How does that make the enum an int?

It doesn't but it hides away the casting to int in one place where it should
be.
Also, I tried using a derived TreeView, but the Designer generates all the
Drag/Drop code in the host form class. So the derived TreeView is there,
but all the code for it is in the host form's class. That's why the enum
was in the form's class - I didn't want to mess with the generated code.

I've never tried deriving from TreeView. What for?
 
D

deko

Hi and thanks for the reply.
It doesn't but it hides away the casting to int in one place where it
should be.

So if I have this in the derived TreeView's class:

Private enum NodeType
{
imgA, imgB, imgC
}

how is it used differently there, as opposed to in the control's host form's
class? Don't I still have to cast like this:

private TreeNode nodeA = new TreeNode("NODE A", (int)NodeImage.imgA,
(int)NodeImage.imgA);

what do you mean "the casting to int is in one place"?
I've never tried deriving from TreeView. What for?

There's a reason why I'm deriving it, I just don't know what it is :)
 
N

Nick Hounsome

deko said:
Hi and thanks for the reply.


So if I have this in the derived TreeView's class:

Private enum NodeType
{
imgA, imgB, imgC
}

how is it used differently there, as opposed to in the control's host
form's class? Don't I still have to cast like this:

private TreeNode nodeA = new TreeNode("NODE A", (int)NodeImage.imgA,
(int)NodeImage.imgA);

what do you mean "the casting to int is in one place"?


There's a reason why I'm deriving it, I just don't know what it is :)

I've kind of lost track of where we started from but I would generally use
something like the following when using a TreeView:

public enum NodeImage { .... }

public class MyTreeNode : TreeNode
{
private MyClass xxx;

public MyTreeNode(MyClass xxx, NodeImage image, NodeImage selectedImage)
{
ImageIndex = (int)image;
SelectedImageIndex = (int)selectedImageIndex;
Text = xxx.UsefulInfo();
this.xxx = xxx;

// can even add subnodes here if appropriate
}

// nice to have a typesafe property instead of Tag
public MyClass MyThing
{
get { return xxx; }
}
}

It moves node logic into the node which makes for much cleaner code IMHO.

It can be especially useful where you have different nodetypes - add a
common interface and you can just operate polymorphically on the nodes.

The same applies for ListViewItem.

It's not perfect because the enumerations must match the image list in the
view but deriving your own TreeView is too much like hard work because of
the node collections.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top