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 > Application Programming

Reply
 
LinkBack Thread Tools
Old 11-15-07   #1 (permalink)
4.0 GHz
 
guitar22891's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Florida
Posts: 997

Rep: 46 guitar22891 is acknowledged by some
Unique Rep: 38
FAQs Submitted: 4
Trader Rating: 0
Default C# - Throwing an ArgumentOutOfRangeException

Hey, working on a C# programming project and part of it is to:

Quote:
a. Throw an ArgumentOutOfRangeException exception if the argument for the on hand quantity is zero or less.
b. Throw an ArgumentOutOfRangeException exception if the argument for the product number is zero or less.
c. Create the ArgumentOutOfRangeException with an appropriate error message for 4a - 4b above.
Does this just mean inserting a try / catch??? code and what I have so far is below...


Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace ProductMod
{
    public abstract class Product
    {
        private int productNum;
        private float quantity;
        private string description, unitOfMeasure;
       
        private const string DEF_DESC = "no description";
        private const string DEF_UM = "no unit of measure";
      

        public Product()
        {
            productNum = 0;
            quantity = 0;
            description = DEF_DESC;
            unitOfMeasure = DEF_UM;
        }// default constructor for each value

        public Product(int prN, float qtd, string desc, string un)
        {
            productNum = prN;
            quantity = qtd;

            if (desc != null && desc != "") // property method
                description = desc;
            else
                description = DEF_DESC;

            if (desc != null && un != "")// property method
                unitOfMeasure = un;
            else
                unitOfMeasure = DEF_UM;
        }//overload constructor for each variable

        public abstract double calcStdCost();
        // abstract calcStdCost method

        public override string ToString()
        {
                return
                productNum + " " +
                quantity + " " +
                description + " " +
                unitOfMeasure;       
        }// public method tostring that overides ToString method of Object class

        public int ProductNumber
        {
            get
            {
                return productNum;
            }
            set
            {
                if (value > 0)
                    productNum = value;
                // only update if value is greater than 0
            }
        }

        public float HandOnQuantity
        {
            get
            {
                return quantity;
            }
            set
            {
                if (value > 0) quantity = value;
                // only update if value is greater than 0
            }
        }
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                if (value != null && value != "") description = value;
                // only update if not blank
            }
        }
        public string UnitOfMeasure
        {
            get
            {
                return unitOfMeasure;
            }
            set
            {
                if (value != null && value != "") unitOfMeasure = value;
                // only update if not blank
            }
        }

    }
}
__________________
Future Upgrades:
  • Another 1gb of GSKILL DDR500
  • Better Graphics Card
Jesus is my Savior

System: My System
CPU
3.0E E0 Presscott @ 3.85
Motherboard
P4P800 SE
Memory
1GB DDR500 Gskill HZ
Graphics Card
FX5500 256mb 128bit AGP
Hard Drive
200GB Sata1.5 Maxtor
Sound Card
Onboard
Power Supply
400w FSP-PN
Case
Xoxide 5 in 1 combo
OS
Windows XP Home & Ubuntu
Monitor
19in Flat Panel
guitar22891 is offline   Reply With Quote
Old 11-15-07   #2 (permalink)
Bifford
 
BFRD's Avatar
 
intel nvidia

Join Date: Dec 2004
Location: Carrollton, TX
Posts: 4,414

FAQs Submitted: 8
Folding Team Rank: 31
Hardware Reviews: 2
Trader Rating: 11
Default

I would use an if statement and then "throw" a specific error depending on the situation. You might even create a private void that throws the error (since it will be reused).
__________________
Helpful Posts (Hopefully )
Overclocker's Calculator
Photo Editing - B&W w/Color Accents

System: Main Rig
CPU
E6700 Conroe
Motherboard
Abit QuadGT
Memory
2GB G.Skill PC2 8000 (HZ)
Graphics Card
EVGA 8800GTX
Hard Drive
150 GB Raptor X / 300GB Storage
Sound Card
Audigy 2 ZS
Power Supply
PCP&C Silencer 750
Case
Sigma Shark
CPU cooling
Stock (for now)
GPU cooling
Stock
OS
Vista Ultimate
Monitor
Dual Samsung 204b
BFRD is offline I fold for Overclock.net Overclocked Account BFRD's Gallery   Reply With Quote
Old 11-15-07   #3 (permalink)
4.0 GHz
 
