View Single Post
  #1 (permalink)  
Old August 24th, 2010, 06:19 PM
chroniclemaster1 chroniclemaster1 is offline
Friend of Wrox
Points: 1,749, Level: 16
Points: 1,749, Level: 16 Points: 1,749, Level: 16 Points: 1,749, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2007
Location: San Diego, CA, USA.
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default Adding generic data to a class

I'm having a couple basic generics problems. Basically, I'm trying to identify a piece of content by two indexes.

1) I need to store the content in the class, so I need to create a class variable, classTableCellContent, to store it in. Therefore I need to create one, requiring me to constrain the generic type "where ContentType : new()". However, when I attempt to consume this type with a string, e.g. TableCell<string>, it throws a compiler error "The type 'string' must have a public parameterless constructor in order to use it as a parameter blahblahblah..."

2) I need to accept input, so I have an add function. However, when I try to indicate that the third parameter is the same data type, i.e. ContentType, the compiler seems to believe that I'm creating a "new" data type with a poorly chosen name that hides the original data type declared in the class declaration. I'm sure this must be something stupid I'm missing. All I want to do is accept an input variable of the generic type in a function and store it in the class for later use.

Here's the code. I've highlighted the lines that seem to be throwing the errors.

Code:
public class TableCell<ContentType> where ContentType : new()
    {
        private int classRowIndex = 0;
        private int classColumnIndex = 0;
        private ContentType classTableCellContent = new ContentType();
        
        
        /// <summary>
        /// TableCell<ContentType>() - Constructors for creating a new TableCell
        /// </summary>
        public TableCell() {}
        public TableCell(int inputRowIndex, int inputColumnIndex, ContentType inputTableCellContent)
        {
            addContent<ContentType>(inputRowIndex, inputColumnIndex, inputTableCellContent);
        }
        
        
        /// <summary>
        /// void addContent<ContentType>(int inputRowIndex, int inputColumnIndex, ContentType inputTableCellContent) - accepts row and column indexesa dictionary and returns the XHTML to display all it's contents as a list
        /// </summary>
        public void addContent<ContentType>(int inputRowIndex, int inputColumnIndex, ContentType inputTableCellContent)
        {
            classRowIndex = inputRowIndex;
            classColumnIndex = inputColumnIndex;
            classTableCellContent = inputTableCellContent;
        }
    }
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.