Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Thank you first for taking the time to read, I know I'm N00B but a N00B lost in code. This is a school assignment and I can turn in the console app, because it works, but I would like to make this look pretty in a WinForm app.

Core problem I need to use LINQ to query and display a collection of objects based on the filter, this I can do in a Console Appliction.

When I tried to make it look pretty in a Winform the array is not accessable from the buton click event. Pretty sure I am just creating the objects in the wrong way. I suck with WinForm apps.

:: My namespaces all match.
:: This code will correctly run the LINQ query in a consol so I know that the var sortDescription.. is probably good.

First there is this code which creates my array of objects:

C#
{
  public partial class DisplayForm : Form
    {
        public DisplayForm()
        {
            InitializeComponent();
            QueryClass QClasses = new QueryClass();
            Invoice[] invoiceItems = new Invoice[8];
            invoiceItems[0] = new Invoice(83, "Electric sander", 7, 57.98M);
            invoiceItems[1] = new Invoice(24, "Power Saw", 18, 99.99M);
            invoiceItems[2] = new Invoice(7, "Sledge Hammer", 11, 21.50M);
            invoiceItems[3] = new Invoice(77, "Hammer", 76, 11.99M);
            invoiceItems[4] = new Invoice(39, "Lawn Mower", 3, 79.50M);
            invoiceItems[5] = new Invoice(68, "Screwdriver", 106, 6.99M);
            invoiceItems[6] = new Invoice(56, "Jig saw", 21, 11.00M);
            invoiceItems[7] = new Invoice(3, "Wrench", 34, 7.50M);

        }

//This is my button click code
private void queryA_Click(object sender, EventArgs e)
{
   var sortDescription =
    from element in invoiceItems
    orderby element.PartDescription
    select element;

   foreach (var element in invoiceItems)
    {
      displayHere.AppendText(element.ToString() + Environment.NewLine);
    }

}


.... I have tried this in both the Publc DisplayFrom() block and created my own block netiher works.


Now the error and my problem.
:: When in some test codes I utilize the method of display it works, the AppendText...

:: The ojbect called invoiceItems is in the standard Winform public DisplayForm() right below the initialzeComponent(). And the button object can't see the object 'invoiceItems'. I am guessing it's because the scope of the two objects is my problem.

:: I have tried creating my array of objects in a public void class but that didn't work either (Told you I was a N00B)

How do I get the objects in the Button Action to see the instatiated array object of items?

Lastly, any help is appreciated and I want to thank you in advance. Yes I have been googleing for hours and gave up so I am outside weeding.

Scott
Posted
Comments
[no name] 31-Mar-13 15:04pm    
You need to move the declaration for invoiceItems to the form level scope. As it is now, it is a local variable to the form constructor.

InvoiceItems array variable is visible only inside DisplayForm() procedure.
Please, read about: Scopes (C#)[^]
 
Share this answer
 
QueryClass QClasses = new QueryClass();
           Invoice[] invoiceItems = new Invoice[8];


write this outside the DisplayForm();
There is a problem with scope of the variable
invoiceItem
.
Declare it as a class level variable.
currentlu this is local variable to
DisplayForm()
 
Share this answer
 
I just want to thank both Mr.Priyank (clever pseudonym) and Maciej Los for the help. Of course problem resolved I hope I get an A on the project.

Scott
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900