guitar22891's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Florida
Posts: 997

Rep: 46 guitar22891 is acknowledged by some
Unique Rep: 38
FAQs Submitted: 4
Trader Rating: 0
Default

Quote:
Originally Posted by BFRD View Post
I would use an if statement and then "throw" a specific error depending on the situation. You might even create a private void that throws the error (since it will be reused).
hmm.. can you give me an example for a?
__________________
Future Upgrades:
  • Another 1gb of GSKILL DDR500
  • Better Graphics Card
Jesus is my Savior

System: My System
CPU
3.0E E0 Presscott @ 3.85
Motherboard
P4P800 SE
Memory
1GB DDR500 Gskill HZ
Graphics Card
FX5500 256mb 128bit AGP
Hard Drive
200GB Sata1.5 Maxtor
Sound Card
Onboard
Power Supply
400w FSP-PN
Case
Xoxide 5 in 1 combo
OS
Windows XP Home & Ubuntu
Monitor
19in Flat Panel
guitar22891 is offline   Reply With Quote
Old 11-15-07   #4 (permalink)
Programmer
 
intel nvidia

Join Date: Jan 2007
Posts: 101

Rep: 8 Kirmie Unknown
Unique Rep: 6
Folding Team Rank: 778
Trader Rating: 0
Default

He means use the if statement to check for your error inside a try/catch. Then if the if statement is true then do something allong the line of
Code:
throw new ArgumentOutOfRangeException("Your error statement");
and have that exception get cought and handled by the catch.
__________________
From American Dad:
I touched her hand... her hand touched her boob. By the transitive property, I got some boob! Algebra's awesome!

System: My giant
CPU
E6600
Motherboard
Asus P5n32-E 680i
Memory
2x1GB Corsair XMS2 PC6400
Graphics Card
GeFroce 7950 GT 256MB
Hard Drive
WDJS SATA-II 160GB + WD80GB + WDAAKS Raid0 320GB
Power Supply
Apevia DarkSide 600W
Case
NZXT Zero
CPU cooling
Stock
GPU cooling
Stock
OS
XP Media Center
Monitor
19" LCD
Kirmie is offline I fold for Overclock.net   Reply With Quote
Old 11-15-07   #5 (permalink)
Programmer
 
stupid's Avatar
 
amd nvidia

Join Date: Jan 2006
Location: Baghdad, Iraq
Posts: 1,243

Rep: 148 stupid is acknowledged by manystupid is acknowledged by many
Unique Rep: 117
FAQs Submitted: 1
Trader Rating: 4
Default

U plugged that all in and I got no exceptions... were you using it in another method? Just the declarations like that shouldn't cause an error...
__________________
"If there is a god, I hope he has a good excuse"
Woody Allen

System: Minazo
CPU
Opteron 1210
Motherboard
Biostar TForce 550 SE
Memory
2x1GB G.Skill DDR2 800
Graphics Card
eVGA 8800GT
Hard Drive
500GB 16MB Buf Samsung
Power Supply
Corsair HX520W Modular
Case
CoolerMaster Centurion 5
CPU cooling
CoolerMaster GeminII & Scythe SFF21Ds
GPU cooling
Accelero S1 & Scythe SFF21D
OS
XP Pro SP2
Monitor
Dual monitors: 22" & 19" Widescreens
stupid is offline   Reply With Quote
Old 11-15-07   #6 (permalink)
Programmer
 
intel nvidia

Join Date: Jan 2007
Posts: 101

Rep: 8 Kirmie Unknown
Unique Rep: 6
Folding Team Rank: 778
Trader Rating: 0
Default

Quote:
Originally Posted by stupid View Post
U plugged that all in and I got no exceptions... were you using it in another method? Just the declarations like that shouldn't cause an error...
Unless I'm missing something guitar22891 is trying to create the throw of an error when the number is out of the range of what it should be, not what it can be. Like when the "on hand quantity" is 0 or less. If it is zero you do not have any on hand, and if its negative, well, have you every had -2 apples for lunch? Dosen't make sense but the variable is able to hold the number so you have to specifically check the variable and create a custom throw to be used if the number is incorrect.

