is an enum a method?

Error: window1.btnLoadFiles.Action, line 18: There is more than one method with this name but this does not match any of the available signatures. ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, ColumnIndexNbr.NameOfFile) = file.Name

I have determined the error is exactly with the implementation of the ColumnIndexNbr.NameOfFile and I have given this countless endless names which are most certainly unique. They do not exist anywhere else in the code, only in the enumeration.

What’s most stupid is, a) they are defined as integers, and yet, if I cast it to an integer, it works!

Here is the definition of my enum:

Enum ColumnIndexNbr As Integer 
  idxNumber
  DateTime 
  Size 
  NameOfFile
End Enum

The code that doesn’t work:

ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, ColumnIndexNbr.NameOfFile) = file.Name

Code that does work:

ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, 3) = file.Name
ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, Integer(ColumnIndexNbr.NameOfFile)) = file.Name

According to ListBox (deprecated) — Xojo documentation CellValueAt takes an integer in it’s second argument and when I pass in an integer directly, it works. However, the enum that is also an integer is being treated as though it’s a duplicate item of something else, but I can’t figure out what.

The error message is totally useless. The idea of me using an enum in this situation is when I later change the position of the columns or add new columns, I can do so without messing up connections, but I just can’t get the enum to work. I suppose I could just create random constants (I will do that for now), but if someone knows why I am getting this error, it would be much appreciated

PS: in coming with. title for this question I realise it’s treating what I am entering as a method and not an enum, but I still don’t know how to fix that :slight_smile:

I think the compiler looks at the enum as type “ColumnIndexNbr” and not as an integer. Therefore it is looking for a matching method of type “Listbox.CellValueAt(Integer, ColumnIndexNbr) as String” which it can’t find. As there are 2 methods for CellValueAt (get/set) it says there is more than one method and it doesn’t know what one you want. Casting to integer is probably the best bet to tell the compiler what you want. Another possibility is to make an extends method (global extends method in a module) that calls the original method “CellValueAt” but with your enum as the second parameter. Not 100% that wouldn’t confuse the compiler more and also keep in mind that it would be 2 function calls so you might lose some performance.

you could add it to the feedback feature/bug tool.

this works too

Listbox1.CellValueAt(listbox1.LastAddedRowIndex,CType(MyEnum.a, Integer)) = "Hello"

Although you can cast an enumeration to an Integer, it is not an Integer. It is it’s own type.

As the doc page notes:

So you could do this:

Listbox1.CellValueAt(Listbox1.LastAddedRowIndex, Integer( ColumnIndexNbr.NameOfFile)) = "Hello"

https://documentation.xojo.com/getting_started/using_the_xojo_language/enumerations.html

https://documentation.xojo.com/api/data_types/enumeration.html

the elements are defined as integer so nobody would expect that to cast again.

Enum ColumnIndexNbr As Integer 

the issue was the error message at CellValueAt.

[quote=497196:@Paul Lefebvre]Although you can cast an enumeration to an Integer, it is not an Integer. It is it’s own type.

As the doc page notes:

[quote]If you find you need the actual Integer value stored by the enumeration, then you have to convert (cast) it to an Integer first
[/quote]
So you could do this:

Listbox1.CellValueAt(Listbox1.LastAddedRowIndex, Integer( ColumnIndexNbr.NameOfFile)) = "Hello"

https://documentation.xojo.com/getting_started/using_the_xojo_language/enumerations.html
https://documentation.xojo.com/api/data_types/enumeration.html[/quote]

Then why do we set the enumerator as an integer if it’s not an integer? If it is it’s own type, then seriously, what is the point?

You use enumeration BECAUSE you want to pull out the underlying value codified by the enumeration. I have not come across this in any other language, you enumerate and use that token as if it is that value, not some obscure value that needs to be cast.

What is the purpose of:

As mentioned, in the end, I removed the enumeration and used constants. At least (for now) they appear to be recognised as what I declare them to be and not some mysterious “other” type that needed to be casted back to its original type. lol

It’s late. I hope you all are well,
Cheers!

So what IS it’s type?

It is its own type. So any enum called ColumnIndexNbr has the type of ColumnIndexNbr. This is similar to how a class you create called Person has a type of Person.

Sometimes you might want a specific integer value assigned to an enum value to use later. Most of the time you don’t care what the specific integer value is that is assigned to an enum.

If enums always auto-converted to integers it would remove their most useful feature, which is the type safety you get because they enforce that you are using a valid enum value.

If you don’t care about that then using a constant is a fine alternative.

With that said, you could also implement Extension functions on the enum to allow it to work more directly with the integer value, if you wanted.

And looking at the Inspector, I can see why this would be confusing and it seems like we could make that clearer. Hopefully the doc pages linked above help clarify this for the time being.

The main point of using Enums vs. Constants is that Enums allow the compiler to do type checking, which can prevent illegal / meaningless assignments.

For exmaple, this code won’t compile (which is a good thing):

Enum Foo
    a = 1
    b = 2

Enum Bar
    x = 10
    y = 20

dim foofoo as Foo = Foo.a // integer value 1

dim barbar as Bar
barbar = foofoo  // compile error!

Whereas if you are using integers and constants, this will compile and likely cause trouble

Const FooA = 1
Const FooB = 2

Const BarX = 10
Const BarY = 20

dim foofoo as Integer =  FooA

dim barbar as integer
barbar = foofoo // this compiles and runs, and is probably going to cause big trouble

[quote=497374:@Dave Matthews]You use enumeration BECAUSE you want to pull out the underlying value codified by the enumeration. I have not come across this in any other language, you enumerate and use that token as if it is that value, not some obscure value that needs to be cast.
[/quote]
C++ is this way

You’re describing a constant.