473,574 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=n ew GenericObjetApp <ObjetApp1>() ;

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

app.myObjetApp. Close();

}
}

--

--------------------------
Merci
Sylfelin
Jul 4 '08 #1
1 970
On Jul 4, 1:38*pm, Sylfelin <sylfelin_EN_TR ...@cegetel.net wrote:
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=n ew GenericObjetApp <ObjetApp1>() ;

* * if (version == "2")
* * * GenericObjetApp <ObjetApp2app=n ew 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=n ew
GenericObjetApp <ObjetApp1>() ;
app.myObjetApp. Close();
}

if (version == "2")
{
GenericObjetApp <ObjetApp2app=n ew
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=n ew
GenericObjetApp <ObjetApp1>() ;
}
else
{
GenericObjetApp <ObjetApp2app=n ew
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
3305
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 way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even...
9
12810
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 implements IQueue<Tfor all types T (so it can make use of queues of various object types internally). As useful as this is, it doesn't seem possible. The...
3
2253
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) The parent form have a custom control, which have a public sub looking
13
3806
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The...
3
2185
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 the type U depending on the type T. As
7
2036
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation: displayMsgDelegate = DisplayStatusMessage; implementation: public void DisplayStatusMessage(string statusMessage)
15
2372
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 bit akin to ask: "the System.Web.UI and System.Windows.Controls namespace both contains a Control class, could I use one in place of the other? common...
2
1571
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
3604
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 delegate type expression in just the right place and a well-named method means we can almost read the code out loud and understand it without even...
2
4172
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 types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to...
0
7832
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8262
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6498
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5331
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3768
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3785
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2262
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1094
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.