Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: (untagged)
Hi,
 
I have a public function named GetQuarter() that I created under class named Default. I also created another public Class namded Class1. I tried to use the GetQuarter class in Class1. So I created an instance for the class Default and call the GetQuarter function. The GetQuarter function accesses the Default class' radiobuttion member controls and assign a value to a variable depending on the radiobutton selected. I always get an error :
 
Object reference not set to an instance of an object.
 
Here is my code from class Default
 
 
 public partial class _Default : System.Web.UI.Page
    {
public short GetQuarter(short ch)
        {
            short Q=0;
            
            switch (ch)
            {
                case 1://From
                    
                    if (radFrmDashQ1.Checked)
                        Q = 1;
                    else if (radFrmDashQ2.Checked)
                        Q = 2;
                    else if (radFrmDashQ3.Checked)
                        Q = 3;
                    else
                        Q = 4;
                break;
                
                case 2://To
                    if (radToDashQ1.Checked)
                        Q = 1;
                    else if (radToDashQ2.Checked)
                        Q = 2;
                    else if (radToDashQ3.Checked)
                        Q = 3;
                    else
                        Q = 4;
                break;
                
                default:
                    Q = 0;
                break;
            }
 
            return Q;
        }
public short GetQuarter(short ch)
        {
            short Q=0;
 
            switch (ch)
            {
                case 1://From

                    if (radFrmDashQ1.Checked)
                        Q = 1;
                    else if (radFrmDashQ2.Checked)
                        Q = 2;
                    else if (radFrmDashQ3.Checked)
                        Q = 3;
                    else
                        Q = 4;
                break;
 
                case 2://To
                    if (radToDashQ1.Checked)
                        Q = 1;
                    else if (radToDashQ2.Checked)
                        Q = 2;
                    else if (radToDashQ3.Checked)
                        Q = 3;
                    else
                        Q = 4;
                break;
 
                default:
                    Q = 0;
                break;
            }
 
            return Q;
        }
}
 
This is the code from class Class1
 
public class Class1
    {
public static bool HasErrors()
        {
          Default xDef = new Default();
bool xErr = False;
           if (xDef.GetQuarter(1) > xDef.GetQuarter(2))
                        xErr = true;
 
            return xErr;
        }
    }
 
Posted 14-Aug-12 5:43am
Comments
Wes Aday at 14-Aug-12 10:46am
   
"new Default();"... but your class is named _Default....
Franco Cipriano at 14-Aug-12 11:11am
   
sorry..i got it wrong it should be Default() not _default..
aspnet_regiis at 14-Aug-12 10:49am
   
Are you working alone on this project ? You are following wrong coding practices.. We don't create a instance of Page class and use it in a class , but vice versa...
Franco Cipriano at 14-Aug-12 16:11pm
   
Hi,
 
Thanks for the tip. I was able to solve my problem, by creating a function in the Class instead of the page and have the class called by the page. The page passes the control that needs to be evaluated as parameter to the GetQuarters() function.
 
Thanks,
aspnet_regiis -i at 12-Mar-13 4:44am
   
Deleted my solution because it was downvoted...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 14 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100