?
Solved

dictionary object

Posted on 1998-12-17
6
Medium Priority
?
508 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 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

777 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