Thursday, March 29, 2012

object reference a mystery

Hi

Im busy building a class where i want the class members to have access to and be able to manipulate items on a web page.
Surely if i declare an instance of my page class (MyPage_Default) in this new class then i should have access to its members?

Could somebody please have a look at my code and tell me what i could be doing wrong?
thanks

Here is the code: (first my new class and then code for my page's code behind)

public class wizard

{

public int myNumber;

public void SetMyNumber(int newNumber)

{

myNumber = newNumber;

}

public void work()

{

MyPage_Default i = new MyPage_Default();

i.label("hello");

}

}

public partial class MyPage_Default : System.Web.UI.Page

{

wizard w = new wizard();

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

w.SetMyNumber(10);

}

w.work();

}

protected void btnNext_Click(object sender, EventArgs e)

{

w.SetMyNumber(30);

w.work();

}

public void PageDo()

{

Label1.Text = w.myNumber.ToString();

}

public void label(string newValue)

{

Label1.Text = newValue;

}

}

Hi,

there are several solutions to this. 2 that pop up in my mind are these:

1<%@. Page Language="C#" %>23<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">45<script runat="server">67 protected void Page_Load(object sender, EventArgs e)8 {9 ContextTest.SetNumber(13);10 ContextTest.SetNumberTry2(Label2, 8);11 }1213</script>1415<html xmlns="http://www.w3.org/1999/xhtml">16<head runat="server">17 <title>Untitled Page</title>18</head>19<body>20 <form id="form1" runat="server">21 <div>22 <asp:Label ID="Label1" runat="server"></asp:Label>23 <asp:Label ID="Label2" runat="server"></asp:Label>24 </div>25 </form>26</body>27</html>

and in the App_Code subfolder I put this class:

1using System;2using System.Data;3using System.Configuration;4using System.Web;5using System.Web.Security;6using System.Web.UI;7using System.Web.UI.WebControls;8using System.Web.UI.WebControls.WebParts;9using System.Web.UI.HtmlControls;1011/// <summary>12/// Summary description for ContextTest13/// </summary>14public class ContextTest15{16public static void SetNumber(int number)17 {18 ((Label)((Page)HttpContext.Current.CurrentHandler).FindControl("Label1")).Text = number.ToString();19 }2021public static void SetNumberTry2(Label control,int number)22 {23 control.Text = number.ToString();24 }25}

As you can see, you don't need to instantiate another object of the type of your particular class. In the SetNumber method I just pass an integer and in the method I get the CurrentHandler, which is the page, that runs in the current context. After casting you can use the FindControl method to find the Label1 and set its text.

In SetNumberTry2 however I just pass the Label2 control directly, which references the instance of the Label2 control, and set its Text property directly to the number.

Grz, Kris.


Hi Kris.

Thanks for your quick reply.


For some reason the first method doesn't work :( It gies me the same error. (Object reference...)
The second however does.

Once again thanks a mil! This will definitely be very useful in time to come
gem-code!


Hi,

Dewald:

For some reason the first method doesn't work :( It gies me the same error. (Object reference...)

can you provide the error you're getting? Did you put the class in the App_Code subfolder or not?

Grz, Kris.


Hi again!

Nah the code is def in the App_Code folder.

This is how i implement it:
//Show the current step. Assign to some label specified
public void ShowStep()
{
//control.Text = WizardCurStep.ToString();
((Label)((Page)HttpContext.Current.CurrentHandler).FindControl("Label1")).Text = WizardCurStep.ToString();
}

WizardCurStep is defined up in the same page as int

Here is the error im getting:

Object reference not set to an instance of an object.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 47: {Line 48: //control.Text = WizardCurStep.ToString();Line 49: ((Label)((Page)HttpContext.Current.CurrentHandler).FindControl("Label1")).Text = WizardCurStep.ToString();Line 50: }Line 51:


Dewald:

WizardCurStep is defined up in the same page as int

In the page but you don't pass it as an input parameter to your method in the custom class.

Grz, Kris.


Hi.

I'm trying to seperate the values used by the wizard class from the actual page that uses the wizard. Therefore ive declare several integers in the seperate class so that it can keep track of those values itself.
(Instead of having to assign values to viewstate or some label etc)

public class wizard
{
private int WizardMinStep;
private int WizardMaxStep;
private int WizardCurStep;

// Class Contructor
public wizard(int MinStep, int MaxStep)
{
WizardMinStep = MinStep;
WizardMaxStep = MaxStep;
}

//Step Forward
public void StepForward(Button control)
{
if (WizardCurStep >= WizardMaxStep)
{
control.Enabled = false;
}
else
{
WizardCurStep++;
control.Enabled = true;
}

//Show the current step. Assign to some label specified
public void ShowStep(Label control)
{
control.Text = WizardCurStep.ToString();
}

}

I wont be able to pass these values to the methods from the page without the page 'knowning' what they are.
Surely this is the same as you suggested?


Ok so ive given this a serious go now.
Actually my logic seem to work just fine. The code does increment the value set at the top of the page when i click my forward button and the "StepForward()" method is called. Only one problem now...
When i click the forward button for the second time... it doesn't increment that value again to 3. It just stays there on 2. (I pass to the class constructor the innitial value to 1)
Since VS doesn't want to break into debugger so i can step through the code i have to assume that the variable is stuck in some way.

I use the word 'stuck' because the value isn't incremeted beyond 2, however once i call the "StepPrevious()" method the value is decremented back to 1.
So it isn't a case of either loosing the value or it being set back to the innitial value of 1 with every server round trip. (If it was reset during server round trip the value would have been 0)

Kris would you know why this is? I know how to fix this problem using ViewState however that is not my prefered way of going about this.

Anyways i know this is off the subject i posted under.
Your advice so far will help me quite a bit in the future.

Thanks again!!


Ok so I’ve given this a serious go now.
Actually my logic seem to work just fine. The code does increment the value set at the top of the page when i click my forward button and the "StepForward()" method is called. Only one problem now...
When i click the forward button for the second time... it doesn't increment that value again to 3. It just stays there on 2. (I pass to the class constructor the initial value to 1)
Since VS doesn't want to break into debugger so i can step through the code i have to assume that the variable is stuck in some way.

I use the word 'stuck' because the value isn't incremented beyond 2, however once i call the "StepPrevious()" method the value is decremented back to 1.
So it isn't a case of either loosing the value or it being set back to the initial value of 1 with every server round trip. (If it was reset during server round trip the value would have been 0)

Kris would you know why this is? I know how to fix this problem using ViewState however that is not my preferred way of going about this.

Anyways I know this is off the subject i posted under.
Your advice so far will help me quite a bit in the future.

Thanks again!!

0 comments:

Post a Comment