471,334 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 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 911
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Seth Gecko | last post: by
13 posts views Thread by rkausch | last post: by
15 posts views Thread by Lloyd Dupont | last post: by
2 posts views Thread by ADN | last post: by
26 posts views Thread by raylopez99 | last post: by

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.