Jump to content

One question about Enum

- - - - -

  • Please log in to reply
6 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello,

I have one question about Enum. I would like to know how is possible to call an Enum from one class to other. More specific, I have two Enum type in Card (Suit, Rank) and I want to call them in Deck class to create a deck.

Thank you,
toto_7

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2
krzysztof86

krzysztof86

    Newbie

  • Members
  • Pip
  • 1 posts
Hello if in Java is the same like in C# you must do:
public partial class MainWindow : Form //first class declare enum wybor
{
public enum wybor :short
{
Window1,
Window2



};
/***/
Next in event Choice
private void choice_Click(object sender, EventArgs e)
{

Window2 window = new Window2(wybor.Window1//HERE YOU SEE YOUR ENUM );
window.Show();
}

Next in seccond window in constructor you must :
public Window2(Window1.wybor wyb)
{
int numer = (int)wyb;
switch (numer)
{
case 0 :
InitializeComponent();
break;
case 1:
MessageBox.Show(" working fine !");
break;
default:
MessageBox.Show("Error with enum");
break;
}

Edited by krzysztof86, 11 August 2011 - 04:56 AM.


#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
It depends how you wrote your enum. It can be inside a class, or outside.
If it's outside you just import it like for a class and you can use it:
import mypkg.MyEnum;

public class SomeClass{
   MyEnum val = MyEnum.VALUE;
}
If it's inside a class, you import that class and access the enum trough it:
public class Outer{
  enum MyEnum(VALUE, VALUE2, VALUE3à;
}

//other class, other file
import Outer;

public class SomeClass{
  Outer.MyEnum val = Outer.MyEnum.VALUE;
}
Note if it's inside a class, I think the access modifiers matter (private/protected/default/public) to whether you can access it or not.

#4
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello wim DC.

I have problem with class import.
When I'm trying to import the Class_1 to the Class_2, "The import Class_1 cannot be resolved"
Also, if I want to retrieve all the elements of an enum, I will use :
Outer.MyEnum val = Outer.MyEnum.values();
???

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

toto_7 said:

Hello wim DC.

I have problem with class import.
When I'm trying to import the Class_1 to the Class_2, "The import Class_1 cannot be resolved"
Also, if I want to retrieve all the elements of an enum, I will use :
Outer.MyEnum val = Outer.MyEnum.values();
???
The values() returns an array:
Outer.MyEnum[] val = Outer.MyEnum.values();

I can't really say much about the import without knowing your file structure and the access modifiers (public/private/...)

Edited by wim DC, 14 August 2011 - 02:43 AM.


#6
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Here is my Card.java
import java.util.*;


import javax.swing.*;



public class Card {


	public enum Rank {DEUCE, THREE, FOUR, FIVE, SIX,

        SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE}

	

	public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }

	

	private final Rank rank;

	private final Suit suit;

	

	public Card(Rank rank, Suit suit){

		this.rank = rank;

		this.suit = suit;

	}

	public Rank rank() { return rank; }

    public Suit suit() { return suit; }

    public String toString() { return rank + "_" + suit; }

    

    public ImageIcon cardToImage(){

    	ImageIcon imageCard = new ImageIcon(rank + "_" + suit);

    	return imageCard;

    }

}

Trying to create a BlackJack game and this class, creates the cards and also, convert each card to an image (corresponding card). Now I would like to have access to enums from my Deck class that creates a deck. I have this:
static {

	        for (Suit suit : Suit.values())

	            for (Rank rank : Rank.values())

	                deck.add(new Card(rank, suit));

	    }
but I'm getting error because Suit and Rank cannot be resolve to types in Deck class. How I can fix it?

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
.If deck is in the same package as Card you need no import and doing
Card.Rank rank = Card.Rank.THREE;
should work.

If it is in another package you need to import the Card class:
import pckgX.Card;
And then you can use the same code as above.

If you don't like to type Card.Rank all the time, and want to just use Rank you can use a static import:
import static pckgX.Card.*
...
Rank rank = Rank.THREE;





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users