How to set a Property?

G

Guest

I am making the transition from VB to C# and am stumped on a seemingly simple
task. I have created a class and defined a property using set/get. Now from
one of my forms I want to set the property to a value but I get a compile
error saying "An object reference is required for the nonstatic field,
method, or property 'PocketRigger.PRCalculations.LegLength1.get'" I don't
understand why it is using the get instead of the set. This is my class:
// internal fields
int m_iDistanceBetweenBeams;
double m_dLegLength1;
double m_dLegLength2;

//
// properties
//

public double LegLength1
{
get { return m_dLegLength1; }
set { m_dLegLength1=value; }
}

And this is how I am trying to set the value:
PRCalculations.LegLength1 = 1;

What am I going wrong? Thanks.
 
A

Alberto Poblacion

Phill said:
[...] "An object reference is required for the nonstatic field,
method, or property [...]

This means that the property is not Static, and that you are referencing
it through the name of the class instead of using an instance of the class.
And this is how I am trying to set the value:
PRCalculations.LegLength1 = 1;

You need something similar to the following:

PRCalculations myInstance = new PRCalculations();
myInstance.LegLength1 = 1;
 
G

Guest

Thanks. That works, now how do I reference those property values within my
class method? I tried this:

public static double Tension(decimal dLeg1, decimal dLeg2, int
intLoadPoint)
{
// (Point Load * Leg * HookOffset) / (VerticalTotal1 * + V2H1)
double dTension = m_dLegLength1;



return dTension;

}
and got this error: "An object reference is required for the nonstatic
field, method, or property 'PocketRigger.PRCalculations.m_dLegLength1'"

Thanks for your response.

Alberto Poblacion said:
Phill said:
[...] "An object reference is required for the nonstatic field,
method, or property [...]

This means that the property is not Static, and that you are referencing
it through the name of the class instead of using an instance of the class.
And this is how I am trying to set the value:
PRCalculations.LegLength1 = 1;

You need something similar to the following:

PRCalculations myInstance = new PRCalculations();
myInstance.LegLength1 = 1;
 
A

Alberto Poblacion

Phill said:
Thanks. That works, now how do I reference those property values within
my
class method? I tried this:

public static double Tension(decimal dLeg1, decimal dLeg2, int
intLoadPoint)
{
// (Point Load * Leg * HookOffset) / (VerticalTotal1 * + V2H1)
double dTension = m_dLegLength1;

return dTension;

}
and got this error: "An object reference is required for the nonstatic
field, method, or property 'PocketRigger.PRCalculations.m_dLegLength1'"

It's more of the same thing. You are running a static method, and
calling an instance property, which is not legal. You can write your class
so that it contains a static method that calls static properties, or a class
with an instance method that calls instance properties. Or a static method
that creates an instance of the class and then calls the instance properties
on it, etc.
Anyway, you use instance properties when you want various copies in
memory. For instance, you create a class "Widget" with a property "Size" if
you are going to use various Widgets and you need to set the Size of each
one. That would be an instance property, and any time you want to read it or
write it you need to specify which one of your multiple instances of the
Widget you want to use. This is what the compiler means when it says that
you need an "object reference". You can't refer to Widget.Size, you need to
use oneOfMyWidgets.Size.
If you are only going to use a single Widget, you may mark the property
as "static". Then it is always available as Widget.Size, without ever
creating a Widget with the "new" operator. However, if you do create several
Widgets, they will all share the same Size.

So, anyway, you need to decide how you are going to use your class, and
depending on your needs either make both the method and the properties
static, or not use "static" on any of them and create an instance of the
class (with "new") and then use that instance to operate with the class.
 
G

Guest

Thanks. I just want a static class. I have a form that gets some input from
the user and then this data is used for calculation on other pages. So I'm
think it just needs to be a static class with a few properties set in my
forms and then a static method that uses these properties to do it's
calculations. So I changed the forms to set the properties like this:
PRCalculations.LegLength1 = double.Parse(Leg1.Text.ToString());
And the property is defined like this:
public static double LegLength1
{
get { return m_dLegLength1; }
set { m_dLegLength1 = value; }
}
But I get this compile error:
"An object reference is required for the nonstatic field, method, or
property 'PocketRigger.PRCalculations.m_dLegLength1'"

So how should the property be defined?
 
P

PS

Phill said:
Thanks. I just want a static class. I have a form that gets some input
from
the user and then this data is used for calculation on other pages. So
I'm
think it just needs to be a static class with a few properties set in my
forms and then a static method that uses these properties to do it's
calculations.

Although this does work for you it is not what you really want and it is
definitely a code smell. You are basically using a static class as a global
variable holder. You should use a class instance and pass the reference to
where it is needed.

PS


So I changed the forms to set the properties like this:
 

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