472,796 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Passing large amounts of data between classes/functions

Hi,

I'm writing an application that will pass a large amount of data between
classes/functions.

In C++ it was more efficient to send a pointer to the object, e.g structure
rather than passing the actual structure itself.

Is this true of C# also?

What is the best way to pass a large amount of data with different data types?

e.g object could contain some strings, doubles, bytes etc.

Regards
Macca

Jan 23 '06 #1
11 4027
Macca,

I would return a reference to a DataSet, which has the appropriate
information (if it is something where the structure is fluid), or create an
instance of a class which has the specific information, which you can then
return a reference to (instances of classes, not structures are reference
types, and you pass around the reference).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
Hi,

I'm writing an application that will pass a large amount of data between
classes/functions.

In C++ it was more efficient to send a pointer to the object, e.g
structure
rather than passing the actual structure itself.

Is this true of C# also?

What is the best way to pass a large amount of data with different data
types?

e.g object could contain some strings, doubles, bytes etc.

Regards
Macca

Jan 23 '06 #2
Macca wrote:
I'm writing an application that will pass a large amount of data between
classes/functions.

In C++ it was more efficient to send a pointer to the object, e.g structure
rather than passing the actual structure itself.

Is this true of C# also?

What is the best way to pass a large amount of data with different data types?

e.g object could contain some strings, doubles, bytes etc.


Unless you're dealing with large value types (structs) you don't need
to worry about this. See
http://www.pobox.com/~skeet/csharp/parameters.html

Jon

Jan 23 '06 #3
It might be worth pointing out instances are sent by reference in C#
e.g.

private void foo(MyObject obj)
{

}

'obj' is a reference. The exception to this rule is value types
(structs) which includes the String class.

Jan 23 '06 #4
Just to be clear... strings are reference types ... it sounds like your
calling them value types.
"john doe" <sl********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
It might be worth pointing out instances are sent by reference in C#
e.g.

private void foo(MyObject obj)
{

}

'obj' is a reference. The exception to this rule is value types
(structs) which includes the String class.

Jan 23 '06 #5
Hi John,

Intersting article. So if I want to pass around a data structure that
contains different types it is best to use a reference object such as a
class? rather than a value type such as a structure?

I am thinking from a performance view.

My app will have 6-7 modules (classes) and will be passing the above data
structure between them.

Regards
Macca

"Jon Skeet [C# MVP]" wrote:
Macca wrote:
I'm writing an application that will pass a large amount of data between
classes/functions.

In C++ it was more efficient to send a pointer to the object, e.g structure
rather than passing the actual structure itself.

Is this true of C# also?

What is the best way to pass a large amount of data with different data types?

e.g object could contain some strings, doubles, bytes etc.


Unless you're dealing with large value types (structs) you don't need
to worry about this. See
http://www.pobox.com/~skeet/csharp/parameters.html

Jon

Jan 23 '06 #6
Hi Nicholas,

Thanks for the advice.

Just to let you know my application will consist of 6-7 modules(classes) and
this data structure(consisting of various data types) will be used to pass
data between them. Is it best to use a class for the data structure rather
than any other data type?

Regards
Macca

"Nicholas Paldino [.NET/C# MVP]" wrote:
Macca,

I would return a reference to a DataSet, which has the appropriate
information (if it is something where the structure is fluid), or create an
instance of a class which has the specific information, which you can then
return a reference to (instances of classes, not structures are reference
types, and you pass around the reference).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
Hi,

I'm writing an application that will pass a large amount of data between
classes/functions.

In C++ it was more efficient to send a pointer to the object, e.g
structure
rather than passing the actual structure itself.

Is this true of C# also?

What is the best way to pass a large amount of data with different data
types?

e.g object could contain some strings, doubles, bytes etc.

Regards
Macca


Jan 23 '06 #7
Macca,

If the data structure is large, and you have to pass it around often,
then yes, a class is better, since you will be copying the data on each pass
of the data from one method to the next. If the structure is large, you
will see some performance impact.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
Hi Nicholas,

Thanks for the advice.

Just to let you know my application will consist of 6-7 modules(classes)
and
this data structure(consisting of various data types) will be used to pass
data between them. Is it best to use a class for the data structure rather
than any other data type?

Regards
Macca

"Nicholas Paldino [.NET/C# MVP]" wrote:
Macca,

I would return a reference to a DataSet, which has the appropriate
information (if it is something where the structure is fluid), or create
an
instance of a class which has the specific information, which you can
then
return a reference to (instances of classes, not structures are reference
types, and you pass around the reference).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
> Hi,
>
> I'm writing an application that will pass a large amount of data
> between
> classes/functions.
>
> In C++ it was more efficient to send a pointer to the object, e.g
> structure
> rather than passing the actual structure itself.
>
> Is this true of C# also?
>
> What is the best way to pass a large amount of data with different data
> types?
>
> e.g object could contain some strings, doubles, bytes etc.
>
> Regards
> Macca
>


