我知道我知道;但... [英] I know, I know; but...

查看:69
本文介绍了我知道我知道;但...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我永远无法解决我的定界问题,所以我正试图在最后一次试图超越这个该死的问题的过程中试图超过这个该死的问题!我会

真的很感谢有人在这里帮助我的笨蛋。


好​​吧,我有一个asp.net程序,我开始写作。我创建了一个名为tank的类

,带有一些子类(圆形和方形)。我想在运行时实例化这个坦克类型对象,并在用户选择某个东西时为其分配成员值。我一直收到有关我的实例的错误,指的是

null对象,因为在用户在运行时选择

之前,实例不存在。我尝试了几种方法,创建一个类文件和

只是将它们直接放在agitatorSz命名空间下,但似乎仍然是某种范围问题。


这是'我的班级代码和我的应用程序代码:


使用System;

使用System.Collections;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Web ;

使用System.Web.SessionState;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Web.UI.HtmlControls;


命名空间agitatorSz

{

///< ;摘要>

/// WebForm1的摘要说明。

///< / summary>

公共类main_frm:System.Web .UI.Page

{

public System.Web.UI.WebControls.Button tankType_btn;

protected System.Web.UI.WebControls。按钮dimensions_btn;

protecte d System.Web.UI.WebControls.TextBox impellers_txt;

protected System.Web.UI.WebControls.TextBox agitators_txt;

protected System.Web.UI.WebControls.Label volume_txt;

protected System.Web.UI.WebControls.Label turnover_txt;

protected System.Web.UI.WebControls.Label gpm_txt;

protected System.Web.UI.WebControls.DataGrid product_grd;

protected System.Web.UI.WebControls.TextBox length_txt;

protected System.Web.UI.WebControls.TextBox width_txt;

protected System.Web.UI.WebControls.TextBox diameter_txt;

protected System.Web.UI.WebControls.TextBox depth_txt;

protected System.Web.UI.WebControls.Button model_btn;

protected System.Web.UI.WebControls.DropDownList model_ddl;

protected System.Web.UI.WebControls.Button powerSrc_btn;

protected System.Web.UI.WebControls.DropDownList power_ddl;

protected System.Web.UI.WebControls.TextBox maxMudLvl_txt;

protected System.Web.UI.WebControls.DropDownList tankType_ddl;


private void Page_Load(object sender,System.EventArgs e)

{

//将用户代码置于此处初始化页面

}


#region Web表单设计器生成的代码

覆盖protected void OnInit(EventArgs e)

{

//

// CODEGEN:此调用是必需的ASP.NET Web窗体设计器。

//

InitializeComponent();

base.OnInit(e);

}


///< summary>

///设计师支持所需的方法 - 不要修改

///使用代码编辑器的方法的内容。

///< / summary>

private void InitializeComponent()

{

this.tankType_btn.Click + = new

System.EventHandler(this.tankType_btn_Click);

this.model_btn.Click + = new System.EventHandler(this.model_btn_Click);

this.Load + = new System.EventHandler(this.Page_Load);


}

#endregion


private void tankType_btn_Click (对象发送者,System.EventArgs e)

{

tankTypeVar = tankType_ddl.SelectedItem.Value;


switch(tankType_ddl。 SelectedItem.Value)

{

case" 1":

{

roundTank rt = new roundTank( );

rt.orientation =" test1";

//启用与课程相关的控件,禁用其他人

diameter_txt.Enabled = true ;

length_txt.Enabled = false;

width_txt.Enabled = false;

break;

}

case" 2":

{

squareTank st = new squareTank();

st.orientation =" ; test2" ;;

//启用与类相关的控件

length_txt.Enabled = true;

width_txt.Enabled = true;

diameter_txt.Enabled = false;

休息;

}

}

}


private void model_btn_Click(object sender,System.EventArgs e)

{

if(rt!= null)

{

deep_txt.Text = rt.orientation;

}

if(st!= null)

{

depth_txt.Text = st.orientation;

}

}

}

}


------------

班级代码

- ----------


使用系统;


命名空间agitatorSz

