Solved

dictionary object

Posted on 1998-12-17
6
503 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 52
egit plugin on eclipse 8 63
tomcat not starting 6 45
jboss wildfly 10.1 10 89
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

770 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