By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,953 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,953 IT Pros & Developers. It's quick & easy.

sending DataTable by ref - is there a way to do it, and any sense of doing it ?

P: n/a
Hi,

I'm wondering if there is a way to send a method parametrs by ref when
DataTabel is a type of this value ?

I done some sort of select over DataTable columns, just by removing them
froma table is each of them isn't on a stirng[], but right no I have to do
it in a wat of:
1. passing DataTable to method (lets call it SelectOverDataTabel)
2. declare a local (in SelectOver...) DataTable, inicialize it with my value
3. doing all things on this local copy (removing.....)
4. and to the parent method send a local copy by return, sth like this
dtMyTable = clsLocalSQL.SelectOverDataTable( dtMyTable,
StringOfSelectedColumns)

Could I do it without closing DataTable in (object) and unpacking it in body
of my static procedure ??

--
best regards
stic
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Stefan:

I'm not entirely sure I understand the problem but you can definitely pass a
DataTable by reference. However, remember this is a reference type to begin
with and there's a lot to a datatable to keep track of if it's size is very
big so you may see all sorts of performance degradation depending on use.
It's often easier to pass the individual rows of the datatable which are
small and easier to work with.

I'm not sure what you mean by Closing the datatable though, there is no open
or closed state to a DataTable, only the database if that's where its filled
from. If you are referrring to the underlying connection, there's no reason
to leave it open here that I can see.

I'm not sure what the goal is here but if you could explain it a little
more, I could be of more help. Also, if you look at MS's Data Access
Application Block (DAAB) it returns Datasets (so DataTables are essentially
in the same boat) in the executeDataSet method. You can pass these things
over layers, back and forth to web services, you name it you can pass it.

Here's a typical scenario. I have a method that fills a datatable and
returns it:

Declare a datatable and cal lthe method:

DataTable dt = myDataAccessClass.GetDataTable();

//now a method in myDataAccessClass
private DataTable GetDataTable(){
//Open Connection,
DataTable dt = new DataTable();
//Fill datatable
return dt;
}

This is fine to do, is this essentially what you're asking?
"Stefan Turalski (stic)" <st*************@kruk-inkaso.com.pl> wrote in
message news:us*************@TK2MSFTNGP10.phx.gbl...
Hi,

I'm wondering if there is a way to send a method parametrs by ref when
DataTabel is a type of this value ?

I done some sort of select over DataTable columns, just by removing them
froma table is each of them isn't on a stirng[], but right no I have to do
it in a wat of:
1. passing DataTable to method (lets call it SelectOverDataTabel)
2. declare a local (in SelectOver...) DataTable, inicialize it with my value 3. doing all things on this local copy (removing.....)
4. and to the parent method send a local copy by return, sth like this
dtMyTable = clsLocalSQL.SelectOverDataTable( dtMyTable,
StringOfSelectedColumns)

Could I do it without closing DataTable in (object) and unpacking it in body of my static procedure ??

--
best regards
stic

Nov 16 '05 #2

P: n/a
Hi,

Thanks for you answer but what I need to do is:
1. take one DataTable filled with for example 12 Columns named by letters
A,B,C,...
DataTable dtMyTable = new DataTable();
//sqlconnection code
dataadapert.Fill(dtMyTable) <- when commandtext is "Select A,B,C,D,F,G,...
from sometable
2. take one string[] with allow me to filter this DataTable for examle new
string[] {"A", "C", "H"}
3. pass them to method in static class like this
csLocalSql.SelectOverDataTable(dtMyTable, new string[] {"A", "C", "H"})
4. Then at this method look in loop for every dtMyTable column name, and if
is on this string list I just pass it, if no do
remove("name_with_is_not_at_list_of_string");
5. I return DataTable without columns I don' need.

So as you see I could make local copy of DataTable with I already have afert
fill, and all logic is on local side... And I really don't think that there
is anything unusal - for performence it is even better becouse I done refer
to SQL sever with any select.

Now the question is, how to pass the DataTable to this method as in/out ->
ref, in a way to avoid making local DataTable for with this passed by
reference (by pointer to the structure on stack), and in way to do all this
removes direct on my DataTable..
By closing in (object) I mean boxing and unboxing... I could do it becouse
when I try to do sth like this

csLocalSql.SelecOverDataTabel( ref dtMyTable, new string [] .....);
to method with wait for ref DataTable dtMyTable, string[] str)

compiler shout at me with 'you can't use ref over a this type...

--
best regards
stci

Użytkownik "William Ryan eMVP" <bi**@NoSp4m.devbuzz.com> napisał w
wiadomo¶ci news:eS**************@TK2MSFTNGP10.phx.gbl...
Stefan:

I'm not entirely sure I understand the problem but you can definitely pass a DataTable by reference. However, remember this is a reference type to begin with and there's a lot to a datatable to keep track of if it's size is very big so you may see all sorts of performance degradation depending on use.
It's often easier to pass the individual rows of the datatable which are
small and easier to work with.

I'm not sure what you mean by Closing the datatable though, there is no open or closed state to a DataTable, only the database if that's where its filled from. If you are referrring to the underlying connection, there's no reason to leave it open here that I can see.