{

///< summary>

///坦克的摘要说明。

///< / summary>

公共舱坦克

{

公共水箱()

{

}

public float powerSource;

公共字符串方向; //水平或垂直(值为:H或V)

公共浮动深度;

公共浮动maxMudLvl;

public int agitatorQty;

public int impellerQty;

public float mudVolume;

public float turnoverRatio;

public float GPM;

}


公共类roundTank:tank

{

public roundTank()

$

}

公共浮动直径;

}


公共类squareTank:tank

{

public squareTank()

{

}

公共浮动长度;

公共浮动宽度;

}

}

解决方案

没有太多查看你的代码(那里很长),我的猜测

将是对象在页面上的一个请求中被实例化。

对页面的不同请求(即下一次点击或其他),你看起来是
它是在那里。


好​​吧,网络应用程序不会那样工作(与

特别是asp.net无关)。对页面的每个请求都将创建该页面的新实例

并运行其代码。然后该对象被销毁。它不会以某种方式保持

,并且知道在用户的下一个请求中匹配它。


所以你必须重新创建这个对象每次加载页面时你都希望它能在那里获得


" StillStuckOnJava" <圣************** @ discussions.microsoft.com>写在

消息新闻:07 ********************************** @ microsof t.com ...

我永远无法解决我的delimma问题,所以我试图在最后一次(希望不是徒劳)试图超越这个该死的问题! !我真的很感谢有人在这里帮助我的笨蛋。

好吧,我有一个asp.net程序,我开始写作。我创建了一个名为tank的类,带有一些子类(圆形和方形)。我想在运行时实例化这个坦克类型的对象,并在
用户选择某些东西时为其分配成员值。我一直收到有关我的实例的错误,指的是
一个空对象,因为在用户在运行时选择
之前,实例不存在。我尝试了几种方法,创建一个类文件并将它们直接放在agitatorSz命名空间下,但似乎仍然是某种范围问题。
这是我的类代码和我的应用程序代码:

使用System;
使用System.Collections;
使用System.ComponentModel;
使用System.Data ;
使用System.Drawing;
使用System.Web;
使用System.Web.SessionState;
使用System.Web.UI;
使用System.Web。 UI.WebControls;
使用System.Web.UI.HtmlControls;

命名空间agitatorSz
{
///< summary>
/// WebForm1的摘要描述。
///< / summary>
公共类main_frm:System.Web.UI.Page
公共System.Web.UI.WebControls。按钮tankType_btn;
protected System.Web.UI.WebControls.Button dimensions_btn;
protected System.Web.UI.WebControls.TextBox impellers_txt;
protected System.Web.UI.WebControls.TextBox agitators_t xt;
受保护的System.Web.UI.WebControls.Label volume_txt;
受保护的System.Web.UI.WebControls.Label turnover_txt;
protected System.Web.UI.WebControls.Label gpm_txt;
protected System.Web.UI.WebControls.DataGrid product_grd;
protected System.Web.UI.WebControls.TextBox length_txt;
protected System.Web.UI.WebControls.TextBox width_txt;
protected System.Web.UI.WebControls.TextBox diameter_txt;
protected System.Web.UI.WebControls.TextBox depth_txt;
protected System.Web.UI.WebControls.Button model_btn;
protected System.Web.UI.WebControls.DropDownList model_ddl;
protected System.Web.UI.WebControls.Button powerSrc_btn;
protected System.Web.UI.WebControls.DropDownList power_ddl;
protected System .Web.UI.WebControls.TextBox maxMudLvl_txt;
protected System.Web.UI.WebControls.DropDownList tankType_ddl;

private void Page_Load(object sender,System.EventArgs e)
{
//用户代码初始化t他在这里页面
}
#region Web表单设计器生成的代码
覆盖保护的void OnInit(EventArgs e)

//
// CODEGEN:ASP.NET Web窗体设计器需要此调用。
//
InitializeComponent();
base.OnInit(e);
}
///< summary>
/// Designer支持所需的方法 - 不要使用代码编辑器修改
///此方法的内容。
/ //< / summary>
private void InitializeComponent()
{
this.tankType_btn.Click + = new
System.EventHandler(this.tankType_btn_Click);
this.model_btn.Click + = new System.EventHandler(this.model_btn_Click);
this.Load + = new System.EventHandler(this.Page_Load);

}
# endregion

