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

C# problem

Options
  • 15-04-2004 4:13pm
    #1
    Registered Users Posts: 2,191 ✭✭✭


    Ok this is probably something small but its going to cost me my end of year project. What Im trying to do is fill a bunch of labels with text the code im using is

    lblMonday = new label() // without this line my project crashes & tells my "Object not set to an instance of an object

    lblMonday.Text = myStaff[1].Firstname;


    where I set a breakpoint & place my mouse over the .text part it tells me that the text is in fact there but its still won't appear on the screen. the first 12 labels were dragged onto the screen & the rest were copied & pasted from them (yes I changed all the names). Ive tried invalidating & refreshing both the tabpage the labels are on as well as the form itself but it doesnt work either.


Comments

  • Registered Users Posts: 6,762 ✭✭✭WizZard


    Is this using windows forms or web forms?


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    windows forms
    ill attach a screenshot


  • Registered Users Posts: 6,762 ✭✭✭WizZard


    Is the visibility of the labels set to treu, and are they enabled. A screenshot of the design time view would be more helpful as i have no idea which of the fields on the form are labels.
    Also, have you considered a datagrid to show the data?


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    yes the visability is set to true & they are enabled. Those white boxes are the labels.

    I thought i needed a dataset/table for a datagrid, can I just add variables to a datagrid???


  • Registered Users Posts: 6,762 ✭✭✭WizZard


    You could create the dataset in code. Its not that difficult.

    Anyway, try the label properties - text colour etc. You shouldn't need to declare the label as new before using it, that is if its defined at the top of your code.

    <brainwave>
    That sometimes happens in VS, if you use the copy paste method to put controls on a form it doesn't insert the declarations properly.
    Check it out
    </brainwave>


  • Advertisement
  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    i tried creating a data set and adding in my variables but the data grid wouldn't accept the table. i'll try to manually put them back in.

    As i said the new label() line stops the error "object not set to an instance of an object"


  • Registered Users Posts: 6,762 ✭✭✭WizZard


    Yeah, but if the label was created properly at design time you shouldn't need that statement


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    well i just redid all the labels this time without cutting & pasting, still won't show the text and still gives an error if i leave out the line
    lblMon = new label();


  • Registered Users Posts: 6,762 ✭✭✭WizZard


    No idea so. If you wanna send me the code feel free to PM for address. I promise nothing though


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    How about you post all the code? It would give us a much better idea of what your doing.


  • Advertisement
  • Registered Users Posts: 6,762 ✭✭✭WizZard


    Yes, post the code - but don't forget to enclose it in [ code ] tags :D


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    ok this is thecode for the method
    
    public void setMonday(int Morn1, int Morn2, int Morn3, int Morn4, int Aft1, int Aft2, int Aft3, int Aft4, int Eve1, int Eve2, int Eve3, int Eve4, DateTime date)
    		{
    			//display the names of these people 
    			
    			//start with mornings
    			lblMonMorn1 = new Label();
    			lblMonMorn1.Enabled = true;
    			lblMonMorn1.Text = CreateRota.StaffList[Morn1].first +" " + CreateRota.StaffList[Morn1].last;
    			lblMonMorn2 = new Label();
    			lblMonMorn2.Text = CreateRota.StaffList[Morn2].first +" " + CreateRota.StaffList[Morn2].last;
    			lblMonMorn3 = new Label();
    			lblMonMorn3.Text = CreateRota.StaffList[Morn3].first +" " + CreateRota.StaffList[Morn3].last;
    			lblMonMorn4 = new Label();
    			lblMonMorn4.Text = CreateRota.StaffList[Morn4].first +" " + CreateRota.StaffList[Morn4].last;
    			//next the afternoon shift
    			lblMonAft1 = new Label();
    			lblMonAft1.Text = CreateRota.StaffList[Aft1].first +" " + CreateRota.StaffList[Aft1].last;
    			lblMonAft2 = new Label();
    			lblMonAft2.Text = CreateRota.StaffList[Aft2].first +" " + CreateRota.StaffList[Aft2].last;
    			lblMonAft3 = new Label();
    			lblMonAft3.Text = CreateRota.StaffList[Aft3].first +" " + CreateRota.StaffList[Aft3].last;
    			lblMonAft4 = new Label();
    			lblMonAft4.Text = CreateRota.StaffList[Aft4].first +" " + CreateRota.StaffList[Aft4].last;
    			//finally the evening shift
    			lblMonEve1 = new Label();
    			lblMonEve1.Text = CreateRota.StaffList[Eve1].first +" " + CreateRota.StaffList[Eve1].last;
    			lblMonEve2 = new Label();
    			lblMonEve2.Text = CreateRota.StaffList[Eve2].first +" " + CreateRota.StaffList[Eve2].last;
    			lblMonEve3 = new Label();
    			lblMonEve3.Text = CreateRota.StaffList[Eve3].first +" " + CreateRota.StaffList[Eve3].last;
    			lblMonEve4 = new Label();
    			lblMonEve4.Text = CreateRota.StaffList[Eve4].first +" " + CreateRota.StaffList[Eve4].last;
    
    			//put the day & date into the top of the column
    			lblMon = new Label();
    			lblMon.Text = "Monday " + date;
    
    		}
    


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


    sorry if I'm repeating anything but is lblMon actually on the form anywhere ?
    its not defined in the setMonday method.

    how did you create the shift display ? are they labels or columns on a datagird ?
    can you attach the entire project ?

    I notice in your first post the label is lblMonday
    while in the code its lblMon


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    lblMon actually on the form anywhere
    yes all labels were dragged & dropped on
    how did you create the shift display ? are they labels or columns on a datagird ?
    they are all labels that I dragged & dropped on
    can you attach the entire project
    this is my end of year project, its simply too big to attach/post/e-mail, even if i zip it.

    ok what ive tried to do instead is create variables in the GUI class, then in the setMonday method instead of :
    lblMonMorn2 = new Label();
    			lblMonMorn2.Text = CreateRota.StaffList[Morn2].first +" " + CreateRota.StaffList[Morn2].last;
    

    I have :
    //note MonMorn is already declared as a string
    MonMorn2 = CreateRota.StaffList[Morn2].first +" " + CreateRota.StaffList[Morn2].last;
    

    when i set a breakpoint at the above code it actually says that it has a value, but it seems to loose that value when the method ends, so when I call that variable later it ends up being null


  • Registered Users Posts: 1,990 ✭✭✭lynchie


    Where is lblMonMorn1 declared in the class ? If you are declaring it in the setMonday method then it would be local, thus loosing its value once the method call ends.


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    Its ok i found the problem, It was simple really, it turns out my controller had created its own instance of the GUI, this is why it needed the new instances of label & it was why the string variables lost their values when the method ended.

    It came to me when i was drinking coffee & discussing UML diagrams. Thanks for the input guys


Advertisement