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