Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 24th, 2010, 06:19 PM
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.
  #2 (permalink)  
Old August 25th, 2010, 03:39 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well you're main problem is that you can't type the following code:

string s = new string();

As there is no constructor for string that takes no parameters.

It may be that you simply remove the " = new ContentType()" from the variable declaration. If you use that variable you will have to check to see if it is null, but other than that you shouldn't have any problems.

Also, it is convention to use "T" as the name of the generic type - this is much shorter and easier to read, and also makes it easier to spot where the generic type is used. ContentType could quite easily be the name of an actual class.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #3 (permalink)  
Old August 25th, 2010, 05:25 AM
Registered User
 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Response:To Generic Data usage error

Hi Chroniclemaster1, There is an error your code, which throws cannot implicitly convert type ContentType to ContentType ....
Just remove the '<ContentType>' content in addContent method and should compile fine.
The error free code is as follows:
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(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(int inputRowIndex, int inputColumnIndex, ContentType inputTableCellContent)
{
classRowIndex = inputRowIndex;
classColumnIndex = inputColumnIndex;
this.classTableCellContent = inputTableCellContent;
}
}

Hope it solves your problem.
Bye
sridhar
  #4 (permalink)  
Old August 25th, 2010, 05:45 AM
Registered User
 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Generic data var

Hi ChronicleMaster1,
What judson says is right, and apart from what i posted (corrected addContent declaration) additionally remove the new constraint.
It should work!
Good luck.
  #5 (permalink)  
Old August 25th, 2010, 12:57 PM
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

Quote:
Originally Posted by samjudson View Post
It may be that you simply remove the " = new ContentType()" from the variable declaration.
:) Got it! I'm no longer simply a consumer of generics, I'm now an author. The string problem was exactly what you pointed out Sam. Since the variable is an open generic type, I can't initialize it (like consuming an interface). Once I dropped the initialization "ContentType classTableCellContent;" that problem went away.

Quote:
Originally Posted by sridhar c View Post
Hi Chroniclemaster1, There is an error your code, which throws cannot implicitly convert type ContentType to ContentType ....
Just remove the '<ContentType>' content in addContent method and should compile fine.
Then sridhar c's fix solved my problem with the second type declaration. Now I only declare one open data type so the function is talking the same set of variable names as the class. :D

Quote:
Originally Posted by samjudson View Post
Also, it is convention to use "T" as the name of the generic type - this is much shorter and easier to read, and also makes it easier to spot where the generic type is used. ContentType could quite easily be the name of an actual class.
:) I tried changing the names on my variable types several times during testing so "ContentType" was simply the last one I'd used. I've now changed it back to my preferred TContent (using a T prefix like the very sensible "I" prefix used for interfaces). I like T because it clearly stands for type, but the typical usage for multiple types (T, U, V) seems a bit silly to me since U and V are only weakly related to the idea of type (their letters are adjacent to T which stands for type). I also have to waste brain space remembering that this T and U in one function are totally unrelated to T and U in another function / class. Using interface style naming with a T prefix like TContent greatly reduces naming collisions. This isn't entirely customary but the consistent use of T (and the variables appearance in brackets <TContent>) still make it clear for anyone that these are open generic type names. So that's what I actually use and why I prefer it.
__________________
-------------------------

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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
write a sophisticated generic class in BLL irProject BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 1 November 29th, 2009 04:05 AM
Generic Q on data bound program Minos Visual Basic 2005 Basics 1 November 9th, 2006 12:28 PM
any help for generic data grid component !!! harmeet Classic ASP Components 0 October 5th, 2006 08:00 AM
adding a class in JavaScript crmpicco Javascript How-To 5 October 4th, 2005 08:15 AM
Adding functionality to the String class? RobMeade BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 5 February 9th, 2005 02:11 PM



All times are GMT -4. The time now is 12:20 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.