473,594 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ 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.P RCalculations.L egLength1.get'" I don't
understand why it is using the get instead of the set. This is my class:
// internal fields
int m_iDistanceBetw eenBeams;
double m_dLegLength1;
double m_dLegLength2;

//
// properties
//

public double LegLength1
{
get { return m_dLegLength1; }
set { m_dLegLength1=v alue; }
}

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 1596
"Phill" <Ph***@discussi ons.microsoft.c omwrote in message
news:B4******** *************** ***********@mic rosoft.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.LegL ength1 = 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.P RCalculations.m _dLegLength1'"

Thanks for your response.

"Alberto Poblacion" wrote:
"Phill" <Ph***@discussi ons.microsoft.c omwrote in message
news:B4******** *************** ***********@mic rosoft.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.LegL ength1 = 1;
May 28 '07 #3
"Phill" <Ph***@discussi ons.microsoft.c omwrote in message
news:DA******** *************** ***********@mic rosoft.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.P RCalculations.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(Le g1.Text.ToStrin g());
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.P RCalculations.m _dLegLength1'"

So how should the property be defined?


"Alberto Poblacion" wrote:
"Phill" <Ph***@discussi ons.microsoft.c omwrote in message
news:DA******** *************** ***********@mic rosoft.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.P RCalculations.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***@discussi ons.microsoft.c omwrote in message
news:DA******** *************** ***********@mic rosoft.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.P RCalculations.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***@discussi ons.microsoft.c omwrote in message
news:9F******** *************** ***********@mic rosoft.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(Le g1.Text.ToStrin g());
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.P RCalculations.m _dLegLength1'"

So how should the property be defined?


"Alberto Poblacion" wrote:
>"Phill" <Ph***@discussi ons.microsoft.c omwrote in message
news:DA******* *************** ************@mi crosoft.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.P RCalculations.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
2478
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 the properties is a follows (irrelevant code omitted):
2
1753
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 a component when another component is destroyed ? I have a managed component called P. Let us say that C is another managed component. If on P I have: __property C * get_CComp(); __property void set_CComp(C *);
0
5557
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 is used to view the state of the running jobs and schedule new jobs. The control also runs in the context of Internet Explorer (we do this so the administrators of the jobs can always receive the latest control). The property grid is used to...
3
6739
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 following default properties: Height = 32px, Width = 144px. I declare the Width property in my control as... \\\
15
1891
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 two? (1)
27
5630
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
2036
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 you could prevent someone from stealing your idea and exploiting it to make a profit? What incentive would there be for you to be innovative, creative and ambitious if you couldn’t be sure that your ideas would be protected? Enter intellectual...
0
1978
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 with. First we have an interface defining how a property can be used (read and written - for simplicity, I ignored read-only and write-only variants). template<typename Tclass Property { public:
0
25537
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 inadequate to say the least, but I was determined to come up with the correct answer, especially since my primary background is Visual Basic, and I knew that there was an answer. Well, I did come up with an answer and I decided to incorporate it into...
1
1349
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 NumberLines property. I can set it to 4 but when it returns it is set to 1 Public Class Profile Private ProfileFileName As String Private ProfileDescription As String Private ProfileVersion As Integer Private...
0
7947
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8255
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8010
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6665
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5413
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2389
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 we have to send another system
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1217
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.