Case insensitive enumeration class
이전 댓글 표시
Hi,
How does one create a case insensitive enumeration class?
So that one could call it as:
aRed = colourEnum.RED;
aRed = colourEnum.red;
aRed = colourEnum.ReD;
I would also like to confirm that enumerations must follow variable naming conventions. Ie you can't have an enum that is:
enumclass.A/B
Am I right in this?
Regards, Phillip
채택된 답변
추가 답변 (1개)
Sean de Wolski
2016년 9월 19일
You could use a containers.Map to use non-MATLAB variable names if you want:
mapper = containers.Map('enumclass.A/B','A')
mapper('enumclass.A/B')
댓글 수: 3
Phillip
2016년 9월 19일
Sean de Wolski
2016년 9월 20일
편집: Sean de Wolski
2016년 9월 20일
Hi Phillip,
What I was suggesting won't work the way you have it above. For best looks, what you're doing is probably best. I was thinking more like a factory function to generate the enumerations given any key. disp can be overloaded to make it look right. This is probably not a good idea, but it is possible.
e = genEnum('Hello World')
e =
Hello World
Function
function enumObj = genEnum(key)
mapper = containers.Map({'enumclass.A/B','Hello World'},{'A','B'});
enum = mapper(key);
enumObj = EnumClass.(enum);
end
And
classdef EnumClass
enumeration
A
B
end
methods
function disp(obj)
mapper = containers.Map({'A','B'},{'enumclass.A/B','Hello World'});
label = mapper(char(obj));
disp(label)
end
end
end
Sean de Wolski
2016년 9월 20일
Why do you want to do this? What's the bigger picture?
카테고리
도움말 센터 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!