G
Guest
Hello,
I have a custom component class that I created that will basically save x
amount of "objects." Once x + 1 object pops in, the oldest one falls off.
It works great as I have it. In my test web page I create a Static object
and use the New to create a new instance of it. If I don't use the Static,
nothing is remembered. The only problem is, when I start a second page, I
expect it to have a NEW instance of the class but the numbers stored show up
the same.
How do I get it to always create a New instance. Am I missing the public,
private, protected, internal - modifiers in the wrong position?
Please help.
- - - - - - - Class File:
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
namespace NASQLStatements {
public class ThingsRemembered : System.ComponentModel.Component {
private System.ComponentModel.Container components = null;
private int iThingsToRemember;
protected internal ArrayList aThings;
public ThingsRemembered(System.ComponentModel.IContainer container) {
container.Add(this);
InitializeComponent();
aThings = new ArrayList(2);
}
public ThingsRemembered() {
InitializeComponent();
aThings = new ArrayList(2);
}
public ThingsRemembered(int iCapacity) {
InitializeComponent();
aThings = new ArrayList(iCapacity);
}
public ThingsRemembered(System.ComponentModel.IContainer container, int
iCapacity) {
container.Add(this);
InitializeComponent();
aThings = new ArrayList(iCapacity);
}
protected override void Dispose( bool disposing ) {
if( disposing ) {
if(components != null) {
components.Dispose();
}
}
base.Dispose( disposing );
iThingsToRemember = 0;
aThings.Clear(); // delete all items
aThings.TrimToSize(); // resize the array to system default
}
#region Component Designer generated code
private void InitializeComponent() {
components = new System.ComponentModel.Container();
}
#endregion
public int ThingsToRemember {
get { return iThingsToRemember; }
set {
iThingsToRemember = value;
aThings.Capacity = iThingsToRemember; // size the array
} // set
} // ThingsToRemember - Property - Read/Write
public bool AddAThing(object oThing) {
try {
if(aThings.Count == aThings.Capacity) {
aThings.RemoveAt(0); // remove the oldest one
} // if there are already the maximum number of items in the array
aThings.Add(oThing); // add the new one
return true;
} catch(Exception e) {
sLastError = e.ToString();
return false;
} // try-catch errors
} // AddAThing - Method
public string LastError {
get { return sLastError; }
} // LastError - Property - Read Only
public object LastThing() {
return aThings[aThings.Count - 1];
} // LastThing - Method
public object FirstThing() {
return aThings[0];
} // FirstThing - Method
public ArrayList AllThings {
get { return aThings; }
} // AllThings - Property - Read Only
}
}
- - - - - - - Test File:
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;
using NASQLStatements;
namespace TestThings {
public class WebForm1 : System.Web.UI.Page {
protected System.Web.UI.WebControls.Button cmndAddThing;
protected System.Web.UI.WebControls.Button cmndShowThings;
protected System.Web.UI.WebControls.Label lablThings;
private static NASQLStatements.ThingsRemembered oStuff = new
NASQLStatements.ThingsRemembered();
private void Page_Load(object sender, System.EventArgs e) {
if(!Page.IsPostBack) {
oStuff.ThingsToRemember = 5;
lablThings.Text = oStuff.AllThings.Capacity.ToString() +
"<br>" + oStuff.AllThings.Count.ToString();
} // if this is the first time to the page
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e) {
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent() {
this.cmndAddThing.Click += new
System.EventHandler(this.cmndAddThing_Click);
this.cmndShowThings.Click += new
System.EventHandler(this.cmndShowThings_Click);
this.cmndError.Click += new System.EventHandler(this.cmndError_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void cmndShowThings_Click(object sender, System.EventArgs e) {
lablThings.Text = "" + oStuff.AllThings.Capacity.ToString() + "<br>";
for(int iPos = oStuff.AllThings.Count - 1; iPos >= 0; iPos--) {
lablThings.Text += oStuff.AllThings[iPos].ToString() + "<hr>";
} // for each item
}
private void cmndAddThing_Click(object sender, System.EventArgs e) {
Random rRandom = new Random(DateTime.Now.Millisecond);
int iValue = rRandom.Next(1, 1000);
lablThings.Text += "<br>" + oStuff.AddAThing(iValue).ToString();
}
}
}
As you can see, in the Test file I declare the Class as private static and
do a NEW. I assumed the New is necessary so it can create a New instance of
it. If it take out the Static modifier, nothing works and no random numbers
are stored. If I leave it in, it will remember the number (5) I have told
it. But so will each new web page I bring up and point to that page.
Please help.
I have a custom component class that I created that will basically save x
amount of "objects." Once x + 1 object pops in, the oldest one falls off.
It works great as I have it. In my test web page I create a Static object
and use the New to create a new instance of it. If I don't use the Static,
nothing is remembered. The only problem is, when I start a second page, I
expect it to have a NEW instance of the class but the numbers stored show up
the same.
How do I get it to always create a New instance. Am I missing the public,
private, protected, internal - modifiers in the wrong position?
Please help.
- - - - - - - Class File:
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
namespace NASQLStatements {
public class ThingsRemembered : System.ComponentModel.Component {
private System.ComponentModel.Container components = null;
private int iThingsToRemember;
protected internal ArrayList aThings;
public ThingsRemembered(System.ComponentModel.IContainer container) {
container.Add(this);
InitializeComponent();
aThings = new ArrayList(2);
}
public ThingsRemembered() {
InitializeComponent();
aThings = new ArrayList(2);
}
public ThingsRemembered(int iCapacity) {
InitializeComponent();
aThings = new ArrayList(iCapacity);
}
public ThingsRemembered(System.ComponentModel.IContainer container, int
iCapacity) {
container.Add(this);
InitializeComponent();
aThings = new ArrayList(iCapacity);
}
protected override void Dispose( bool disposing ) {
if( disposing ) {
if(components != null) {
components.Dispose();
}
}
base.Dispose( disposing );
iThingsToRemember = 0;
aThings.Clear(); // delete all items
aThings.TrimToSize(); // resize the array to system default
}
#region Component Designer generated code
private void InitializeComponent() {
components = new System.ComponentModel.Container();
}
#endregion
public int ThingsToRemember {
get { return iThingsToRemember; }
set {
iThingsToRemember = value;
aThings.Capacity = iThingsToRemember; // size the array
} // set
} // ThingsToRemember - Property - Read/Write
public bool AddAThing(object oThing) {
try {
if(aThings.Count == aThings.Capacity) {
aThings.RemoveAt(0); // remove the oldest one
} // if there are already the maximum number of items in the array
aThings.Add(oThing); // add the new one
return true;
} catch(Exception e) {
sLastError = e.ToString();
return false;
} // try-catch errors
} // AddAThing - Method
public string LastError {
get { return sLastError; }
} // LastError - Property - Read Only
public object LastThing() {
return aThings[aThings.Count - 1];
} // LastThing - Method
public object FirstThing() {
return aThings[0];
} // FirstThing - Method
public ArrayList AllThings {
get { return aThings; }
} // AllThings - Property - Read Only
}
}
- - - - - - - Test File:
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;
using NASQLStatements;
namespace TestThings {
public class WebForm1 : System.Web.UI.Page {
protected System.Web.UI.WebControls.Button cmndAddThing;
protected System.Web.UI.WebControls.Button cmndShowThings;
protected System.Web.UI.WebControls.Label lablThings;
private static NASQLStatements.ThingsRemembered oStuff = new
NASQLStatements.ThingsRemembered();
private void Page_Load(object sender, System.EventArgs e) {
if(!Page.IsPostBack) {
oStuff.ThingsToRemember = 5;
lablThings.Text = oStuff.AllThings.Capacity.ToString() +
"<br>" + oStuff.AllThings.Count.ToString();
} // if this is the first time to the page
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e) {
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent() {
this.cmndAddThing.Click += new
System.EventHandler(this.cmndAddThing_Click);
this.cmndShowThings.Click += new
System.EventHandler(this.cmndShowThings_Click);
this.cmndError.Click += new System.EventHandler(this.cmndError_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void cmndShowThings_Click(object sender, System.EventArgs e) {
lablThings.Text = "" + oStuff.AllThings.Capacity.ToString() + "<br>";
for(int iPos = oStuff.AllThings.Count - 1; iPos >= 0; iPos--) {
lablThings.Text += oStuff.AllThings[iPos].ToString() + "<hr>";
} // for each item
}
private void cmndAddThing_Click(object sender, System.EventArgs e) {
Random rRandom = new Random(DateTime.Now.Millisecond);
int iValue = rRandom.Next(1, 1000);
lablThings.Text += "<br>" + oStuff.AddAThing(iValue).ToString();
}
}
}
As you can see, in the Test file I declare the Class as private static and
do a NEW. I assumed the New is necessary so it can create a New instance of
it. If it take out the Static modifier, nothing works and no random numbers
are stored. If I leave it in, it will remember the number (5) I have told
it. But so will each new web page I bring up and point to that page.
Please help.