473,473 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to set a Property?

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.
May 28 '07 #1
6 1588
"Phill" <Ph***@discussions.microsoft.comwrote in message
news:B4**********************************@microsof t.com...
[...] "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;
May 28 '07 #2
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" wrote:
"Phill" <Ph***@discussions.microsoft.comwrote in message
news:B4**********************************@microsof t.com...
[...] "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;
May 28 '07 #3
"Phill" <Ph***@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
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.

May 28 '07 #4
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?


"Alberto Poblacion" wrote:
"Phill" <Ph***@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
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.

May 28 '07 #5
I figured it out. I needed to define my variables as static.

"Alberto Poblacion" wrote:
"Phill" <Ph***@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
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.

May 28 '07 #6
PS

"Phill" <Ph***@discussions.microsoft.comwrote in message
news:9F**********************************@microsof t.com...
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:
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?


"Alberto Poblacion" wrote:
>"Phill" <Ph***@discussions.microsoft.comwrote in message
news:DA**********************************@microso ft.com...
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.


May 28 '07 #7

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

Similar topics

3
by: Johnny M | last post by:
using Access 2003 Pardon the subject line, but I don't have a better word for this strange behavior (or behavior I don't understand!!!) I have a class module named DepreciationFactor. One of...
2
by: Edward Diener | last post by:
How does one specify in a component that a property is a pointer to another component ? How is this different from a property that is actually an embedded component ? Finally how is one notified in...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
27
by: sklett | last post by:
I just found myself doing something I haven't before: <code> public uint Duration { get { uint duration = 0; foreach(Thing t in m_things) { duration += t.Duration;
15
by: Lauren Wilson | last post by:
Owning your ideas: An essential tool for freedom By Daniel Son Thinking about going into business? Have an idea that you think will change the world? What if you were told that there was no way...
0
by: Memfis | last post by:
While I was looking through the group's archives I came across a discussion on doing properties (as known from Delphi/C#/etc) in C++. It inspired me to do some experimenting. Here's what I came up...
0
ADezii
by: ADezii | last post by:
The motivation for this Tip was a question asked by one of our Resident Experts, FishVal. The question was: How to Declare Default Method/Property in a Class Module? My response to the question was...
1
by: cday119 | last post by:
I have a Class with about 10 properties. All properties return right except for one. It is real annoying and I can't see why its not working. Maybe someone else can see something. It is the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.