We help IT Professionals succeed at work.

C# types or templates

Can someone help me understand this code.  It is meant to be an example of how to use a C# library talking to a database called Mongo DB (http://wiki.github.com/atheken/NoRM/)

When compiling I get

Error      1      The type or namespace name 'Product' could not be found (are you missing a using directive or an assembly reference?)      ...
Error      2      The type or namespace name 'Product' could not be found (are you missing a using directive or an assembly reference?)      ...
Error      3      'Norm.ObjectId' does not contain a definition for 'NewObjectID'      ...
Error      4      The type arguments for method 'Norm.Collections.MongoCollection<Product>.FindOne<U>(U)' cannot be inferred from the usage. Try specifying the type arguments explicitly.      .....

I don't understand the syntax around the <Product> - looks like a template or something.
Assuming this looks right, where should the Product template/object be defined?

Thanks
BT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Norm;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var db = new Mongo("test", "10.1.1.20", "27017", "");
            var coll = db.GetCollection<Product>();
            //create a new object to be added to the collection
            var obj = new Product();
            obj._id = ObjectId.NewObjectID();
            obj.Title = "Shoes";
            //save the object
            coll.Insert(obj);
            //find the object
            var obj2 = coll.FindOne(new { _id = obj._id }).First();
        }
    }
}

Open in new window

Comment
Watch Question

did you download and install that assemblies.

<Product> is name as generics in c# i.e. used for array/collection of objects of type Product same template in c++.

For further reference, (if you cant see code click on c#)
http://msdn.microsoft.com/en-us/library/512aeb7t(VS.80).aspx


In order to create object of Mongo you have to add reference. You need to download that assembly(dll) from http://github.com/atheken/NoRM/downloads inorder to run the code.
Commented:
Rajesh's answer is correct.  
To answer your other question, db.GetCollection<Product> appears to be a template function rather than a template class.  It's the same basic concept; you're passing in the type, Product, so that it can specialize the function to make it work for that type.  
The syntax is fine but you do nee the assembly that Rajesh pointed out since you obviously cant specialize the function without the type being used to specialize it.
Good luck man :)
-w00te

Author

Commented:
OK, so the 'Product' is some other object (as it happens, not included in the download).

So with regard to assemblies, as I already have the 'Norm' included, I would need the Product assembly so that the compiler can do its stuff with the GetCollection template syntax.

It is rather annoying when 'hello world' type examples on how to use a library don't include all the required objects.  Ho hum!

Thanks for your help both

BT