Solved

dictionary object

Posted on 1998-12-17
6
507 Views
Last Modified: 2006-11-17
I want to use a dictionary object to pass values/objects to another class.

I can't get it to work (I can do it using VB, but that's a different language).

This code:

=================
import java.awt.*;
import java.util.Dictionary;
import java.applet.*;

public class TestDictionary extends Applet  {
   Dictionary dicTest;
   Image imgInput;
   Image curimage;

   public void init() {
      imgInput = getImage(getDocumentBase(),"aan.gif");
      dicTest.put("curimage", imgInput);
      curimage = dicTest.get("curimage");
   }

   public void paint(Graphics g) {
      g.drawImage(imgInput, 0, 0, this);
   }
}
======================
gives an error on compilation:

Incompatible type for =. Explicit cast needed to convert java.lang. Object to java.awt.Image.

In fact I have 2 uncertainties/problems:

1. Is it possible to get the above code working, in other words, can I store an image in a dictionary object and retrieve it into another variable.

2. For further development: I want to store different objects in the dictionary object, such as String, integer, image, arrays. Can I store all those in one dictionary object, pass the dictionary object as a parameter to another class, and retrieve all values in that class ??

I would appreciate some sample code.
0
Comment
Question by:sybe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 200 total points
ID: 1229636
try:
  curimage = (Image)dicTest.get("curimage");
0
 
LVL 6

Expert Comment

by:gadio
ID: 1229637
Sorry, I didn't answer all you asked:
1. Yes, try the above code. When you take an object out of the dictionary, its an Object. You must cast it to your type.
2. Yes you can do that. You still nead to cast when you take them out. If you will not know what type the object is when you take it out, you can use the 'instnaceof' operator to check it.

Hope that helps,
G
0
 
LVL 6

Expert Comment

by:gadio
ID: 1229638
Sample code:

// storing
          dicTest.put("curimage", imgInput);
          dicTest.put("myname", "gadio");
          dicTest.put("num_of_games", new Integer(7));
          dicTest.put("num_of_minuts", new Double(3.5));


// getting
          Object ob = dicTest.get(the_key);
          if( ob instanceof Image ) {
             Image img = (Image)ob;
             // ... do somthing ...
          }
          else if( ob instanceof String ) {
             String str = (String)ob;
             // ... do somthing ...
          }
          else if( ob instanceof Double ) {
             double dbl = ((Double)ob).doubleValue();
             // ... do somthing ...
          }
          else if( ob instanceof Integer ) {
             int i = ((Integer)ob).intValue();
             // ... do somthing ...
          }

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Author Comment

by:sybe
ID: 1229639
Great, if you tell me how the instanceof parameter works (sample code), I'll grade your answer an A.

0
 
LVL 6

Expert Comment

by:gadio
ID: 1229640
You have that in the sample code I sent. the instanceof is an operator, a bit like the == operator. Its different in the way that if class a_class is a java.awt.Button for example the all the folowing expression will result 'true'

(a_class instanceof Button)
(a_class instanceof java.awt.Button)
(a_class instanceof Component)
(a_class instanceof Object)

Also if this class was implementing any interfaces, the instanceof would result a true for them as well. This is one of the advantages of Java over C++.
If there are still some points that you didn't understand, don't hesitate to ask.

Regards, G
0
 
LVL 28

Author Comment

by:sybe
ID: 1229641
Ok, thanks

I saw the sample code after I submiited my comment (I was trying your code and then interrupted by something).


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question