Case insensitive enumeration class

조회 수: 3(최근 30일)
Phillip
Phillip 2016년 9월 19일
편집: Sean de Wolski 2016년 9월 20일
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

채택된 답변

Brendan Hamm
Brendan Hamm 2016년 9월 19일
Why would you want to have your enumeration be able to have any case?
The only place I could consider this, is if the user has to specify the string via some form of text input e.g. an inputdlg. If this is the case, then you should simply do a case insensitive string validation and with this result dynamically create your object:
val = validatestring('red',{'Red'})
aRed = colourEnum.(val)
Other than this, the MATLAB command prompt will alert the user of the correct case of the characters involved.
Yes. Enumerations must conform to the MATLAB variable naming convention.
  댓글 수: 1
Phillip
Phillip 2016년 9월 19일
Hi Brendan
I hear you and I think you are right (need to give it some thought). The enumeration is unique in itself and should be treated as such. The only problem I have is that I need to code that validation outside of the enumeration object, where somehow naturally i wanted to do it within the object (so that the object handles it). Ie RED is ReD is red and it should be able to accept that.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Sean de Wolski
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
Sean de Wolski
Sean de Wolski 2016년 9월 20일
Why do you want to do this? What's the bigger picture?

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by