private void tankType_btn_Click(object sender,System.EventArgs e)
{tankTypeVar = tankType_ddl.SelectedItem.Value;

switch(tankType_ddl。 SelectedItem.Value)
{
案例" 1":
{
roundTank rt = new roundTank();
rt.orientation =" test1";
//启用与课程相关的控件,禁用其他人
diameter_txt.Enabled = true;
length_txt.Enabled = false;
width_txt.Enabled = false;
break;
}
case" 2":
{
squareTank st = new squareTank();
st.orientation =" test2";
//启用与类相关的控件
length_txt.Enabled = true;
width_txt.Enabled = true;
diameter_txt.Enabled = false;
break;
}
}

私有的void model_btn_Click(对象发送者,系统。 EventArgs e)
{
if(rt!= null)
{
depth_txt.Text = rt.orientation;
}
if(st!= null)
{
depth_txt.Text = st.orientation;
}
}
}
}

--- ---------
班级代码
------------

使用系统;

namespace agitatorSz
///< summary>
///坦克概要说明。
/// < / summary>
公共类坦克
公共坦克()
{
}公共浮动电源;
公共字符串方向; //水平或垂直(值为:H或V)
公共浮动深度;
public float maxMudLvl;
public int agitatorQty;
public int impellerQty;
public float mudVolume;
public float turnoverRatio;
public float GPM;


公共类roundTank:tank
{public roundTank()
{
}
公共浮动直径;

公共类squareTank:tank
{public squareTank()
{
}
公共浮动长度;
公共浮动宽度;
}
}


我可能会遗漏一些东西,但是你有没有想过推迟实际使用
实例化,直到用户做出选择?如果我理解正确,

你知道你需要一个Tank对象,但是在用户选择之前你不知道哪种类型的坦克

。所以在那时实例化;它应该消除

空对象错误。


或者,如果你有绝对的行为,那么肯定要在之前初步确定
Tank对象被实例化,你或许可以使用类似于策略模式的东西。在单独的TankHelper类中封装早期行为

,然后将Tank委托给指定行为的

TankHelper。就我个人而言,除非我真的不得不这样做,否则我不愿意这样做。我会更加舒服地推迟实际购买。


-

David Veeneman

远见系统

芝加哥美国


谢谢,这是很好的信息。显然,这个asp.net的东西对于我来说是新的。您是否有任何想法可以保持对象静态或保留

用户选择的值?


" Marina"写道:

没有太多查看你的代码(那里很长),我的猜测
将是对象在页面的一个请求上实例化。<并且对页面的不同请求(即下一次点击或其他),您正在寻找它在那里。

好吧,Web应用程序不会那样工作(特别是与asp.net无关)。对页面的每个请求都将创建该页面的新实例并运行其代码。然后该对象被销毁。它不会以某种方式保持不变,并且知道在用户的下一个请求中匹配它。

因此,每次页面加载并且您期望时,您必须重新创建此对象
它就在那里。

StillStuckOnJava <圣************** @ discussions.microsoft.com>在消息新闻中写道:07 ********************************** @ microsof t.com。 ..

我永远无法解决我的定界问题,所以我正在尝试最后的(希望不是徒劳)试图超越这个该死的问题!我真的很感谢有人在这里帮助我的笨蛋。

好吧,我有一个asp.net程序,我开始写作。我创建了一个名为tank的类,带有一些子类(圆形和方形)。我想在运行时实例化这个坦克类型的对象,并在
用户选择某些东西时为其分配成员值。我一直收到有关我的实例的错误,指的是
一个空对象,因为在用户在运行时选择
之前,实例不存在。我尝试了几种方法,创建一个类文件并将它们直接放在agitatorSz命名空间下,但似乎仍然是某种范围问题。
这是我的类代码和我的应用程序代码:

