It is currently 08 Sep 2025, 20:16
   
Text Size

To those who compare enums with .equals() -- moomarc, Sloth

Post MTG Forge Related Programming Questions Here

Moderators: timmermac, Blacksmith, KrazyTheFox, Agetian, friarsol, CCGHQ Admins

To those who compare enums with .equals() -- moomarc, Sloth

Postby Max mtg » 19 Feb 2013, 02:58

Dear fellow developers, keep in ming that SpellAbility method getAPI() may return you a null and you'll get a funny NPE with the code you wrote like

Code: Select all
if (sa.getApi().toString().equals("DealDamage")) {

 - and -

|| (!a.getApi().equals(ApiType.Mana) && !a.getApi().equals(ApiType.ManaReflected))) {
the correct way to do it is:
Code: Select all
if (sa.getApi() == ApiType.DealDamage) {

 - and -

|| (a.getApi() != ApiType.Mana && a.getApi() != ApiType.ManaReflected)) {
The internets also consider == a better option:
http://stackoverflow.com/questions/1750 ... -or-equals
Single class for single responsibility.
Max mtg
Programmer
 
Posts: 1997
Joined: 02 Jul 2011, 14:26
Has thanked: 173 times
Been thanked: 334 times

Re: To those who compare enums with .equals() -- moomarc, Sl

Postby moomarc » 19 Feb 2013, 05:57

Thanks Max. I'll keep that in mind in future. :D
-Marc
User avatar
moomarc
Pixel Commander
 
Posts: 2091
Joined: 04 Jun 2010, 15:22
Location: Johannesburg, South Africa
Has thanked: 371 times
Been thanked: 372 times


Return to Developer's Corner

Who is online

Users browsing this forum: No registered users and 34 guests

Main Menu

User Menu

Our Partners


Who is online

In total there are 34 users online :: 0 registered, 0 hidden and 34 guests (based on users active over the past 10 minutes)
Most users ever online was 7303 on 15 Jul 2025, 20:46

Users browsing this forum: No registered users and 34 guests

Login Form