Jan 23 '06 #8
Macca wrote:
Intersting article. So if I want to pass around a data structure that
contains different types it is best to use a reference object such as a
class? rather than a value type such as a structure?

I am thinking from a performance view.

My app will have 6-7 modules (classes) and will be passing the above data
structure between them.


I very, very rarely write my own value types. Performance is usually
less important than getting the right semantics, to be honest - and
reference semantics *tend* to be preferable to value semantics IMO. Of
course, just because they're reference types doesn't mean they have to
be mutable... often an immutable reference type (such as string) gives
the best performance *and* semantics.

Jon

Jan 23 '06 #9
In reference to the above about strings, they behave like value types.
I don't know the internal workings well enough to say whether a
reference is passed or not, however use them like value types is the
easiest solution. e.g.:

private void button1_Click(object sender, System.EventArgs e)
{
string s = "moo";
this.foo(s);
MessageBox.Show(s);
}

private void foo(string s)
{
s = "hello";
}

Displays a message box with "Moo", not "hello".

Jan 24 '06 #10
This is the normal behaviour for both reference and value types (when
re-assigning the argument without the ref keyword); I think you are
confusing reference types with the ref keyword - but they mean different
things.

Marc
Jan 24 '06 #11
Chris S. wrote:
In reference to the above about strings, they behave like value types.
Well, I think it's more accurate to say that they behave like immutable
reference types. When you pass them around, a reference is passed, not
the actual text data.

If you treat it like a value type, you *might* be tempted to pass it by
reference for efficiency reasons if you've got a large string - when in
fact it would have no performance benefit (and may even harm
performance slightly).
I don't know the internal workings well enough to say whether a
reference is passed or not, however use them like value types is the
easiest solution. e.g.:

private void button1_Click(object sender, System.EventArgs e)
{
string s = "moo";
this.foo(s);
MessageBox.Show(s);
}

private void foo(string s)
{
s = "hello";
}

Displays a message box with "Moo", not "hello".


Indeed, and if you use any other type it would do the same thing there,
because s is not passed *by* reference. Changing the value of a
parameter doesn't change the value used for the argument unless it's an
argument which is passed by reference.

Now, not that changing the contents of an object that a parameter
refers to is not the same thing.

To make this concrete, I think we can all agree that StringBuilder is a
reference type. If you changed your foo method to:

private void foo (StringBuilder sb)
{
sb = new StringBuilder ("hello");
}

you'd still see the same behaviour - the caller wouldn't notice
anything.

If you changed the method to:

private void foo (StringBuilder sb)
{
sb.Append ("hello");
}

*then* they'd notice a difference.

And that highlights the difference between String and StringBuilder -
String doesn't have any methods which change the data stored in the
String; it's immutable.

Knowing this difference is important because it suggests that often
when you want what are often called "value type semantics" you can
achieve them with something which is still very definitely a reference
type, by making it immutable (at least to the outside world) in the
same way that String is immutable.

Jon

Jan 24 '06 #12

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

Similar topics

2
by: flamesrock | last post by:
Hi, Basically, what I'm trying to do is store large amounts of data in a list or dictionary and then convert that to a custom formatted xml file. My list looks roughly like this: (d,r,p]]])...
1
by: michaaal | last post by:
If I use a form to pass data (say, for example, through a textbox) the data seems to be limited to somewhat smaller amounts. What should I do if I want to pass a large amount of data? For example...
10
by: Digety | last post by:
We are looking to store a large amount of user data that will be changed and accessed daily by a large number of people. We expect around 6-8 million subscribers to our service with each record...
4
by: oshanahan | last post by:
Does anyone have ideas on the best way to move large amounts of data between tables? I am doing several simple insert/select statements from a staging table to several holding tables, but because...
3
by: Wayne Marsh | last post by:
Hi all. I am working on an audio application which needs reasonably fast access to large amounts of data. For example, the program may load a 120 second stereo sound sample stored at 4bytes per...
2
by: Dennis C. Drumm | last post by:
What is the best way to add several pages of text to a readonly TextBox? The text does not change and was created in a Word rtf document but could as easly be put in a ASCII text file. Can this be...
7
by: =?Utf-8?B?TW9iaWxlTWFu?= | last post by:
Hello everyone: I am looking for everyone's thoughts on moving large amounts (actually, not very large, but large enough that I'm throwing exceptions using the default configurations). We're...
4
by: bcomeara | last post by:
I am writing a program which needs to include a large amount of data. Basically, the data are p values for different possible outcomes from trials with different number of observations (the p...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.