使用System;
使用System.Collections;
使用System.ComponentModel;
使用System.Data ;
使用System.Drawing;
使用System.Web;
使用System.Web.SessionState;
使用System.Web.UI;
使用System.Web。 UI.WebControls;
使用System.Web.UI.HtmlControls;

命名空间agitatorSz
{
///< summary>
/// WebForm1的摘要描述。
///< / summary>
公共类main_frm:System.Web.UI.Page
公共System.Web.UI.WebControls。按钮tankType_btn;
protected System.Web.UI.WebControls.Button dimensions_btn;
protected System.Web.UI.WebControls.TextBox impellers_txt;
protected System.Web.UI.WebControls.TextBox agitators_t xt;
受保护的System.Web.UI.WebControls.Label volume_txt;
受保护的System.Web.UI.WebControls.Label turnover_txt;
protected System.Web.UI.WebControls.Label gpm_txt;
protected System.Web.UI.WebControls.DataGrid product_grd;
protected System.Web.UI.WebControls.TextBox length_txt;
protected System.Web.UI.WebControls.TextBox width_txt;
protected System.Web.UI.WebControls.TextBox diameter_txt;
protected System.Web.UI.WebControls.TextBox depth_txt;
protected System.Web.UI.WebControls.Button model_btn;
protected System.Web.UI.WebControls.DropDownList model_ddl;
protected System.Web.UI.WebControls.Button powerSrc_btn;
protected System.Web.UI.WebControls.DropDownList power_ddl;
protected System .Web.UI.WebControls.TextBox maxMudLvl_txt;
protected System.Web.UI.WebControls.DropDownList tankType_ddl;

private void Page_Load(object sender,System.EventArgs e)
{
//用户代码初始化t他在这里页面
}
#region Web表单设计器生成的代码
覆盖保护的void OnInit(EventArgs e)

//
// CODEGEN:ASP.NET Web窗体设计器需要此调用。
//
InitializeComponent();
base.OnInit(e);
}
///< summary>
/// Designer支持所需的方法 - 不要使用代码编辑器修改
///此方法的内容。
/ //< / summary>
private void InitializeComponent()
{
this.tankType_btn.Click + = new
System.EventHandler(this.tankType_btn_Click);
this.model_btn.Click + = new System.EventHandler(this.model_btn_Click);
this.Load + = new System.EventHandler(this.Page_Load);

}
# endregion

private void tankType_btn_Click(object sender,System.EventArgs e)
{tankTypeVar = tankType_ddl.SelectedItem.Value;

switch(tankType_ddl。 SelectedItem.Value)
{
案例" 1":
{
roundTank rt = new roundTank();
rt.orientation =" test1";
//启用与课程相关的控件,禁用其他人
diameter_txt.Enabled = true;
length_txt.Enabled = false;
width_txt.Enabled = false;
break;
}
case" 2":
{
squareTank st = new squareTank();
st.orientation =" test2";
//启用与类相关的控件
length_txt.Enabled = true;
width_txt.Enabled = true;
diameter_txt.Enabled = false;
break;
}
}

私有的void model_btn_Click(对象发送者,系统。 EventArgs e)
{
if(rt!= null)
{
depth_txt.Text = rt.orientation;
}
if(st!= null)
{
depth_txt.Text = st.orientation;
}
}
}
}

--- ---------
班级代码
------------

使用系统;

namespace agitatorSz
///< summary>
///坦克概要说明。
/// < / summary>
公共类坦克
公共坦克()
{
}公共浮动电源;
公共字符串方向; //水平或垂直(值为:H或V)
公共浮动深度;
public float maxMudLvl;
public int agitatorQty;
public int impellerQty;
public float mudVolume;
public float turnoverRatio;
public float GPM;


公共类roundTank:tank
{public roundTank()
{
}
公共浮动直径;

公共类squareTank:tank
{public squareTank()
{
}
公共浮动长度;
公共浮动宽度;
}
}



I could never get a solution to my delimma, so I''m trying in a last
(hopefully not futile) attempt to get past this damned issue!!! I would
REALLY appreciate someone helping my dumb-ass out here.