I'm not sure what the goal is here but if you could explain it a little
more, I could be of more help. Also, if you look at MS's Data Access
Application Block (DAAB) it returns Datasets (so DataTables are essentially in the same boat) in the executeDataSet method. You can pass these things
over layers, back and forth to web services, you name it you can pass it.

Here's a typical scenario. I have a method that fills a datatable and
returns it:

Declare a datatable and cal lthe method:

DataTable dt = myDataAccessClass.GetDataTable();

//now a method in myDataAccessClass
private DataTable GetDataTable(){
//Open Connection,
DataTable dt = new DataTable();
//Fill datatable
return dt;
}

This is fine to do, is this essentially what you're asking?
"Stefan Turalski (stic)" <st*************@kruk-inkaso.com.pl> wrote in
message news:us*************@TK2MSFTNGP10.phx.gbl...
Hi,

I'm wondering if there is a way to send a method parametrs by ref when
DataTabel is a type of this value ?

I done some sort of select over DataTable columns, just by removing them
froma table is each of them isn't on a stirng[], but right no I have to do it in a wat of:
1. passing DataTable to method (lets call it SelectOverDataTabel)
2. declare a local (in SelectOver...) DataTable, inicialize it with my

value
3. doing all things on this local copy (removing.....)
4. and to the parent method send a local copy by return, sth like this
dtMyTable = clsLocalSQL.SelectOverDataTable( dtMyTable,
StringOfSelectedColumns)

Could I do it without closing DataTable in (object) and unpacking it in

body
of my static procedure ??

--
best regards
stic


Nov 16 '05 #3

P: n/a
Stefan Turalski (stic) <st*************@kruk-inkaso.com.pl> wrote:
Thanks for you answer but what I need to do is:
1. take one DataTable filled with for example 12 Columns named by letters
A,B,C,...
DataTable dtMyTable = new DataTable();
//sqlconnection code
dataadapert.Fill(dtMyTable) <- when commandtext is "Select A,B,C,D,F,G,...
from sometable
2. take one string[] with allow me to filter this DataTable for examle new
string[] {"A", "C", "H"}
3. pass them to method in static class like this
csLocalSql.SelectOverDataTable(dtMyTable, new string[] {"A", "C", "H"})
4. Then at this method look in loop for every dtMyTable column name, and if
is on this string list I just pass it, if no do
remove("name_with_is_not_at_list_of_string");
5. I return DataTable without columns I don' need.

So as you see I could make local copy of DataTable with I already have afert
fill, and all logic is on local side... And I really don't think that there
is anything unusal - for performence it is even better becouse I done refer
to SQL sever with any select.


I think you're missing the difference between reference types and value
types. I certainly don't think you need to pass the DataTable by
reference in this case - passing the reference by value (the default)
is fine, I believe.

See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a

Uzytkownik "Jon Skeet [C# MVP]" <sk***@pobox.com> napisal w wiadomosci
news:MP************************@msnews.microsoft.c om...
Stefan Turalski (stic) <st*************@kruk-inkaso.com.pl> wrote:
Thanks for you answer but what I need to do is:
1. take one DataTable filled with for example 12 Columns named by letters A,B,C,...
DataTable dtMyTable = new DataTable();
//sqlconnection code
dataadapert.Fill(dtMyTable) <- when commandtext is "Select A,B,C,D,F,G,... from sometable
2. take one string[] with allow me to filter this DataTable for examle new string[] {"A", "C", "H"}
3. pass them to method in static class like this
csLocalSql.SelectOverDataTable(dtMyTable, new string[] {"A", "C", "H"})
4. Then at this method look in loop for every dtMyTable column name, and if is on this string list I just pass it, if no do
remove("name_with_is_not_at_list_of_string");
5. I return DataTable without columns I don' need.

So as you see I could make local copy of DataTable with I already have afert fill, and all logic is on local side... And I really don't think that there is anything unusal - for performence it is even better becouse I done refer to SQL sever with any select.


I think you're missing the difference between reference types and value
types. I certainly don't think you need to pass the DataTable by
reference in this case - passing the reference by value (the default)
is fine, I believe.

See http://www.pobox.com/~skeet/csharp/parameters.html


Hi,

Could be ;-)

Ok, so as far I as understood if I use ref before parametr, in method
inicialization / calls these DataTable should be send by value (as I
thought) and all opperation could be done right on this table, or I'm
lossing something and there will be no change when I use it ?

The thing btw. is that you couldn't send DataTable as a ref parametr... -
and that is why I started this topic...

--
best regards
stic
Nov 16 '05 #5

P: n/a
Stefan Turalski (stic) <st*************@kruk-inkaso.com.pl> wrote:
Could be ;-)

Ok, so as far I as understood if I use ref before parametr, in method
inicialization / calls these DataTable should be send by value (as I
thought) and all opperation could be done right on this table, or I'm
lossing something and there will be no change when I use it ?
The reference is passed by value, but it doesn't create a new copy of
the object itself.

See my article for more information.
The thing btw. is that you couldn't send DataTable as a ref parametr... -
and that is why I started this topic...


You *can* pass a DataTable by reference - but you need to state that on
both the method declaration and the method call itself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.