Solved

dictionary object

Posted on 1998-12-17
6
500 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
  • 4
  • 2
6 Comments
 
LVL 6

Accepted Solution

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

Expert Comment

by:gadio
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 28

Author Comment

by:sybe
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now