Alright, I have an asp.net program I started writing. I created a class
called tank, with some subclasses (round and square). I want to instantiate
this tank-type object at runtime, and assign it member values when the user
selects something. I keep getting an error about my instances referring to a
null object because the instances don''t exist until the user makes a
selection at runtime. I tried this several ways, creating a class file and
just putting them directly under the agitatorSz namespace, but still seems to
be some kind of scope issue.

Here''s my class code and my application code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace agitatorSz
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class main_frm : System.Web.UI.Page
{
public System.Web.UI.WebControls.Button tankType_btn;
protected System.Web.UI.WebControls.Button dimensions_btn;
protected System.Web.UI.WebControls.TextBox impellers_txt;
protected System.Web.UI.WebControls.TextBox agitators_txt;
protected System.Web.UI.WebControls.Label volume_txt;
protected System.Web.UI.WebControls.Label turnover_txt;
protected System.Web.UI.WebControls.Label gpm_txt;
protected System.Web.UI.WebControls.DataGrid product_grd;
protected System.Web.UI.WebControls.TextBox length_txt;
protected System.Web.UI.WebControls.TextBox width_txt;
protected System.Web.UI.WebControls.TextBox diameter_txt;
protected System.Web.UI.WebControls.TextBox depth_txt;
protected System.Web.UI.WebControls.Button model_btn;
protected System.Web.UI.WebControls.DropDownList model_ddl;
protected System.Web.UI.WebControls.Button powerSrc_btn;
protected System.Web.UI.WebControls.DropDownList power_ddl;
protected System.Web.UI.WebControls.TextBox maxMudLvl_txt;
protected System.Web.UI.WebControls.DropDownList tankType_ddl;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tankType_btn.Click += new
System.EventHandler(this.tankType_btn_Click);
this.model_btn.Click += new System.EventHandler(this.model_btn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void tankType_btn_Click(object sender, System.EventArgs e)
{
tankTypeVar=tankType_ddl.SelectedItem.Value;

switch(tankType_ddl.SelectedItem.Value)
{
case "1":
{
roundTank rt = new roundTank();
rt.orientation="test1";
//enable controls relevant to class, disable others
diameter_txt.Enabled=true;
length_txt.Enabled=false;
width_txt.Enabled=false;
break;
}
case "2":
{
squareTank st = new squareTank();
st.orientation="test2";
//enable controls relevant to class
length_txt.Enabled=true;
width_txt.Enabled=true;
diameter_txt.Enabled=false;
break;
}
}
}

private void model_btn_Click(object sender, System.EventArgs e)
{
if(rt!=null)
{
depth_txt.Text=rt.orientation;
}
if(st!=null)
{
depth_txt.Text=st.orientation;
}
}
}
}

------------
class code
------------

using System;

namespace agitatorSz
{
/// <summary>
/// Summary description for tank.
/// </summary>
public class tank
{
public tank()
{
}
public float powerSource;
public string orientation; //horizontal or vertical (values are: H or V)
public float depth;
public float maxMudLvl;
public int agitatorQty;
public int impellerQty;
public float mudVolume;
public float turnoverRatio;
public float GPM;
}

public class roundTank:tank
{
public roundTank()
{
}
public float diameter;
}

public class squareTank:tank
{
public squareTank()
{
}
public float length;
public float width;
}
}

解决方案

Without looking through your code too much (very long piece there), my guess
would be that the object is getting instantiated on one request to the page.
And on a different request to the page (i.e. next click or something), you
are looking for it to be there.

Well, web applications dont'' work that way (nothing to do with asp.net in
particular). Each request to a page will create a new instance of that page
and run through its code. That object is then destroyed. It doesn''t stay
around somehow, and knows to match it up on the next request from the user.

So you have to recreate this object every time the page loads and you expect
for it to be there.
"StillStuckOnJava" <St**************@discussions.microsoft.com> wrote in
message news:07**********************************@microsof t.com...

I could never get a solution to my delimma, so I''m trying in a last
(hopefully not futile) attempt to get past this damned issue!!! I would
REALLY appreciate someone helping my dumb-ass out here.

