Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 10-10-09   #1 (permalink)
Linux Lobbyist
 
Dethredic's Avatar
 
intel nvidia

Join Date: Jun 2007
Location: Ontario, Canada
Posts: 1,029

Rep: 48 Dethredic is acknowledged by some
Unique Rep: 44
Trader Rating: 1
Default Help Understanding c# Objects

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 am not sure if that is correct, or how this would be used in a program, because there seems to be easier ways to do it.

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.

System: The WC3 Machine
CPU
E6420 @ 2.8
Motherboard
Asus P5Q
Memory
4GB OCZ Platinum DDR2 800
Graphics Card
XFX 8800GTS 640mb
Hard Drive
250GB Westren Digital
Sound Card
Sound Blaster XtremeGamer
Power Supply
OCZ GameXStream 700 watt
Case
Antec 900
OS
Arch Linux x86_64
Monitor
Samsung 226BW
Dethredic is offline   Reply With Quote
Old 10-10-09   #2 (permalink)
*cough* Stock *cough*
 
intel nvidia

Join Date: Jun 2008
Posts: 139

Rep: 5 Viscerous Unknown
Unique Rep: 5
Trader Rating: 0
Default

You shouldn't set the value of the circumference in that property. It's a radius property as you specified.
__________________
System: First Build
CPU
i7 920 @ 4.0 Ghz
Motherboard
Foxconn Bloodrage Gti
Memory
3x2gb OCZ Gold
Graphics Card
EVGA GTX 275
Hard Drive
WD Caviar Black 640gb
Power Supply
Corsair TX750
Case
HAF 922
OS
Windows 7
Monitor
Samsung SyncMaster 226BW
Viscerous is online now   Reply With Quote
Old 10-10-09   #3 (permalink)
.
 
BiG O's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Virginia
Posts: 3,589

Trader Rating: 4
Default

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.

System: My System
CPU
Q6700
Motherboard
Gigabyte p35-DS4
Memory
8GB Crucial Ballistix DDR2-800 (4x2GB)
Graphics Card
BFG 8800GTS 512MB g92
Hard Drive
2X 500GB Seagate Barracuda 7,200 RPM SATA RAID 0
Sound Card
Razer Barracuda AC-1 7.1
Power Supply
ABS 700W Modular
Case
Antec 900
CPU cooling
Apogee GTZ
GPU cooling
Stock
OS
Vista Ultimate x64 SP1, Xubuntu 8.10, Windows 7
Monitor
Hanns-G 28" & Dell 19" widescreen
BiG O is offline Overclocked Account   Reply With Quote
Old 10-10-09   #4 (permalink)
Linux Lobbyist
 
Dethredic's Avatar
 
intel nvidia

Join Date: Jun 2007
Location: Ontario, Canada
Posts: 1,029

Rep: 48 Dethredic is acknowledged by some
Unique Rep: 44
Trader Rating: 1
Default

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.

System: The WC3 Machine
CPU
E6420 @ 2.8
Motherboard
Asus P5Q
Memory
4GB OCZ Platinum DDR2 800
Graphics Card
XFX 8800GTS 640mb
Hard Drive
250GB Westren Digital
Sound Card
Sound Blaster XtremeGamer
Power Supply
OCZ GameXStream 700 watt
Case
Antec 900
OS
Arch Linux x86_64
Monitor
Samsung 226BW
Dethredic is offline   Reply With Quote
Old 10-10-09   #5 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Quote:
Originally Posted by Dethredic View Post
Code:
    public double Circumference
    {
    	set
    	{
    		circumference = value;
    	}
    }
If it was me, I wouldn't store both the radius and the circumference, since when one is updated the other has to be updated as well:

Code:
    public double Circumference
    {
    	set
    	{
    		radius = RadiusFromCircumference(value);
    	}
    }
	
    private double RadiusFromCircumference(double circumference)
    {
        // TODO!
        return 0;
    }
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month

Last edited by Spotswood : 10-10-09 at 11:10 PM
Spotswood is offline   Reply With Quote
Old 10-11-09   #6 (permalink)
4.0 GHz
 
dharmaBum's Avatar
 
intel nvidia

Join Date: Apr 2007
Location: Raleigh, NC
Posts: 747

Rep: 121 dharmaBum is acknowledged by manydharmaBum is acknowledged by many
Unique Rep: 89
Trader Rating: 0
Default

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:
reset_circ(){ circumference=2*pi*Radius; }
and replace your line setting the circumference with a call to this function (note you can do this in your constructor as well). This way, if in the future you need to update this code, let's say you need to put the area of a circle as another property, you only need to update this function.

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:
There are only two industries that refer to their customers as ‘users’.

System: Europa
CPU
E8500 4.36ghz @ 1.36v
Motherboard
EVGA 780i SLi P05 Bios
Memory
G.SKILL 4GB(2x2GB) @ 924MHz (5-4-4-12-2T)
Graphics Card
2xEVGA 8800GTS (G92) 512MB @800/2000/2110
Hard Drive
Seagate 500gbx2, (fake-)RAID0
Sound Card
Sound Blaster X-Fi XtremeGamer
Power Supply
CORSAIR 1000HX 1000W
Case
Gigabyte GZ-FA2CA-AJB Black Aluminum
CPU cooling
TDX 775 Block, 360 BlackIce rad
GPU cooling
MAZE5x2, TT copper HS
OS
Fedora10-86_64/Vista64
Monitor
22" Samsung SyncMaster 2232BW
dharmaBum is online now   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 11:28 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.14655 seconds with 8 queries