Also, I haven't had time to check the code I put above but it should work. If not you have to do something very similar and you can find it by googling "c# force throw error" or something like that
__________________
From American Dad:
I touched her hand... her hand touched her boob. By the transitive property, I got some boob! Algebra's awesome!

System: My giant
CPU
E6600
Motherboard
Asus P5n32-E 680i
Memory
2x1GB Corsair XMS2 PC6400
Graphics Card
GeFroce 7950 GT 256MB
Hard Drive
WDJS SATA-II 160GB + WD80GB + WDAAKS Raid0 320GB
Power Supply
Apevia DarkSide 600W
Case
NZXT Zero
CPU cooling
Stock
GPU cooling
Stock
OS
XP Media Center
Monitor
19" LCD
Kirmie is offline I fold for Overclock.net   Reply With Quote
Old 11-18-07   #7 (permalink)
4.0 GHz
 
guitar22891's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Florida
Posts: 997

Rep: 46 guitar22891 is acknowledged by some
Unique Rep: 38
FAQs Submitted: 4
Trader Rating: 0
Default

Where would I throw the exception though?

Could someone give me an example? Help?
__________________
Future Upgrades:
  • Another 1gb of GSKILL DDR500
  • Better Graphics Card
Jesus is my Savior

System: My System
CPU
3.0E E0 Presscott @ 3.85
Motherboard
P4P800 SE
Memory
1GB DDR500 Gskill HZ
Graphics Card
FX5500 256mb 128bit AGP
Hard Drive
200GB Sata1.5 Maxtor
Sound Card
Onboard
Power Supply
400w FSP-PN
Case
Xoxide 5 in 1 combo
OS
Windows XP Home & Ubuntu
Monitor
19in Flat Panel
guitar22891 is offline   Reply With Quote
Old 11-18-07   #8 (permalink)
IDDQD
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,912

Rep: 296 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 226
FAQs Submitted: 6
Folding Team Rank: 289
Hardware Reviews: 9
Trader Rating: 1
Default

Here's a quick example. It doesn't really apply to your code, and it also probably isn't correct for syntax (I only code Java/C), but you get the idea of how they work.

Code:
public int checkProductStock(int prN)
        {
            int[] product = {12345,21314,12312};
            if (prN > -1 && prN < 3) 
                return product[prN];
            else
                throw new ArgumentOutOfRangeException();

        }
Then in your code where you're using that function, you would go:

Code:
try{

qtyRemaining = checkProductStock(prodNum);

}catch(ArgumentOutOfRangeException() ex){
//do whatever task you want to react to the invalid number
}

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business x64
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 11-18-07   #9 (permalink)
4.0 GHz
 
guitar22891's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Florida
Posts: 997

Rep: 46 guitar22891 is acknowledged by some
Unique Rep: 38
FAQs Submitted: 4
Trader Rating: 0
Default

THanks!! This makes a lot more sense. Now for my code should I throw the exception in the constructor, overload constructor, or the get / set??

This is what I'm thinking:

Code:
public int ProductNumber
        {
            get
            {
                return productNum;
            }
            set
            {
                if (value <= 0)
                    throw new ArgumentOutOfRangeException
                        ("Argument for Product Number is 0 or Less");
                else
                    productNum = value;
               
            }
        }
__________________
Future Upgrades:
  • Another 1gb of GSKILL DDR500
  • Better Graphics Card
Jesus is my Savior

System: My System
CPU
3.0E E0 Presscott @ 3.85
Motherboard
P4P800 SE
Memory
1GB DDR500 Gskill HZ
Graphics Card
FX5500 256mb 128bit AGP
Hard Drive
200GB Sata1.5 Maxtor
Sound Card
Onboard
Power Supply
400w FSP-PN
Case
Xoxide 5 in 1 combo
OS
Windows XP Home & Ubuntu
Monitor
19in Flat Panel

Last edited by guitar22891 : 11-18-07 at 06:05 PM.
guitar22891 is offline   Reply With Quote
Reply



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



All times are GMT -4. The time now is 03:31 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

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