Alright, I have an asp.net program I started writing. I created a class
called tank, with some subclasses (round and square). I want to
instantiate
this tank-type object at runtime, and assign it member values when the
user
selects something. I keep getting an error about my instances referring to
a
null object because the instances don''t exist until the user makes a
selection at runtime. I tried this several ways, creating a class file and
just putting them directly under the agitatorSz namespace, but still seems
to
be some kind of scope issue.

Here''s my class code and my application code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace agitatorSz
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class main_frm : System.Web.UI.Page
{
public System.Web.UI.WebControls.Button tankType_btn;
protected System.Web.UI.WebControls.Button dimensions_btn;
protected System.Web.UI.WebControls.TextBox impellers_txt;
protected System.Web.UI.WebControls.TextBox agitators_txt;
protected System.Web.UI.WebControls.Label volume_txt;
protected System.Web.UI.WebControls.Label turnover_txt;
protected System.Web.UI.WebControls.Label gpm_txt;
protected System.Web.UI.WebControls.DataGrid product_grd;
protected System.Web.UI.WebControls.TextBox length_txt;
protected System.Web.UI.WebControls.TextBox width_txt;
protected System.Web.UI.WebControls.TextBox diameter_txt;
protected System.Web.UI.WebControls.TextBox depth_txt;
protected System.Web.UI.WebControls.Button model_btn;
protected System.Web.UI.WebControls.DropDownList model_ddl;
protected System.Web.UI.WebControls.Button powerSrc_btn;
protected System.Web.UI.WebControls.DropDownList power_ddl;
protected System.Web.UI.WebControls.TextBox maxMudLvl_txt;
protected System.Web.UI.WebControls.DropDownList tankType_ddl;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tankType_btn.Click += new
System.EventHandler(this.tankType_btn_Click);
this.model_btn.Click += new System.EventHandler(this.model_btn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void tankType_btn_Click(object sender, System.EventArgs e)
{
tankTypeVar=tankType_ddl.SelectedItem.Value;

switch(tankType_ddl.SelectedItem.Value)
{
case "1":
{
roundTank rt = new roundTank();
rt.orientation="test1";
//enable controls relevant to class, disable others
diameter_txt.Enabled=true;
length_txt.Enabled=false;
width_txt.Enabled=false;
break;
}
case "2":
{
squareTank st = new squareTank();
st.orientation="test2";
//enable controls relevant to class
length_txt.Enabled=true;
width_txt.Enabled=true;
diameter_txt.Enabled=false;
break;
}
}
}

private void model_btn_Click(object sender, System.EventArgs e)
{
if(rt!=null)
{
depth_txt.Text=rt.orientation;
}
if(st!=null)
{
depth_txt.Text=st.orientation;
}
}
}
}

------------
class code
------------

using System;

namespace agitatorSz
{
/// <summary>
/// Summary description for tank.
/// </summary>
public class tank
{
public tank()
{
}
public float powerSource;
public string orientation; //horizontal or vertical (values are: H or V)
public float depth;
public float maxMudLvl;
public int agitatorQty;
public int impellerQty;
public float mudVolume;
public float turnoverRatio;
public float GPM;
}

public class roundTank:tank
{
public roundTank()
{
}
public float diameter;
}

public class squareTank:tank
{
public squareTank()
{
}
public float length;
public float width;
}
}



I may be missing something, but have you thought about deferring
instantiation until the user makes a selection? If I understand correctly,
you know you will need a Tank object, but you won''t know which type of Tank
until the user selects. So instantiate at that point; it should eliminate
the null object errors.

Alternatively, if there are behaviors you absolutely, positively have to
initialize before the Tank object gets instantiated, you might be able to
use something similar to a Strategy pattern. Encapsulate the early behaviors
in a separate TankHelper class, then have the Tank delegate to the
TankHelper for the specified behaviors. Personally, I''d be reluctant to do
this unless I really had to-- I''m far more comfortable deferring
instantiation.

--
David Veeneman
Foresight Systems
Chicago USA


Thank you, that''s good information. Obviously, this asp.net stuff is new to
me. Do you have any ideas as to how I can keep the object static or retain
its values as the user makes selections?

"Marina" wrote:

