473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Generic and COM

Hello,

In an object COM of an editor (without source), I have several versions
of an object. ObjectApp1 and ObjectApp2.

ObjectApp2 is a copy of ObjectApp1 but which implements more methods
and properties.

I would like to be able to write a generic class. I try the code below
but that does not compile because the declaration is in the IF
statement.

Has you he a solution for me ?

Thank you
class GenericObjetApp<T>
{
public T myObjetApp
}

class Program
{
static void Main(string[] args)
{
string version = "2";

if (version == "1")
GenericObjetApp<ObjetApp1app=new GenericObjetApp<ObjetApp1>();

if (version == "2")
GenericObjetApp<ObjetApp2app=new GenericObjetApp<ObjetApp2>();

app.myObjetApp.Close();

}
}

--

--------------------------
Merci
Sylfelin
Jul 4 '08 #1
1 961
On Jul 4, 1:38*pm, Sylfelin <sylfelin_EN_TR...@cegetel.netwrote:
Hello,

In an object COM of an editor (without source), I have several versions
of an object. ObjectApp1 and ObjectApp2.

ObjectApp2 is a copy of ObjectApp1 but which implements more methods
and properties.

I would like to be able to write a generic class. I try the code below
but that does not compile because the declaration is in the IF
statement.

Has you he a solution for me ?

Thank you

class GenericObjetApp<T>
{
* public T myObjetApp

}

class Program
{
* static void Main(string[] args)
* {
* * string version = "2";

* * if (version == "1")
* * * GenericObjetApp<ObjetApp1app=new GenericObjetApp<ObjetApp1>();

* * if (version == "2")
* * * GenericObjetApp<ObjetApp2app=new GenericObjetApp<ObjetApp2>();

* * app.myObjetApp.Close();

* }

}

--

--------------------------
Merci
Sylfelin
Hi Sylfelin,

This happening because you are allocation memory to the objects with
in the if condition . When you are compiling the code , compiler is
checking the sure way of memory allocation for the object app.But here
if the version will be something except "1" or "2" then the object
app will never get the memory allocation as there is no else block. So
it will generae error "Use of unassigned local variable app".

Here is two solutions for your code..

First Solution :---
---------------------

class GenericObjetApp<T>
{
public T myObjetApp
}

class Program
{
static void Main(string[] args)
{
string version = "2";

if (version == "1")
{
GenericObjetApp<ObjetApp1app=new
GenericObjetApp<ObjetApp1>();
app.myObjetApp.Close();
}

if (version == "2")
{
GenericObjetApp<ObjetApp2app=new
GenericObjetApp<ObjetApp2>();
app.myObjetApp.Close();
}

}

}

Second Solution :---
---------------------

class GenericObjetApp<T>
{
public T myObjetApp
}

class Program
{
static void Main(string[] args)
{
string version = "2";

if (version == "1")
{
GenericObjetApp<ObjetApp1app=new
GenericObjetApp<ObjetApp1>();
}
else
{
GenericObjetApp<ObjetApp2app=new
GenericObjetApp<ObjetApp2>();
}

app.myObjetApp.Close();

}

}

Have a nice day
Nikhil
Jul 4 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
3
by: Boris | last post by:
I have a class which should like this ideally: generic <typename T> public ref class ManagedClass { T ^managedMember; UnmanagedClass<U*unmanagedMember; }; I actually would like to specify...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
15
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a...
2
by: ADN | last post by:
Hi, I have a method which calls my service factory: class person { public int ID { get; set: } public string Name {get; set;} } IList<Personmypeople = __serviceFactory.Fetch(new Person())
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.