Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 2nd, 2004, 12:54 AM
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ChronicCode
Default Generic Type specification during runtime

is there any way of specifying the type of a generic class during runtime instead of at compile time?

my problem is that i have file several different versions of a file with each version having different properties and methods and it is not unitl compile time that i can findout what version of the file it is. Below is examples of the classes that i have

public class version1_0
{
   //example member
   public int total;
}

public class version2_2
{
   //example member
   public string extendedDesc;
}

public Gen<T>
{
   public T file;

   //other methods are here
}

public enum Versions
{
   v1, //represents the version1_0 class
   v2 //represents the version2_2 class
}

i want to add the generic class "Gen" to the class below using the _file variable and then set the type of the generic class Gen by the version that was passed into the constructor so i can specify the type of the Gen variable at compile time only at run time.
public class myFile
{
   private Gen??? _file;

   public myFile(Versions)
   {
      //set type of _file here by using Versions enum.
   }
}

is there any other way that i could go about solving this problem?

many thanks in advance.

Glenn
 
Old December 2nd, 2004, 09:44 AM
jminatel's Avatar
Wrox Staff
Points: 18,059, Level: 58
Points: 18,059, Level: 58 Points: 18,059, Level: 58 Points: 18,059, Level: 58
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2003
Posts: 1,906
Thanks: 62
Thanked 139 Times in 101 Posts
Default

Please don't cross post. See:

http://p2p.wrox.com/topic.asp?TOPIC_ID=22912

instead

Jim Minatel
Senior Acquisitions Editor
Wiley Technology Publishing
WROX Press
Jim's Book of the week: Beginning XML 3rd Edition
 
Old December 3rd, 2004, 01:51 AM
Friend of Wrox
 
Join Date: Feb 2004
Posts: 177
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Declare an Interface and implement the interface in each of the version of classes. In the final class you declare a type for the interface, so at run time you can assign instance of any of the derived class.


It is not how much we do,
but how much love we put in the doing.

-Mother Theresa





Similar Threads
Thread Thread Starter Forum Replies Last Post
Generic return type mega C# 4 November 30th, 2006 11:00 AM
Generic return type mega C# 2005 1 November 30th, 2006 07:27 AM
Runtime Error type 3146 Bruce Sexton Beginning VB 6 0 March 30th, 2005 09:33 AM
Generic Type specification during runtime ChronicCode .NET Framework 2.0 0 December 2nd, 2004 12:55 AM





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