Without looking through your code too much (very long piece there), my guess
would be that the object is getting instantiated on one request to the page.
And on a different request to the page (i.e. next click or something), you
are looking for it to be there.

Well, web applications dont'' work that way (nothing to do with asp.net in
particular). Each request to a page will create a new instance of that page
and run through its code. That object is then destroyed. It doesn''t stay
around somehow, and knows to match it up on the next request from the user.

So you have to recreate this object every time the page loads and you expect
for it to be there.
"StillStuckOnJava" <St**************@discussions.microsoft.com> wrote in
message news:07**********************************@microsof t.com...

I could never get a solution to my delimma, so I''m trying in a last
(hopefully not futile) attempt to get past this damned issue!!! I would
REALLY appreciate someone helping my dumb-ass out here.

Alright, I have an asp.net program I started writing. I created a class
called tank, with some subclasses (round and square). I want to
instantiate
this tank-type object at runtime, and assign it member values when the
user
selects something. I keep getting an error about my instances referring to
a
null object because the instances don''t exist until the user makes a
selection at runtime. I tried this several ways, creating a class file and
just putting them directly under the agitatorSz namespace, but still seems
to
be some kind of scope issue.

Here''s my class code and my application code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace agitatorSz
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class main_frm : System.Web.UI.Page
{
public System.Web.UI.WebControls.Button tankType_btn;
protected System.Web.UI.WebControls.Button dimensions_btn;
protected System.Web.UI.WebControls.TextBox impellers_txt;
protected System.Web.UI.WebControls.TextBox agitators_txt;
protected System.Web.UI.WebControls.Label volume_txt;
protected System.Web.UI.WebControls.Label turnover_txt;
protected System.Web.UI.WebControls.Label gpm_txt;
protected System.Web.UI.WebControls.DataGrid product_grd;
protected System.Web.UI.WebControls.TextBox length_txt;
protected System.Web.UI.WebControls.TextBox width_txt;
protected System.Web.UI.WebControls.TextBox diameter_txt;
protected System.Web.UI.WebControls.TextBox depth_txt;
protected System.Web.UI.WebControls.Button model_btn;
protected System.Web.UI.WebControls.DropDownList model_ddl;
protected System.Web.UI.WebControls.Button powerSrc_btn;
protected System.Web.UI.WebControls.DropDownList power_ddl;
protected System.Web.UI.WebControls.TextBox maxMudLvl_txt;
protected System.Web.UI.WebControls.DropDownList tankType_ddl;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tankType_btn.Click += new
System.EventHandler(this.tankType_btn_Click);
this.model_btn.Click += new System.EventHandler(this.model_btn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void tankType_btn_Click(object sender, System.EventArgs e)
{
tankTypeVar=tankType_ddl.SelectedItem.Value;

switch(tankType_ddl.SelectedItem.Value)
{
case "1":
{
roundTank rt = new roundTank();
rt.orientation="test1";
//enable controls relevant to class, disable others
diameter_txt.Enabled=true;
length_txt.Enabled=false;
width_txt.Enabled=false;
break;
}
case "2":
{
squareTank st = new squareTank();
st.orientation="test2";
//enable controls relevant to class
length_txt.Enabled=true;
width_txt.Enabled=true;
diameter_txt.Enabled=false;
break;
}
}
}

private void model_btn_Click(object sender, System.EventArgs e)
{
if(rt!=null)
{
depth_txt.Text=rt.orientation;
}
if(st!=null)
{
depth_txt.Text=st.orientation;
}
}
}
}

------------
class code
------------

using System;

namespace agitatorSz
{
/// <summary>
/// Summary description for tank.
/// </summary>
public class tank
{
public tank()
{
}
public float powerSource;
public string orientation; //horizontal or vertical (values are: H or V)
public float depth;
public float maxMudLvl;
public int agitatorQty;
public int impellerQty;
public float mudVolume;
public float turnoverRatio;
public float GPM;
}

public class roundTank:tank
{
public roundTank()
{
}
public float diameter;
}

public class squareTank:tank
{
public squareTank()
{
}
public float length;
public float width;
}
}




这篇关于我知道我知道;但...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