|
When is the LoadStaff() function called?
In the first code block you create the Database object then immediately try to read the StaffNames without calling the Load.
If you will always be calling LoadStaff when you create a Database object you should place a call to LoadStaff() in the constructor for the Database class.
If you won't be calling LoadStaff everytime then you should create a new StaffNames array.
public Database() {
LoadStaff();
} or
public Database() {
StaffNames = new string[0]
} HTH,
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
I have tried that way and it does work but my idea was that I call the LoadStaff() function when my application starts and then I do not need to access the database again until they try update something.
So as the SplashScreen is shown my application will load all the data needed into my arrays and then I can just read from them without accessing the database again.
something like this.
[code]
//SplashScreen_Load Code
Database LoadArrays = new Database();
InformationLabel.Text = "Loading Clinic Array.....";
LoadArrays.LoadClinics();
InformationLabel.Text = "Loading Staff Array.....";
LoadArrays.LoadStaff();
//So the 2 arrays are now populated
//The main menu now loads and they choose Edit User from the menu
//EditUser_Load Code
Database sn = new Database();
int uBound = sn.StaffNames.GetUpperBound(0);
[/code]
Now I haven't called the the load function again because I do not want to access the database again.
Am I going about this in all the wrong way?
Thanks,
PS:How do you change the bgColor where your code is displayed?
|
|
|
|
|
OK! Now I see where the problem is, you're confused about how properties work on a class.
I'm going to discuss a smaller class to keep the example simple.
public class MyClass {
public int data;
public MyClass() { }
} Simple right? The class has one public member, an integer called data.
Lets do some tests, and hopefully this will clear up your confusion.
public void Tests() {
MyClass foo = new MyClass();
foo.data = 1;
System.Console.WriteLine("foo = {0}", foo.data);
MyClass bar = new MyClass();
bar.data = 2;
System.Console.WriteLine("foo = {0}", foo.data);
System.Console.WriteLine("bar = {0}", bar.data);
} You'll notice that foo is still 1 while bar is 2. The reason is that the data is stored with each instance of the class (an instance is a creation of that type in memory). But what if you have a value that needs to change in ALL instances of your type? Enter the static field/property.
Simply make one change to the class definition public static int data .
Now re-run the tests; foo and bar will both be 2 now.
How does this apply to your circumstance? The arrays in your class are like data in the first example. Each array stays with that instance; so what happens when you create a new instance of your database class? A brand new instance of your arrays is created as well, this time without all the data.
You now have a couple different tactics you can use. The easiest solution is to put the static keyword in the declaration with your arrays. This makes your data 'stick' with the type. The other solution is to use the same instance over and over again; one way of doing this is through the Singleton pattern, but I'll let you learn about that when you feel ready.
Gavin_Mannion wrote:
PS:How do you change the bgColor where your code is displayed?
Secrets of the Jedi :-P Actually just do this with your code: <pre>code</pre>.
HTH,
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
Well I feel like I have tangled myself in a web I just ain't going to be getting out of
Okay I have done as you said and I do understand everything that you have written down (minus the singleton thing but I don't wanna know )
I have made my arrays static but now I cannot access them from my form.
I get the following error message
C:\Documents and Settings\gavin\My Documents\Visual Studio Projects\LTBRMaintenance\EditUser.cs(202): Static member
'LTBRMaintenance.Database.StaffNames' cannot be accessed with an instance reference; qualify it with a type name instead
And they are no longer in my intellisense dropdown.
Here is what I have
public class Database
{
public static string[] StaffNames;
And in my EditUser_Load I have this
Database sn = new Database();
int uBound = sn.StaffNames.GetUpperBound(0);
StaffNames is where the blue squiggly is squatting at the moment.
Thanks for the help,
|
|
|
|
|
Whoops! (famous last words)
I forgot to mention this, when you have a static field you access it not by variablename.field, you access it by classname.field. So the last line should have been Database.StaffNames.GetUpperBound(0); .
Completely my fault.
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
YAY YAY YAY YAY ...
It worked ....
Thanks for the help maybe now I can get back on schedule with this project...
Cheers,
Gavin
|
|
|
|
|
Gavin_Mannion wrote:
maybe now I can get back on schedule with this project...
Good luck
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
If im developing a webservice how do i put up security so only authorized users will get access to it ?
/Mathias
|
|
|
|
|
Hi guys, I hope there is a simple explanation, or a simple workaround for this...
I need to capture both the click and double click events of a control. Depending on the event, the eventhandlers will do two totally different things.
The problem is, it seems that if you subscribe to both events, you ALWAYS get a click event before you get a doubleclick event.
Here's a little experiment I did when I got curious. Place a label on a form and set it's text property to "". Now add both a click and doubleclick event handler to the FORM. In the click event handler do the following
label1.Text="SingleMouseClick"
and in the doubleclick event handler...
label1.Text+="DoubleMouseClick"
Now, run the app and DOUBLE CLICK the form.
I get the label spittig out SingleMouseClickDoubleMouseClick indicating that the click event code ran first.
How can I get around this? I really want the events to be separate.
James, I know you're listening...you must have a plan as usual
Regards
Senkwe
Just another wannabe code junky
|
|
|
|
|
Senkwe Chanda wrote:
James, I know you're listening...you must have a plan as usual
LOL, yep you got me
The reason for having both events fire is to make the UI appear responsive, since the double-click time can be changed in the control panel you could have to wait up to a second or more before a click event would fire since it would have to wait to see if the second click ever occurred before firing the click event. whew, that was long winded :-P
Now for the fix.
Have your single click event handler enable a windows timer; have the double-click even disable that timer and do whatever you want.
Inside of the tick event in the windows timer is where you would code the single-click stuff.
You can obtain the maximum number of milliseconds that can elapse between clicks by reading the SystemInformation.DoubleClickTime property.
To be even more thorough you need to also ensure that the second click occurs within a rectangle of size SystemInformation.DoubleClickSize from the first click.
I highly recommend that you first set your double-click time way up; so you can see if this is going to be detrimental to the usability of your application.
HTH,
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
Thanks again James, that should work beautifully.
Regards
Senkwe
Just another wannabe code junky
|
|
|
|
|
How to programmatically add a new control in Window Application in C# language?
|
|
|
|
|
MyControl c = new MyControl();
this.Controls.Add(c); Simple
I assume that this code is being executed in the forms class.
This is also how you add a control to a windows form normally.
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
Thank you! I can add a new control but i can't set the properties for the control. this is my code:
TextBox tb = new TextBox();
this.Controls.Add(tb);
this.tb.Location = new System.Drawing.Point(150, 100);
this.tb.Multiline = true;
this.tb.Name = "textBox2";
this.tb.ReadOnly = false;
this.tb.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.tb.Size = new System.Drawing.Size(168, 32);
this.tb.TabIndex = 1;
this.tb.Text = "textBox1";
this.tb.Visible = false;
It gives errors. How to set the properties on the control?
|
|
|
|
|
What errors are you getting?
From what I see that code is fine; though I don't think it matters can you try psetting the properties before you do the 'this.Controls.Add(tb)'?
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
Ok, i got it ! I am not suppose to put
'this.tb.Location(....)'
but this:
'tb.Location(....)'
ok... Thank you very much!!
thongkk
|
|
|
|
|
lol, I can't believe I missed that! At least you have it figured out now
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
I would guess
Container.Controls.Add(new Control());
without having tested it yet
*woops, didnt notice the earlier post agreeing.
|
|
|
|
|
Hi !
Did anyone know an article who describes "how to install and starts a windows service remotely." I would like to implement such a function in C#.
Thanks in Advance
:confused
|
|
|
|
|
I'm trying to figure out if there's something wrong with my system or if it's normal. I don't remember it being this slow before.
I made a very small console app that opens a text file, writes one line to it and then reads that line back and prints it to the screen. It takes 22 seconds to run. The system is a pIII 700 with 256 megs of ram running win2K. I'm pretty sure that with the beta framework's small console apps ran at a reasonable speed.
Is this normal or is there something wrong with my installation?
|
|
|
|
|
that certainly sounds like its way too long; even with the JITing. Just to be sure though run ngen on your exe, then time it again. ngen will pre-JIT so the only startup time will be loading the framework.
HTH,
James
Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
Uninstalled and reinstalled the framework and it's fixed. takes only a couple seconds to fire up the first time. Pretty much instant the second time.
|
|
|
|
|
I have a label and I want to change the font style to bold or italic programmically.Can anybody tell me the code?
Mazy
Don't Marry a Person You Can Live With...
Marry Someone You Can Not Live Without
|
|
|
|
|
Based on ASP.net work (it may not be the same)
Object.Font.Bold = true;
Object.Font.Italic = true;
Object.Font.Name = "verdana";
etc.
// Rock
|
|
|
|
|
I used this code but there is an error return for it:
E:\....(106): Property or indexer 'System.Drawing.Font.Bold' cannot be assigned to -- it is read only
Any suggestion?
Mazy
Don't Marry a Person You Can Live With...
Marry Someone You Can Not Live Without
|
|
|
|