Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

functions from object in Vector?

Thread view: 
tim - 01 May 2004 10:39 GMT
Hi,

I have a Vector in which I store all my objects of the same class,
because I don't know how long this "array" has to be from before. Now,
off course I want to use my objects, but I can't seem to get access to
the functions of these objects I've stored.

        private Vector MenuItemList;
        MenuItemList = new Vector();
              tempMenuItem = new MenuItem();
                             // class MenuItem extends Object
              MenuItemList.add(tempMenuItem);

This works fine... but when trying:

              MenuItemList.elementAt(i).getId();
              // getId() is a method from class MenuItem

I get the error "The method "getId()" is undefined for the type Object".
So I thought of copying the object back into a temporary object of the
type MenuItem, but:

              tempMenuItem = MenuItemList.elementAt(i);

gives me the error: "Cannot convert from Object to MenuItem".

Now, I wonder why there is such a thing as a Vector to store your object
in, if you can't get them out. So I probably just missed something here.
How can I access my objects and their methods?

thanx in advance,
Tim
Bjorn Abelli - 01 May 2004 10:50 GMT
"tim" wrote...

You can do:

  tempMenuItem = (MenuItem) MenuItemList.elementAt(i);

...or even:

 ((MenuItem)MenuItemList.elementAt(i)).getId();

// Bjorn A
tim - 01 May 2004 11:26 GMT
> You can do:
>
[quoted text clipped - 3 lines]
>
>   ((MenuItem)MenuItemList.elementAt(i)).getId();

Ok, well, I'm normally a C++ programmer, so I'm not quite familiar with
those tricks in java. ;-) Thanx for your (very) quick respond!

greetz
Tim
Bjorn Abelli - 01 May 2004 11:59 GMT
"tim" wrote...

> > You can do:
> >
[quoted text clipped - 3 lines]
> >
> >   ((MenuItem)MenuItemList.elementAt(i)).getId();

> Ok, well, I'm normally a C++ programmer,
> so I'm not quite familiar with
> those tricks in java. ;-)

You should be!

The concept of casting isn't *that* much different in C++.

> Thanx for your (very) quick respond!

You're welcome... :-)

// Bjorn A
Luc Van Bogaert - 01 May 2004 11:26 GMT
> I have a Vector in which I store all my objects of the same class,
> because I don't know how long this "array" has to be from before. Now,
> off course I want to use my objects, but I can't seem to get access to
> the functions of these objects I've stored.

First *cast* the object you get from the Vector to the desired type,
before calling its methods.

Signature

Luc Van Bogaert

via ProNews/2 & eComStation...
http://www.os2world.com/os2ecs

tim - 01 May 2004 11:34 GMT
>> I have a Vector in which I store all my objects of the same class,
>> because I don't know how long this "array" has to be from before.
[quoted text clipped - 3 lines]
> First *cast* the object you get from the Vector to the desired type,
> before calling its methods.

is that what Bjorn suggested to do? Like:

tempMenuItem = (MenuItem)MenuItemList.elementAt(i);

? Because I tried this, but Eclipse doesn't seem to accept this
either... nor the notation:

((MenuItem)MenuItemList.elementAt(i)).getId()

And this while I have no trouble at all working with normal objects from
this class, so the class is definitly known in the project...

Thanx in advance
Tim
Bjorn Abelli - 01 May 2004 12:06 GMT
"tim" wrote...

> > First *cast* the object you get from the Vector
> > to the desired type, before calling its methods.
>
> is that what Bjorn suggested to do? Like:
>
> tempMenuItem = (MenuItem)MenuItemList.elementAt(i);

Yes.

> ? Because I tried this, but Eclipse doesn't seem
> to accept this either... nor the notation:
[quoted text clipped - 4 lines]
> normal objects from this class, so the class is definitly
> known in the project...

What is "Eclipse" complaining about when you try
some of the lines above?

What's the error message?

// Bjorn A

P.S. The preferred method to use instead of
elementAt(int) is get(int), since JDK 1.2.
tim - 01 May 2004 16:17 GMT
> What is "Eclipse" complaining about when you try
> some of the lines above?
>
> What's the error message?

Well, it was first the same as the first message, then it was complaining
about expecting an array type... Very weird, so I deleted the whole thing,
wrote it again, and now it seems to accept most of it. Thanx for the
information. The problem with java is that it is so popular that google
finds way to much. ;-)

greetings
Tim
javac - 07 May 2004 23:23 GMT
[..]
> // Bjorn A
>
> P.S. The preferred method to use instead of
> elementAt(int) is get(int), since JDK 1.2.

more important, it's better to use an interface (ie, list) than
vector.  using the interface gives you more flexability down the road.
you might want to change that vector to ArrayList or whatever it's
called :)

javac@mail.com
http://www.geocities.com/cjavacjava/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.