Using enum and specific values in method.

  • Follow


Hi all.

I wrote a method and I want to have only 2 possibility when using arguments. 
In other words, I just want to be able to use 2 type of values.



public class DataAnalyzer {

    public enum Mode { YSI, HYPERPRO } //the 2 possible values

    //Specify the file type
    public Mode mode;

    public DataAnalyzer(Mode m) {
    mode = m;
    }

Here's my main :

DataAnalyzer d = new DataAnalyzer(DataAnalyzer.Mode.YSI);

I want to know if this is a good way to do.  I dont like the 
"DataAnalyzer.Mode.YSI".  Maybe there's a better solution.

Best regards,
Phil 


0
Reply Philippe 6/8/2007 4:11:39 PM

In article <chfai.25548$ih7.119576@weber.videotron.net>,
 "Philippe" <Philippe@Massicotte.com> wrote:

> Hi all.
> 
> I wrote a method and I want to have only 2 possibility when using arguments. 
> In other words, I just want to be able to use 2 type of values.
> 
> 
> 
> public class DataAnalyzer {
> 
>     public enum Mode { YSI, HYPERPRO } //the 2 possible values
> 
>     //Specify the file type
>     public Mode mode;
> 
>     public DataAnalyzer(Mode m) {
>     mode = m;
>     }
> 
> Here's my main :
> 
> DataAnalyzer d = new DataAnalyzer(DataAnalyzer.Mode.YSI);
> 
> I want to know if this is a good way to do.  I dont like the 
> "DataAnalyzer.Mode.YSI".  Maybe there's a better solution.
> 
> Best regards,
> Phil 

I think it's a good idea.

If you want to use the Mode enum locally, you might want to declare it 
as a "public static enum" instead.  And you should *not* make the "mode" 
member variable public, but private so that it's required to be provided 
in the constructor.

If you really don't want the dotted notation, however, you can move the 
Mode enum outside of the DataAnalyzer class -- either in the same file 
or in a separate one.  But IMHO it's better design, if indeed this enum 
is only for use with DataAnalyzer, if it's part of the class.

= Steve =
-- 
Steve W. Jackson
Montgomery, Alabama
0
Reply Steve 6/8/2007 4:45:28 PM


Thank for your comments.
I corrected the public for a private member.

Best regards,
Phil

"Steve W. Jackson" <stevewjackson@knology.net> a �crit dans le message de 
news: stevewjackson-70BB23.11452708062007@individual.net...
> In article <chfai.25548$ih7.119576@weber.videotron.net>,
> "Philippe" <Philippe@Massicotte.com> wrote:
>
>> Hi all.
>>
>> I wrote a method and I want to have only 2 possibility when using 
>> arguments.
>> In other words, I just want to be able to use 2 type of values.
>>
>>
>>
>> public class DataAnalyzer {
>>
>>     public enum Mode { YSI, HYPERPRO } //the 2 possible values
>>
>>     //Specify the file type
>>     public Mode mode;
>>
>>     public DataAnalyzer(Mode m) {
>>     mode = m;
>>     }
>>
>> Here's my main :
>>
>> DataAnalyzer d = new DataAnalyzer(DataAnalyzer.Mode.YSI);
>>
>> I want to know if this is a good way to do.  I dont like the
>> "DataAnalyzer.Mode.YSI".  Maybe there's a better solution.
>>
>> Best regards,
>> Phil
>
> I think it's a good idea.
>
> If you want to use the Mode enum locally, you might want to declare it
> as a "public static enum" instead.  And you should *not* make the "mode"
> member variable public, but private so that it's required to be provided
> in the constructor.
>
> If you really don't want the dotted notation, however, you can move the
> Mode enum outside of the DataAnalyzer class -- either in the same file
> or in a separate one.  But IMHO it's better design, if indeed this enum
> is only for use with DataAnalyzer, if it's part of the class.
>
> = Steve =
> -- 
> Steve W. Jackson
> Montgomery, Alabama 


0
Reply Philippe 6/8/2007 5:14:42 PM

On Fri, 08 Jun 2007 11:45:28 -0500, "Steve W. Jackson"
<stevewjackson@knology.net> wrote, quoted or indirectly quoted someone
who said :

>If you want to use the Mode enum locally, you might want to declare it 
>as a "public static enum" instead.  And you should *not* make the "mode" 
>member variable public, but private so that it's required to be provided 
>in the constructor.
>
>If you really don't want the dotted notation, however, you can move the 
>Mode enum outside of the DataAnalyzer class -- either in the same file 
>or in a separate one.  But IMHO it's better design, if indeed this enum 
>is only for use with DataAnalyzer, if it's part of the class.

You can make it a totally separate class in its own source file.  Then
you import static.  See http://mindprod.com/jgloss/importstatic.html
That way you can leave off the class name.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
Reply Roedy 6/13/2007 5:26:02 AM

3 Replies
268 Views

(page loaded in 0.077 seconds)

5/20/2013 4:17:40 PM


Reply: