473,382 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Mixing object model and interface model.

Hi to all,

Having come from a delphi environment, I remember that when using
interfaces, we were told not to mix the object models and interface
models. In other words, when I create an object, return and interface
straight away, instead of creating an object of type object and then
getting the interface. The reason given for this was that when passing
interfaces around, the interface could
release the object and then if I try to use the object reference, I
would
get access violations.

In C# I have seen examples like:

Person myPerson;
AddressInterface IAddress;
myPerson = Person.Create;
AddressInterface = myPerson as IAddress;

etc.

Is this correct or should I do :

AddressInterface = Person.Create;

In which case I never ever use myPerson.

TIA
Steven.
Nov 15 '05 #1
3 4255
Steven,

I think the question is moot, because if you are going to use an
interface model, then you should be using a class factory to create the
classes, and have the factory return the interface. However, I really don't
see the difference between:

Person myPerson;
AddressInterface IAddress;
myPerson = Person.Create;
AddressInterface = myPerson as IAddress;

And:

AddressInterface = Person.Create;

Ultimately though, I would do this:

AddressInterface IAddress = Person.Create;

Only because it saves me three lines of code (as opposed to four or two
in the examples you gave, the second example requires two because you need
the declaration still).

Also, I would only use the interface if there wasn't anything exposed in
the underlying type of the implementation that you needed to access.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com
"Steven Quail" <sq******@yahoo.com> wrote in message
news:3a**************************@posting.google.c om...
Hi to all,

Having come from a delphi environment, I remember that when using
interfaces, we were told not to mix the object models and interface
models. In other words, when I create an object, return and interface
straight away, instead of creating an object of type object and then
getting the interface. The reason given for this was that when passing
interfaces around, the interface could
release the object and then if I try to use the object reference, I
would
get access violations.

In C# I have seen examples like:

Person myPerson;
AddressInterface IAddress;
myPerson = Person.Create;
AddressInterface = myPerson as IAddress;

etc.

Is this correct or should I do :

AddressInterface = Person.Create;

In which case I never ever use myPerson.

TIA
Steven.

Nov 15 '05 #2
Hi Nicholas,

Thank you for your reply.

What the problem with mixing models with delphi was that if I did the
following:

Person myPerson;
myPerson = Person.Create;

DealWithAddress(myPerson as IAddress); // where parameter is passed
by value.

myPerson.DoOtherThing; // Leads to an access violation.
....

Then once DealWithAddress is finished, the reference count goes down
and myperson is destroy. If I use myPerson I will get an access
violation.

This was true if DealWithAddress had the IAddress parameter by value.

My concern is in C#, would this still occur?

TIA
Steven.

"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote in message news:<O$**************@TK2MSFTNGP11.phx.gbl>...
Steven,

I think the question is moot, because if you are going to use an
interface model, then you should be using a class factory to create the
classes, and have the factory return the interface. However, I really don't
see the difference between:

Person myPerson;
AddressInterface IAddress;
myPerson = Person.Create;
AddressInterface = myPerson as IAddress;

And:

AddressInterface = Person.Create;

Ultimately though, I would do this:

AddressInterface IAddress = Person.Create;

Only because it saves me three lines of code (as opposed to four or two
in the examples you gave, the second example requires two because you need
the declaration still).

Also, I would only use the interface if there wasn't anything exposed in
the underlying type of the implementation that you needed to access.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com
"Steven Quail" <sq******@yahoo.com> wrote in message
news:3a**************************@posting.google.c om...
Hi to all,

Having come from a delphi environment, I remember that when using
interfaces, we were told not to mix the object models and interface
models. In other words, when I create an object, return and interface
straight away, instead of creating an object of type object and then
getting the interface. The reason given for this was that when passing
interfaces around, the interface could
release the object and then if I try to use the object reference, I
would
get access violations.

In C# I have seen examples like:

Person myPerson;
AddressInterface IAddress;
myPerson = Person.Create;
AddressInterface = myPerson as IAddress;

etc.

Is this correct or should I do :

AddressInterface = Person.Create;

In which case I never ever use myPerson.

TIA
Steven.

Nov 15 '05 #3
BS
You will not have this problem with .NET. The CLR does not engage in
reference-counting; the garbage collector dynamically scans for
orphaned objects in the background. No object is destroyed as long as
there is at least one reference to it somewhere in the application,
whether that reference is typed as an interface, or as the object's
class, or a base class of the object's class.

sq******@yahoo.com (Steven Quail) wrote in message news:<3a**************************@posting.google. com>...
Hi Nicholas,

Thank you for your reply.

What the problem with mixing models with delphi was that if I did the
following:

Person myPerson;
myPerson = Person.Create;

DealWithAddress(myPerson as IAddress); // where parameter is passed
by value.

myPerson.DoOtherThing; // Leads to an access violation.
...

Then once DealWithAddress is finished, the reference count goes down
and myperson is destroy. If I use myPerson I will get an access
violation.

This was true if DealWithAddress had the IAddress parameter by value.

My concern is in C#, would this still occur?

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is...
4
by: Cristian Tota | last post by:
Hi, I'd appreciate any thoughts on mixing C++ and C code. I have a project that uses a given C interface, the rest of the project can be either in C or C++. What would be the recomended design...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
5
by: Ram | last post by:
Hi Friends I want to develope a custom control in .net which can be used with any project. I am writing a function in that class which I want to take any object as parameter. For that I have...
6
by: ziman137 | last post by:
Hello all, I have a question and am seeking for some advice. I am currently working to implement an algorithmic library. Because the performance is the most important factor in later...
12
by: Doug | last post by:
Hi, I learned a little about the model view presenter pattern at a conference this last week and am experimenting with it. It's working pretty well but I have a question. I am trying to use...
5
by: jehugaleahsa | last post by:
Hello: I am trying to find what is the very best approach to business objects in Windows Forms. Windows Forms presents an awesome opportunity to use DataTables and I would like to continue doing...
3
by: H. S. Lahman | last post by:
Responding to siddharthkhare... Ignore Topmind and frebe. They are anti-OO P/R guys. Let's not confuse things with specific 3GL syntax. At the OOA/D level the model looks like: | 1
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.