|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Help Understanding c# Objects
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||
|
Linux Lobbyist
![]() |
So, I have some basic programming knowledge but I can't seem to understand objects, or a reason for using them until programs get really big.
I was asked to write a property to access (get) and mutate (set) the value of the circle's radius. Note that you will also need to recalculate the circumference of the circle when the radius is updated. Code:
using System;
class Circle
{
double radius;
double circumference;
public Circle( double r )
{
radius = r;
circumference = 2.0d * Math.PI * radius;
}
// my additions start
public double Radius
{
get
{
return radius;
}
set
{
radius = value;
circumference = 2.0d * Math.PI * radius;
}
}
// my additions stop
public override string ToString( )
{
string s;
s = string.Format(
"Radius = {0:F2} m, Circumference = {1:F2} m",
radius, circumference );
return s;
}
}
I have read a bunch of google results but I can't say they helped.
__________________
There are 10 types of people in this world. Those who can read binary, and those who can't.
|
|||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||
|
*cough* Stock *cough*
|
You shouldn't set the value of the circumference in that property. It's a radius property as you specified.
__________________
|
||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
.
![]() |
What you have is right. That's how you do getters and setters in C#. Not sure why you would think there's an easier way though.
You do, however, need to move the circumference statement out of the setter field. You could call it outside if you wanted.
__________________
Imaging with Windows PE Please keep the OCN Terms of Service in mind when posting.
|
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||
|
Linux Lobbyist
![]() |
so would something like this be better:
Code:
// my additions start
public double Radius
{
get
{
return radius;
}
set
{
radius = value;
}
}
public double Circumference
{
set
{
circumference = value;
}
}
// my additions stop
__________________
There are 10 types of people in this world. Those who can read binary, and those who can't.
|
|||||||||||
|
|
|
|
|
#5 (permalink) | |
|
Case Modder
![]() |
Quote:
Code:
public double Circumference
{
set
{
radius = RadiusFromCircumference(value);
}
}
private double RadiusFromCircumference(double circumference)
{
// TODO!
return 0;
}
__________________
Rich Custom Wooden Case Builder
Last edited by Spotswood : 10-10-09 at 11:10 PM |
|
|
|
|
|
|
#6 (permalink) | |||||||||||||||
|
4.0 GHz
![]() |
Your first try was fine, but it would probably be best coding practise to define a function that resets the value of the circumference.
Quote:
This is the power of using objects. You are correct in saying there are probably much simpler ways of doing this specific assignment without using objects, but down the road it may so happen that this code needs to get modified for a larger purpose, and this is where OO programming starts to shine.
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s Programming Quote of the Day: Bjarne Stroustrup: Quote:
|
|||||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|