473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Help! Stupid C# Problem

I'm having a stupid problem with V.S./C#. I've created a class and two
subclasses, and I want to instantiate either of hte subclasses after users
make a selection. Then I want to use that class to modify its members as
users make more selections. IT'S NOT WORKING DAMN IT!

The following is my asp.net behind code for the web form, and then my class
code. I'm getting an error when I try to access the class instance that I
previously created. (yes, i've tried this different ways within my
application namespace)

---------------------------
asp.net web form 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

//************************************************** ************//
//Global (namespace-wide) declarations
//************************************************** ************//

public static string tankTypeVar;

private void tankType_btn_Click(object sender, System.EventArgs e)
{
tankTypeVar=tankType_ddl.SelectedItem.Value;
switch(tankType_ddl.SelectedItem.Value)
{
case "1":
{
rt = new roundTank();
//enable controls relevant to class, disable others
diameter_txt.Enabled=true;
length_txt.Enabled=false;
width_txt.Enabled=false;
break;
}
case "2":
{
st = new squareTank();
//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(tankTypeVar=="1")
{
rt.orientation=model_ddl.SelectedItem.Value;
depth_txt.Text=rt.orientation;
}
if(tankTypeVar=="2")
{
st.orientation=model_ddl.SelectedItem.Value;
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
{
public roundTank()
{
}
public float diameter;
}

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

Nov 17 '05 #1
5 1246
I didn't see where "rt" or "st" variables are originally declared.
I see where you instantiate them based on conditions though.

That said, my guess is that your project is far better suited
for the Parameterized Factory Pattern. I think you'll find your code a lot
easier to support especially when new types of tank shapes
are added. No more, "if this" or "if that" crap all over your app.

http://www.eggheadcafe.com/articles/20031103.asp

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"StillStuckOnJava" <St**************@discussions.microsoft.com> wrote in
message news:79**********************************@microsof t.com...
I'm having a stupid problem with V.S./C#. I've created a class and two
subclasses, and I want to instantiate either of hte subclasses after users
make a selection. Then I want to use that class to modify its members as
users make more selections. IT'S NOT WORKING DAMN IT!

The following is my asp.net behind code for the web form, and then my
class
code. I'm getting an error when I try to access the class instance that I
previously created. (yes, i've tried this different ways within my
application namespace)

---------------------------
asp.net web form 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

//************************************************** ************//
//Global (namespace-wide) declarations
//************************************************** ************//

public static string tankTypeVar;

private void tankType_btn_Click(object sender, System.EventArgs e)
{
tankTypeVar=tankType_ddl.SelectedItem.Value;
switch(tankType_ddl.SelectedItem.Value)
{
case "1":
{
rt = new roundTank();
//enable controls relevant to class, disable others
diameter_txt.Enabled=true;
length_txt.Enabled=false;
width_txt.Enabled=false;
break;
}
case "2":
{
st = new squareTank();
//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(tankTypeVar=="1")
{
rt.orientation=model_ddl.SelectedItem.Value;
depth_txt.Text=rt.orientation;
}
if(tankTypeVar=="2")
{
st.orientation=model_ddl.SelectedItem.Value;
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
{
public roundTank()
{
}
public float diameter;
}

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

Nov 17 '05 #2
Oops. Here it is:
(I also tried declaring rt and st in the namespace before i instantiated it
in the event, but that didn't work)

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

//************************************************** ************//
//Global (namespace-wide) declarations
//************************************************** ************//

public static string tankTypeVar;

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();
//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();
//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(tankTypeVar=="1")
{
rt.orientation=model_ddl.SelectedItem.Value;
depth_txt.Text=rt.orientation;
}
if(tankTypeVar=="2")
{
st.orientation=model_ddl.SelectedItem.Value;
depth_txt.Text=st.orientation;
}
}
}
}
Nov 17 '05 #3
----------------------
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

//************************************************** ************//
//Global (namespace-wide) declarations
//************************************************** ************//

public static string tankTypeVar;

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();
//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();
//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(tankTypeVar=="1")
{
rt.orientation=model_ddl.SelectedItem.Value;
depth_txt.Text=rt.orientation;
}
if(tankTypeVar=="2")
{
st.orientation=model_ddl.SelectedItem.Value;
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;
}
}
Nov 17 '05 #4
You don't really tell what your problem is. What errors do you get (copy/paste the exact error message is helpful)?

In your tankType_btn_Click you create one of the tanks but you don't store the references anywhere so they will be destroyed when the method returns.

You could also store the information about width/length/diameter in your parent and set this information when you create the child

public class Tank
{
public bool HasLength = false;
public bool HasWidth = false;
public bool HasDiameter = false;
}

public class RoundTank : Tank
{
public RoundTank()
{
HasDiameter = true;
}
}

public class SquareTank : Tank
{
public SquareTank()
{
HasLength = true;
HasWidth = true;
}
}

In your tankType_btn_Click you could then do (no {} blocks needed in case)

Tank t = null;
switch(tankType_ddl.SelectedItem.Value)
{
case "1":
t = new RoundTank();
break;
case "2":
t = new SquareTank();
break;
}

if(t != null)
{
diameter_txt.Enabled = t.HasDiameter;
length_txt.Enabled = t.HasLength;
width_txt.Enabled = t.HasWidth;
}

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #5
"Store the reference;" Damn! That's the problem. Thanks a lot man. What the
hell was I thinking? I appreciate your help.
You don't really tell what your problem is. What errors do you get (copy/paste the exact error message is helpful)?

In your tankType_btn_Click you create one of the tanks but you don't store the references anywhere so they will be destroyed when the method returns.

You could also store the information about width/length/diameter in your parent and set this information when you create the child

public class Tank
{
public bool HasLength = false;
public bool HasWidth = false;
public bool HasDiameter = false;
}

public class RoundTank : Tank
{
public RoundTank()
{
HasDiameter = true;
}
}

public class SquareTank : Tank
{
public SquareTank()
{
HasLength = true;
HasWidth = true;
}
}

In your tankType_btn_Click you could then do (no {} blocks needed in case)

Tank t = null;
switch(tankType_ddl.SelectedItem.Value)
{
case "1":
t = new RoundTank();
break;
case "2":
t = new SquareTank();
break;
}

if(t != null)
{
diameter_txt.Enabled = t.HasDiameter;
length_txt.Enabled = t.HasLength;
width_txt.Enabled = t.HasWidth;
}

--
Happy coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
2
by: Sudheer Kareem | last post by:
Dear All Please tell me how to assosiate help files with my Vb.net Project. Regards Sudheer
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.