30 Replies - 11595 Views - Last Post: 11 December 2010 - 02:51 PM
#1 Guest_Tyrone*
C# last next and previous button
Posted 06 December 2010 - 02:33 AM
Replies To: C# last next and previous button
#2
Re: C# last next and previous button
Posted 06 December 2010 - 02:49 AM
We can't just give you the code when you have shown no effort yourself
nameOfArray[nameOfArray.Length - 1];
This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 02:50 AM
#3 Guest_Tyrone*
Re: C# last next and previous button
Posted 06 December 2010 - 03:06 AM
the code i tried is this
private void btnLast_Click(object sender, EventArgs e)
{
int i = 0;
foreach (string myCurrentName in myNames)
{
if (myCurrentName == null)
{
i = i - 1;
myNames[i] = txtName.Text;
mySurnames[i] = txtSurname.Text;
myAddresses[i] = txtAddress.Text;
myTels[i] = txtTel.Text;
myTowns[i] = cbxTown.Text;
myIDs[i] = txtId.Text;
myBankAccs[i] = txtAcc.Text;
myNIs[i] = txtNi.Text;
myComments[i] = txtComments.Text;
break;
}
MOD EDIT: Moved code into post. Please do not attach code to your post; put IN CODE TAGS as shown below within the post itself.
Attached File(s)
-
code.txt (757bytes)
Number of downloads: 301
This post has been edited by JackOfAllTrades: 06 December 2010 - 08:25 AM
#4
Re: C# last next and previous button
Posted 06 December 2010 - 03:17 AM
Anyway, that's the event handler method to find the last element in the array. Why are you filling the array in that method? Surely that method only needs to find the last element in the array, like I did with the code in my first post?
This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 03:36 AM
#5
Re: C# last next and previous button
Posted 06 December 2010 - 03:18 AM
myNames[i] is myNames[-1] and that is a IndexOutOfBounds.
#6 Guest_Tyrone*
Re: C# last next and previous button
Posted 06 December 2010 - 03:27 AM
sorry but I'm only a beginner in c#
#7
Re: C# last next and previous button
Posted 06 December 2010 - 04:21 AM
Thanks.
This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 04:22 AM
#8
Re: C# last next and previous button
Posted 06 December 2010 - 04:30 AM
for (int i = 0; i < myArray.Lentgh; i++) {
... = myArray[i];
}
This post has been edited by mavarazo: 06 December 2010 - 04:31 AM
#9 Guest_Tyrone*
Re: C# last next and previous button
Posted 06 December 2010 - 04:31 AM
#10
Re: C# last next and previous button
Posted 06 December 2010 - 04:49 AM
Define an Employee class:
public class Employee{
public string FirstName{
get;
private set;
}
public string LastName{
get;
private set;
}
public string Address{
get;
private set;
}
public string PhoneNumber{
get;
private set;
}
public string Town{
get;
private set;
}
public string ID{
get;
private set;
}
public string AccountNumber{
get;
private set;
}
public string NISNumber{
get;
private set;
}
public string Comments{
get;
private set;
}
public Employee(string firstName, string lastName, string address, string phoneNumber, string town, string ID, string accountNumber, string nisNumber, string comments){
this.FirstName = firstName;
this.LastName = lastName;
this.Address = address;
this.PhoneNumber = phoneNumber;
this.Town = town;
this.ID = ID;
this.AccountNumber = accountNumber;
this.NISNumber = nisNumber;
this.Comments = comments;
}
}
You now have an Employee object that can be used to hold all individual employee detail.
I would use a generic list instead of an array to hold the details, as generic lists dynamically resize. Therefore, in you main class, you could do something like this:
List<Employee> employees = new List<Employee>();
You can then fill that list with al the Employees in the company. Although, if it is a big company, a database is recommended. However, if there are going to be only a few employees that are part of the program, a list will do fine (unless the details need to be stored after the app has closed!).
Then, you need to fill the list with employees. Here is an example of how to add one employee to the list:
employee.Add(new Employee("Joe","Bloggs","Somewhere","01567 283843","SomeTown","345","02938848828566","9284777593",""));
And there we are, one employee called Joe Bloggs added to the list, with all his details nicely encapsulated in the Employee object. I would also create an abstract Person class and have Employee inherit from that, but don't worry about that for the time being.
You now have a single list rather than 9 different arrays. You can now find the next, previous, last etc items in that single list!
You have to understand though that using lists and arrays alone will not store the details. Once the application is closed, all details will be lost. To actually store the details indefinately after they have been entered, you need to use serialization, or preferably a database back end (or even a flat file, at a push).
I know there may seem a lot there. Any questions then just ask
This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 11:35 AM
#11
Re: C# last next and previous button
Posted 06 December 2010 - 05:28 AM
#12 Guest_Tyrone*
Re: C# last next and previous button
Posted 06 December 2010 - 09:35 AM
private void btnLast_Click(object sender, EventArgs e)
{
for (int i = 0; i < myNames.Length; i++)
{
txtName.Text = myNames[i];
txtSurname.Text = mySurnames[i];
txtAddress.Text = myAddresses[i];
txtTel.Text = myTels[i];
cbxTown.Text = myTowns[i];
txtId.Text = myIDs[i];
txtAcc.Text = myBankAccs[i];
txtNi.Text = myNIs[i];
txtComments.Text = myComments[i];
lblMessage.Text = "";
break;
}
also i have a date time picker and i need to get the value of the day but i cant convert the value to int. can someone tell me how i need to get the value
#13
Re: C# last next and previous button
Posted 06 December 2010 - 10:42 AM
I'm sorry but you have completely lost me...
I suggested list because it looked like you were capturing input from the user at runtime from the code in the .txt file you posted. You can use arrays instead if that isn't the case, no problem
I honestly have know idea what you are trying to do in that btn event handler.
What record do you think that code will show?
Why are you using a loop to display one record?
Why are you breaking out of the loop after one loop using the break; keyword? Don't you think that kind of defeats the idea of a loop?
Why are you not using the button called btnLast to return the last record (i.e. like I showed you in my first post)?
Why are you using 9 different arrays? Surely you want an array of Employees each of which has a name, accountNumber etc, rather than having their details sprawled everywhere in 9 different, unrelated arrays? Unless the 9 different arrays is specific to the assignment...
Try creating the Employee class if it isn't because I really don't know where to go from here... Sorry. Once you have an Employee class, fill one, single array with Employee objects. Then you can access the different elements using the ideas I posted in my first post. I explained the basic idea of navigating through the array n my first post too! Once you have one single array with all the details in, it will be ridiculously easy to do the next, previous etc things you want.
Also, what exactly will you be using the DateTime picker for? That sort of suggests to me that you want to give the user the ability to change and/or add records?
This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 11:13 AM
#14 Guest_Tyrone*
Re: C# last next and previous button
Posted 06 December 2010 - 11:14 AM
myNames[myNames.Length - 1];
I made a mistake with the break; keyword
I'm using 9 different arrays because my instructor said i need to use arrays to do it.
The date time picker will be so the user can select the dates the employee has worked then find the total week days, Saturdays and Sundays and multiply the payment per hour by the total number of hours worked in those days.
#15
Re: C# last next and previous button
Posted 06 December 2010 - 11:21 AM
You need to actually do something with the value returned using the line showed in my first post. That code will return the last element in an array. However, you need to do something with that value. Like store it in a variable, display it in a text box etc. Your getting the errror because you aren't doing anything with the element you are accessing. The value of the last element is just floating aimlessly.
Just because your instructor said use arrays, I don't think it means you should use as many arrays as is physically possible. Please, please, please use only one array. If only for my sanity.
Also, please create the Employee class I showed a few posts ago. Then post the class you create. I will then talk you through filling an array with Employees and accessing next, previous; last elements.
This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 11:30 AM
|
|

New Topic/Question
Reply
MultiQuote








|