Casting vector from object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Casting vector from object

  1. #1
    Join Date
    May 2006
    Posts
    1

    Casting vector from object

    Hi all,

    I have a vector which has a string of dates retrieved from a DB

    what I need to do is parse the string which is in a vector and return it in a Date object type

    i.e.

    parseDate(vec.get(i));

    private void parseDate(Object o)
    {
    Date isoDate = valueOf(o);

    }

    However, this returns the error "the method valueOf(Object) is undefined for the type <Classname>

    Can anyone please tell me why I get this error and how I could go about fixing it...

    Many Thanks

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    You're trying to call valueOf() as if its part of your own class. It's a static method belonging to the String class.

    String.valueOf();

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    If you know that the objects you are storing in the vector are Strings, why would you use the "Object" class as the classtype of your argument to your method instead of a String object?. The compiler won't know that you want to use the String class methods or to treat the object as anything other than an Object.

    If you are using Java 5, make your vector a vector of Strings

    Vector<String> myVector = new Vector<String>();

    then. when you get() from the vector, it returns a String to you.

    If you are using an earlier verson, you'll need to cast the type of the object returned by the get(i) to a String object:

    parseDate((String)vec.get(i));

    I suggest that you use the DateFormat class to parse the String to create a date object. Look at the parse( String s ) method, which returns a Date.

Similar Threads

  1. casting object to int
    By jb in forum .NET
    Replies: 1
    Last Post: 03-27-2002, 10:05 PM
  2. Even C++ had this much right...
    By Derek Mooney in forum .NET
    Replies: 94
    Last Post: 10-29-2001, 08:44 PM
  3. Validating XML
    By Jaco de Villiers in forum XML
    Replies: 1
    Last Post: 06-01-2001, 05:50 PM
  4. Object casting..?
    By Micki in forum .NET
    Replies: 4
    Last Post: 05-10-2001, 03:22 AM
  5. Re: COM object Password Security
    By Tom Shreve in forum Enterprise
    Replies: 0
    Last Post: 04-07-2000, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center