πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.csharpbuilder.language.csharp
Article #31

Re: Boxing and unboxing

#31
From: Joanna Carter [TeamB]
Date: Sat, 06 May 2006 17:42
91 lines
4512 bytes
"Andre" <andretruter@webmail.co.za> a οΏ½crit dans le message de news: 
445cd440@newsgroups.borland.com...

| Ok, my code worked, but then i remembered about the ref keyword. Isn't it
| better to use the ref prefix in the method signature rather than the 
boxing?
| Using boxed object means that i have to work with object-type, but i'd
| rather work with the known types. Which is preformance wise a better 
choice?
|
| private void doSome(ref DataSet _ds)
| {
|    adp.Fill(_ds);
| }

Once again, I will say that what you are doing is *not* boxing!! All you are 
doing is casting and, as Marc said, there is no penalty casting from DataSet 
to object and a lesser penalty than boxing a value type when casting from 
object to DataSet.

You should always work with known types, whatever the situation, if at all 
possible.

As to using the ref keyword, it will make no difference performance wise. A 
non-ref parameter is really a reference to an object as well; the only 
difference is the way the reference is passed, "by value" or "by reference". 
"By value" means that, whatever you do to that reference inside the method, 
it won't affect the reference passed in. "By reference" means that you can 
change the reference inside the method and it will affect the reference 
passed that was passed in.

In summary, looking at the limited code you gave in your original post, I 
will comment inline :

| The mission is to fill a Dataset in DAL by using a DataSet object
| instantiated by BLL assembly.
| ( i havn;t tested this code. it's in theory correct...i think)
|
| BLL assembly:
| public void addTestTable()
| {
|    //NOTE: the DataSet ds has a table already defined during design time.
| an additional table (testTable) must be added to the
|    //DataSet ds. For preformance optimization I want to use the refrence 
of
| the exiting DataSet ds and not a copy thereof.

Passing a DataSet or any other class type as a parameter "by value" is no 
less performant than "by reference". "By value" is *not* passing a copy, 
that only applies to value types, not reference (class) types. "By value" is 
passing the original object, not a copy.

Because you don't want to change the instance that the parameter is holding, 
you should not pass it "by reference"; all you are wanting to do is to 
change its internal state.

|    //How feasible is this approach? Is it worth the hype? And will this
| work?
|
|    DAL.FillWithData( "SELECT * FROM testTable" ,  (object)ds
| ,"myTestTable");

What is the point of casting the DataSet to object, when you are going to 
have to cast it back inside the method ?? If you know you have a DataSet 
outside the method and you need a DataSet inside the method, then pass it as 
a DataSet parameter.

Your code should look like this :

BLL assembly.
public void addTestTable()
{
    DAL.FillWithData( "SELECT * FROM testTable",  ds, "myTestTable");
}

DAL assembly:
public void FillWithData(string sql, DataSet dataSource, string tableName)
{
        //assuming all connections are made and commandtext set.
        mysqlAdapter.Fill(dataSource, tableName);
}

Just BTW, If you are building a DAL, you really shouldn't be allowing SQL 
outside of the DAL. The business layer should no nothing at all about how 
the objects are being stored, tables, fields, databases, etc.

Joanna

-- 
Joanna Carter [TeamB]
Consultant Software Engineer 
Technical Details
Message-ID: <445ce011$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!news.glorb.com!ecngs!feeder.ecngs.de!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <445bb0fa@newsgroups.borland.com> <445bcf41@newsgroups.borland.com> <16i3bfc9nqjks.dlg@dlg.marcrohloff.com> <445cd440@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,17b83a1499eb2170
X-Google-Attributes: gid1efe35,public
X-Google-Language: ENGLISH,ASCII
From: "Joanna Carter [TeamB]" <joanna@not.for.spam>
Newsgroups: borland.public.csharpbuilder.language.csharp
References: <445bb0fa@newsgroups.borland.com> <445bcf41@newsgroups.borland.com> <16i3bfc9nqjks.dlg@dlg.marcrohloff.com> <445cd440@newsgroups.borland.com>
Subject: Re: Boxing and unboxing
Date: Sat, 6 May 2006 18:42:33 +0100
Lines: 93
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
NNTP-Posting-Host: 82.69.239.102
Message-ID: <445ce011$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1146937361 82.69.239.102 (6 May 2006 10:42:41 -0700)
X-Authenticated-User: jcarter
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!news.glorb.com!ecngs!feeder.ecngs.de!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail