Generic and COM

  • Thread starter Thread starter Sylfelin
  • Start date Start date
S

Sylfelin

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<ObjetApp1> app=new GenericObjetApp<ObjetApp1>();

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

app.myObjetApp.Close();

}
}
 
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<ObjetApp1> app=new GenericObjetApp<ObjetApp1>();

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

    app.myObjetApp.Close();

  }

}

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<ObjetApp1> app=new
GenericObjetApp<ObjetApp1>();
app.myObjetApp.Close();
}

if (version == "2")
{
GenericObjetApp<ObjetApp2> app=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<ObjetApp1> app=new
GenericObjetApp<ObjetApp1>();
}
else
{
GenericObjetApp<ObjetApp2> app=new
GenericObjetApp<ObjetApp2>();
}

app.myObjetApp.Close();

}

}

Have a nice day
Nikhil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top