Re: [android-developers] Re: switch case using string from Enum
Oo rfe p -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [android-developers] Re: switch case using string from Enum
Please be more specific? Regards, Saurav Mukherjee Twitter https://twitter.com/fasuke Facebook https://www.facebook.com/fuusuke LinkedIn http://www.linkedin.com/in/sauravmukherjeelinkedin On Mon, Jul 28, 2014 at 2:34 PM, Ziri Ziri ziridm...@gmail.com wrote: Oo rfe p -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[android-developers] Re: switch case using string from Enum
Yep, you can't do that. If 'event' is of type 'Type', then you could do switch(event) { case TYPE_ONE: case TYPE_TWO: ...} ..., because Enum values are considered to be constant. Instead, change your switch statement to if (...) else if (...) { ... } else if (...) { ... } else if (...) { ...} .. ... statements. A bit more typing, but works well :) On Friday, July 18, 2014 11:57:03 AM UTC-4, sweety fx wrote: *I have the below enum:* public enum Type { TYPE_ONE(event/one); TYPE_TWO(event/two); private Type(String mType){ this.mType = mType; } private final String mType; public String getType(){return mType;} } *I am trying to compare in Switch/Case:* switch (event.getType()){ case Type.TYPE_ONE.getType(): // says Constant expression required Do something: case Type.TYPE_TWO.getType(): // says Constant expression required Do something; } -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[android-developers] Re: switch case using string from Enum
Is there a way to pass enum value from the string? So that I can make event.getType() to return enum of the string instead of returning string? On Friday, July 18, 2014 1:22:45 PM UTC-4, Streets Of Boston wrote: Yep, you can't do that. If 'event' is of type 'Type', then you could do switch(event) { case TYPE_ONE: case TYPE_TWO: ...} ..., because Enum values are considered to be constant. Instead, change your switch statement to if (...) else if (...) { ... } else if (...) { ... } else if (...) { ...} .. ... statements. A bit more typing, but works well :) On Friday, July 18, 2014 11:57:03 AM UTC-4, sweety fx wrote: *I have the below enum:* public enum Type { TYPE_ONE(event/one); TYPE_TWO(event/two); private Type(String mType){ this.mType = mType; } private final String mType; public String getType(){return mType;} } *I am trying to compare in Switch/Case:* switch (event.getType()){ case Type.TYPE_ONE.getType(): // says Constant expression required Do something: case Type.TYPE_TWO.getType(): // says Constant expression required Do something; } -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[android-developers] Re: switch case using string from Enum
Not without adding some code yourself. I have done this many times when values from some JSON string from a server map to Enum values but these JSON strings don't map onto the names of the Enum values directly. I usually added a public static method to my Enum class, a factory that produces its values based upon a String value: public enum Type { public static Type get(String typeStr) { for (Type value : Type.values()) { if (value.getType().equals(typeStr)) { return value; } } return null; } } And now, as an example, in any other part of the code, you can just call Type.get(event/one) and get the correct Type value back. On Friday, July 18, 2014 1:35:27 PM UTC-4, sweety fx wrote: Is there a way to pass enum value from the string? So that I can make event.getType() to return enum of the string instead of returning string? On Friday, July 18, 2014 1:22:45 PM UTC-4, Streets Of Boston wrote: Yep, you can't do that. If 'event' is of type 'Type', then you could do switch(event) { case TYPE_ONE: case TYPE_TWO: ...} ..., because Enum values are considered to be constant. Instead, change your switch statement to if (...) else if (...) { ... } else if (...) { ... } else if (...) { ...} .. ... statements. A bit more typing, but works well :) On Friday, July 18, 2014 11:57:03 AM UTC-4, sweety fx wrote: *I have the below enum:* public enum Type { TYPE_ONE(event/one); TYPE_TWO(event/two); private Type(String mType){ this.mType = mType; } private final String mType; public String getType(){return mType;} } *I am trying to compare in Switch/Case:* switch (event.getType()){ case Type.TYPE_ONE.getType(): // says Constant expression required Do something: case Type.TYPE_TWO.getType(): // says Constant expression required Do something; } -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[android-developers] Re: Switch case on String
No, you can't. In Java we don't have switch with Strings. Regards, Felipe Silveira On Thu, Sep 24, 2009 at 11:56 AM, Abhi abhishek.r.sha...@gmail.com wrote: Can I do a switch(String), case (compare with other string) and how? I am trying to compare string results and perform actions... Thanks, Abhi -- Felipe Silveira Engenharia da Computação Universidade Federal de Itajubá http://www.felipesilveira.com.br MSN: felipeuni...@hotmail.com Skype: fsunifei - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Switch case on String
Ok. Thanks Felipe. I just figured that out. Regards, Abhi On Sep 24, 12:47 pm, Felipe Silveira webfel...@gmail.com wrote: No, you can't. In Java we don't have switch with Strings. Regards, Felipe Silveira On Thu, Sep 24, 2009 at 11:56 AM, Abhi abhishek.r.sha...@gmail.com wrote: Can I do a switch(String), case (compare with other string) and how? I am trying to compare string results and perform actions... Thanks, Abhi -- Felipe Silveira Engenharia da Computação Universidade Federal de Itajubáhttp://www.felipesilveira.com.br MSN: felipeuni...@hotmail.com Skype: fsunifei - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---