[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

dictionary object

Posted on 1998-12-17
6
Medium Priority
?
513 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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