By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Generic and COM

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.