Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# windows program help needed!

  • 17-11-2007 9:47pm
    #1
    Registered Users, Registered Users 2 Posts: 6,535 ✭✭✭


    Ive just started windows programming this year ( hate it as its impossible) anyways what I have to do is have a form with two text boxes one with first names and one with surnames using arrays.

    It will show up a list of names hard coded so it will happen just when it loads. Ive to use arrays to load the names in each text box.

    string [] names = {"john", "mick"} blah blah. I cant get the names to display in the text boxes once I run it. All i get is system.String[]. I know that i just have to double click the form in design to have the load event happen.

    How do I get it to work? I can do it with listboxes ok but cant get them to load in the text boxes?

    Not sure if ive explained it well but any help would be great thanks.


Comments

  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    Can you post up the code you have done so far? It would help us see where you are going wrong.


  • Registered Users, Registered Users 2 Posts: 6,535 ✭✭✭joe123


    namespace RandomNames
    {
    public partial class frmRandomNames : Form
    {
    public frmRandomNames()
    {
    InitializeComponent();
    }

    private void frmRandomNames_Load(object sender, EventArgs e)
    {
    string[] names ={ "John", "Mary", "Jack", "Frank", "Mel", "Horace", "Henry", "Declan" };
    txtFirstName.Text =names.ToString();

    string [] surName = { "Malone", "casey", "Humbolt", "Davey", "Seinnheiser", "Bradley", "Kehoe", "Lomax", "Reilly", "Nielsen" };
    txtSurName.Text = surName.ToString();

    }

    }
    }


    I cant seem to get the array to display the list of names. What am i doing wrong? thanks for the help and im pretty bad at programming so go easy:o


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    What you need to do is iterate through the array with a loop and then display the current element.


  • Registered Users, Registered Users 2 Posts: 6,535 ✭✭✭joe123


    frsName[0] = "john";
    frsName[1] = "mary";
    frsName[2] = "jack";
    frsName[3] = "frank";
    frsName[4] = "mel";
    frsName[5] = "horace";
    frsName[6] = "henry";
    frsName[7] = "declan";

    for (int i = 0; i < 8; i++)

    should it look more like that?


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,125 Mod ✭✭✭✭AlmightyCushion


    joe123 wrote: »
    frsName[0] = "john";
    frsName[1] = "mary";
    frsName[2] = "jack";
    frsName[3] = "frank";
    frsName[4] = "mel";
    frsName[5] = "horace";
    frsName[6] = "henry";
    frsName[7] = "declan";

    You already have the elements in the array declared so there is no need to do this.
    joe123 wrote: »
    for (int i = 0; i < 8; i++)

    should it look more like that?

    That will run 8 times. What happens if you take away or add an element to the array?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 6,535 ✭✭✭joe123


    That will run 8 times. What happens if you take away or add an element to the array?


    Does that matter? Im given the 8 names. They wont change. I click the load names button and these 8 names are supposed to appear in the First name text box.

    Problem is I dont know how im supposed to go through the loop and display the names.

    index of or .items or something?


  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    joe123 wrote: »
    Does that matter? Im given the 8 names. They wont change. I click the load names button and these 8 names are supposed to appear in the First name text box.

    Problem is I dont know how im supposed to go through the loop and display the names.

    index of or .items or something?
    it's not that it matters that much, it's just a bad habit to get into.

    when working with arrays it's always good to use a constant value when accessing the values of the array through a loop.

    eg.

    constant ARRAY_SIZE = 8

    for i in ARRAY_SIZE
    do
    yadda = yadda
    done

    this way if you add/remove elements from the array you only have to change the value stored in your constant

    have a google for using arrays in c# the first hit i got displayed how to properly loop through an array without hard coding values in.


  • Registered Users, Registered Users 2 Posts: 15,065 ✭✭✭✭Malice


    Cremo wrote: »
    it's not that it matters that much, it's just a bad habit to get into.

    when working with arrays it's always good to use a constant value when accessing the values of the array through a loop.

    eg.

    constant ARRAY_SIZE = 8

    for i in ARRAY_SIZE
    do
    yadda = yadda
    done

    this way if you add/remove elements from the array you only have to change the value stored in your constant
    You're absolutely right about it being a bad idea to hardcode array boundaries but I always get the array boundary by doing something like this:

    [PHP]
    // Define the array
    string [] inputArray = { "John", "Mary", "Jack", "Frank", "Mel", "Horace", "Henry", "Declan" };
    int uBound = inputArray.GetUpperBound(0);
    [/PHP]That way you never need to worry about having to change a constant as your code automatically calculates the boundary for you.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    The usual way to do it is:
    string[] names = new string[] { James, Jim, Joe, John, Jan };
    
    for(int i=0; i < names.Length; i++)
         DoThingToAddToTextBox();    
    

    The 'DoThingToAddToTextBox' could be to call textbox.Append(string) or possibly just textbox.Text = textbox.Text + currentname, or a fancier way using a string builder to generate the required string first.

    The reason why "txtFirstName.Text =names.ToString();" didn't work as you expected is that .ToString() is not overridden on an array type to do anything useful, so it just defaults to printing out the type name, in your case "System.string[]" is the type of the object.


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    you would be better doing
    [PHP]
    int numArrayelements = names.Length;
    no need to array each time and its neater!

    [/PHP]


  • Advertisement
Advertisement