Class vs. Struct - Newbie question & app.

R

Russell

I am extremly new to c# - I have been programming in VB6 for 9 years.

I am going to migrate a program that I created years ago in VB6 to C# (just
for fun! for those interested).

I have an object called a motor. This motor has a table (not an equation),
that has two parameters gallons per minute of fluid at a pressure. It has an
output of revolutions per minute at pounds per inch of torque.

These numbers are in a table, so I will need to extrapolate from the table
any intermediate values.

I will need to get maximum and minimum values.

I will have several motors.

Now my question - is this a class or a struct.

Motor A and Motor B will need to be inquired the same - but I will not
modify any thing about A or B, they are strictly driven by data in file Z.

So the fact that there is no modification of the object by the program is it
a struct?
 
R

Russell

I think what I want is a structure that has classes.
A class for each motor.
Then a structure that handles the motor.

I want to be able to ask with 2 parameters - give me the motor for this with
the results in decending order.

This is more than the "hello world" question.
 
F

fidel

Hi Russell,

yep - if you create a struct and pass it to a function, it will be just
be a copy. so the function can modify it till it's heart's content, and
it wont affect the original struct.

If you tried the same with a class though, you would be passing it "by
reference" so to speak, and therefore that method could modify it.
 
B

Bruce Wood

Russell said:
I think what I want is a structure that has classes.
A class for each motor.
Then a structure that handles the motor.

I want to be able to ask with 2 parameters - give me the motor for this with
the results in decending order.

This is more than the "hello world" question.

You want a class. If you search this newsgroup for "class vs struct" or
just "class struct" you'll find lots of discussions about when to use
structs, which is quite rarely.

The only things I saw in the description of your problem that cried
"struct" to me were the following:

gallons per minute
fluid at a pressure
revolutions per minute at pounds per inch of torque

In other words, quantities that might act like values upon which you
could conceivably do mathematical operations. Those are good candidates
for structs. To give you an example, in my system I have a struct
called Measure, which has two fields: a decimal quantity and a
UnitOfMeasure (an immutable reference type). I can, for example, add
"two feet" to "five inches" and get a result of "29 inches" out, like
this:

Measure one = new Measure(2.0, UnitOfMeasure.Feet);
Measure two = new Measure(4.0, UnitOfMeasure.Inches);
Measure sum = two + one;

These things act like values, like int or double. I don't think that
you want a Motor to act like that.

Anyway, poke around this newgroup for discussions of struct / class and
you'll find lots of talk about this.
 
B

Bill Butler

fidel said:
Hi Russell,

yep - if you create a struct and pass it to a function, it will be
just
be a copy. so the function can modify it till it's heart's content,
and
it wont affect the original struct.

If you tried the same with a class though, you would be passing it "by
reference" so to speak, and therefore that method could modify it.

Any "Sane" design would keep the table private inside the class so I
doubt this would be a problem.

A class is the way to go.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

fidel said:
yep - if you create a struct and pass it to a function, it will be just
be a copy. so the function can modify it till it's heart's content, and
it wont affect the original struct.

If you tried the same with a class though, you would be passing it "by
reference" so to speak, and therefore that method could modify it.

Strictly speaking it will be passing a reference by value.

The method can change the object passed, but it can not
change the original reference to point to another object.

That still requires the ref keyword.

Arne
 
B

Bruce Wood

Bill said:
Any "Sane" design would keep the table private inside the class so I
doubt this would be a problem.

Well, yes, but I think that fidel was implying that by using a struct,
instances passed to methods could be altered by the method, even
indirectly through the passed instance's interface, and this wouldn't
affect the value that the caller passed. fidel wasn't specific about
whether the called method would mess with the object's state directly
or through its interface.

Regardless, fidel's original statement is accurate only if the
information stored inside the struct is all in the form of value types
(primitive types / other structs). If the table of information, for
example, is a mutable class, then the called method can make changes to
the struct that will persist after the called method returns.

All in all, it's a poor reason for using struct instead of class. If
you did want to pass a reference object (a class instance) to a method
and be insulated to any changes that the method makes, you'd be far
better off creating a class with a Clone method, and passing a clone.
A class is the way to go.

Yes, but it's the way to go because of the semantics of what a "Motor"
is, not because of insulating one's self from change across method
calls, or not.
 
B

Bill Butler

Bruce Wood said:
Well, yes, but I think that fidel was implying that by using a struct,
instances passed to methods could be altered by the method, even
indirectly through the passed instance's interface, and this wouldn't
affect the value that the caller passed. fidel wasn't specific about
whether the called method would mess with the object's state directly
or through its interface.
<snip>
In a vacuum, I guess you could interpret fidel's response two ways.
1. If you wish to allow the user to "Play" with a table (change values),
then struct does this automatically.

2. If a user changes your data, it won't effect the original version.

The first implies that this class/struct is "Meant" for manipulation by
the end-user. I suspect that this is the way you (Bruce) interpreted his
statement. unfortunately this is not supported in the original post.
Russel desires a class that encapsulates some behavior that is
implemented via interpolating data in a lookup table. I can think of No
reason why you would ever make the internal lookup data public. If
anything this class should simply have a method(s) that
Takes input requests
Looks up the correct response(using interpolation) in the table.
Return the response.
Different "motor" objects could have different table data, but the table
itself should never change.
Therefore, I think it is safe to assume that a "motor class" would
not want to allow the end-user to be tampering with the data.


The second implies that using a struct is a form of "security against
tampering" . This is the way I interpreted the statement. As I pointed
out, it is far more preferable to make this a class with a private
lookup table and public methods to access the table.

I think that regardless of interpretation, a struct is a poor choice for
needs of the problem.

Hopefully this clarifies my position
Bill
 
J

Jon Skeet [C# MVP]

fidel said:
yep - if you create a struct and pass it to a function, it will be just
be a copy. so the function can modify it till it's heart's content, and
it wont affect the original struct.

Unless, of course, a member of the struct is a mutable reference
type... it's worth clarifying that when a struct value is copied, it's
only a shallow copy.
If you tried the same with a class though, you would be passing it "by
reference" so to speak, and therefore that method could modify it.

Note that it's not *really* passed by reference. That means something
different, and is available with the "ref" keyword.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top