-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: build error, 1st attempt from quick start
PostPosted: Wed Jan 31, 2007 10:51 pm 
Newbie

Joined: Wed Jan 31, 2007 10:20 pm
Posts: 4
Apologies if this is too basic, have just started picking up .net 2.0 from 1.1 and am really confused with this error i'm getting, search has helped none and I'm not familiar enough with the framework to make sense of the error. NHibernate is working though as up until this point all was working perfectly.

I suspect this is an error with how i've slapped the code together or some inconsistency with using #develop instead of visual studio. All the same if anyone can help out at all it'd be very much appreciated.


Here is the code I'm using (as ripped from the quickstart guide)

Code:
Configuration cfg = new Configuration();
            cfg.AddAssembly("testConsoleApplication");
            
            ISessionFactory factory = cfg.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();
            
            User newUser = new User();
            newUser.Id = "joe_cool";
            newUser.UserName = "Joseph Cool";
            newUser.Password = "abc123";
            newUser.EmailAddress = "joe@cool.com";
            newUser.LastLogon = DateTime.Now;
            
            // Tell NHibernate that this object should be saved
            session.Save(newUser);
            
            // commit all of the changes to the DB and close the ISession
            transaction.Commit();
            session.Close();
            Console.WriteLine("User added");
            
            // open another session to retrieve the just inserted user
            session = factory.OpenSession();
            User joeCool = (User)session.Load(typeof(User), "joe_cool");
            Console.WriteLine("User retrieved");
            // set Joe Cool's Last Login property
            joeCool.LastLogon = DateTime.Now;
            // flush the changes from the Session to the Database
            session.Flush();
            Console.WriteLine("User updated");
            // ****** all works fine up until here...
            IList userList = session.CreateCriteria(typeof(User)).List();
            foreach(User user in userList)
            {
                Console.WriteLine(user.Id + " last logged in at " + user.LastLogon);
            }


These are the build errors i'm receiving:

Quote:
Build started.
Compiling testConsoleApplication
C:\Documents and Settings\Administrator\My Documents\Projects\testConsoleApplication\Main.cs(52,5) : error CS0305: Using the generic type 'System.Collections.Generic.IList<T>' requires '1' type arguments
C:\Documents and Settings\Administrator\My Documents\Projects\testConsoleApplication\Main.cs(53,5) : error CS1579: foreach statement cannot operate on variables of type 'IList' because 'IList' does not contain a public definition for 'GetEnumerator'
Build failed.


Thanks for any suggestions


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 12:32 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Looks like you are mixing the non-generic and generic IList implementations...
change this
Code:
IList userList = session.CreateCriteria(typeof(User)).List();
foreach(User user in userList)
{
     Console.WriteLine(user.Id + " last logged in at " + user.LastLogon);
}


to this...
Code:
IList<User> userList = session.CreateCriteria(typeof(User)).List<User>();
foreach(User user in userList)
{
      Console.WriteLine(user.Id + " last logged in at " + user.LastLogon);
}


and it should work. If you have worked with the .net 1.1 framework, recall that iterating on a non-generic arraylist would fail (iterating over the index is common), but this isn't true with the .net 2.0 List<T> where you can strongly iterate over the collection contents' types themselves (weird way of saying that but you get the point :)
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 12:35 am 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
Hi Danny,

It's a bit tricky. I was tripped when I upgraded from .net 1.1 to 2.0. What matters is the default using statements.

in .net 1.1, "using System.Collections;" is one of the default using statements when you create a source file.

in .net 2.0, we have "using System.Collections.Generic;" instead.

Back to your error code "IList userList = session.CreateCriteria(typeof(User)).List();", it is an error because we don't have the class IList in System.Collections.Generic. We only have IList<T>.

Change it to "System.Collections.IList userList = session.CreateCriteria(typeof(User)).List();" and you're done. Alternatively, add a "using System.Collections" also helps

Regards,

Ken


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 7:29 am 
Newbie

Joined: Wed Jan 31, 2007 10:20 pm
Posts: 4
Thanks guys, was wondering when I'd come across my first .net2ism

Mike, your explanation made sense but the code you offered didn't quite work and threw the error:

Quote:
C:\Documents and Settings\Administrator\...\Main.cs(55,65) : error CS0308: The non-generic method 'NHibernate.ICriteria.List()' cannot be used with type arguments
Build failed.


But Kens does so thanks ever so much guys.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 10:50 pm 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
Mike's code should work if your are using NH 1.2.0. Upgrade your NH to 1.2.0 Beta 3 if you can because it supports Generics in .net 2.0.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 9:38 am 
Newbie

Joined: Wed Jan 31, 2007 10:20 pm
Posts: 4
I see, that makes sense.

thanks again!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.