6 Replies - 561 Views - Last Post: 30 January 2012 - 08:37 PM Rate Topic: -----

#1 wreken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 12

Pulling Data and displaying data from a "List"

Posted 29 January 2012 - 04:57 PM

I am having trouble pulling specific data and displaying it from a "list" that I assembled.
Please see the test display I am trying to do pulling the data from line 2 in the list.


    public partial class Form1 : Form
    {
        public Form1()
        {

           InitializeComponent();
        }
        decimal soapsTotal = 0;
        decimal itemtracker = 0;
        string n1 = "qq";

        List<Soaps> soapItems = new List<Soaps>();

         
        private void Form1_Load(object sender, EventArgs e)
        {

            soapItems.Add(new Soaps("Two Sheep", 12, 24.95, Image.FromFile("sheep.jpg")));
            soapItems.Add(new Soaps("Chicks", 14, 19.95, Image.FromFile("chicks.jpg")));
            soapItems.Add(new Soaps("Robin Eggs", 15, 29.95, Image.FromFile("robin.jpg")));
            soapItems.Add(new Soaps("Piglets", 15, 29.95, Image.FromFile("pigs.jpg")));
            soapItems.Add(new Soaps("Scottie Dog", 6.5, 24.95, Image.FromFile("scottie.jpg")));
            soapItems.Add(new Soaps("Tres Chic", 8.5, 24.95, Image.FromFile("treschic.jpg")));
            soapItems.Add(new Soaps("Lightfoot's Pure Pine", 10, 19.95, Image.FromFile("lightfoots.jpg")));
             
            
            soapsTotal = soapItems.Count;
          
          
 

            updateControls();

        }

         private void updateControls()
        {

            
            n1 = soapItems[2].SoapName;
            lblDescription.Text = n1;


             

        }

         private void label1_Click(object sender, EventArgs e)
         {

         }

       
      

    }





The New Class is as follows:

    class Soaps
    {
         string _SoapName;
         decimal _SoapWeight;
         decimal _SoapPrice;
         Image _SoapImage;



        public string SoapName
        {
            get { return _SoapName; }
            set { _SoapName = value; }
        }

        public decimal SoapWeight
        {
            get { return _SoapWeight; }
            set { _SoapWeight = value; }
        }

        public decimal SoapPrice
        {
            get { return _SoapPrice; }
            set { _SoapPrice = value; }
        }

        public Image SoapImage
        {
            get { return _SoapImage; }
            set { _SoapImage = value; }
        }



        public Soaps(string sname, double swt, double sprice, Image simage)
        {
            string _SoapName = sname;
            double _SoapWeight = swt;
            double _SoapPrice = sprice;
            Image _SoapImage = simage;
        }
    }


}


Thanks
Ken M.

Is This A Good Question/Topic? 0
  • +

Replies To: Pulling Data and displaying data from a "List"

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,759
  • Joined: 02-June 10

Re: Pulling Data and displaying data from a "List"

Posted 29 January 2012 - 05:06 PM

First: ImageFromFile is evil. Don't use it. IT keeps a constant connection to the file on the HDD. Read it as a byte[] then make an image from that.


What does "having trouble" entail? Exception error?

What does "line 2 of the list mean"? The 2nd element or are you referring to a line number in code?
Was This Post Helpful? 0
  • +
  • -

#3 wreken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 12

Re: Pulling Data and displaying data from a "List"

Posted 29 January 2012 - 05:39 PM

Yes, output on form is blank, like I am pulling blank data.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,759
  • Joined: 02-June 10

Re: Pulling Data and displaying data from a "List"

Posted 30 January 2012 - 08:21 AM

Let's talk about this constructor
public Soaps(string sname, double swt, double sprice, Image simage)
        {
           _SoapName = sname;
           _SoapWeight = swt;
           _SoapPrice = sprice;
           _SoapImage = simage;
        }



You should set the property, not the field behind. Its a better habit to get in to. That way if you upgrade the property to something more advanced you don't have to change anything else. For example

public Soaps(string sname, double swt, double sprice, Image simage)
        {
           SoapName = sname;
           SoapWeight = swt;
           SoapPrice = sprice;
           SoapImage = simage;
        }
string SoapName
{
    get { return _SoapName; }
    set
       {
         _SoapName = value;
         NotifyPropertyChange("SoapName");
         LogThis("New Soap object made");
         AddToStatusLine("New Soap: " + value);
       }




After all one of the advantages of properties is they have methods built in that do things. If you aren't taking advantage of that you might as we not use the field behind.

Can you please put a breakpoint on this line and show us a screenshot with the value of soapItems[2] opened in the Locals pallet?


40 n1 = soapItems[2].SoapName;

This post has been edited by tlhIn`toq: 30 January 2012 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Pulling Data and displaying data from a "List"

Posted 30 January 2012 - 08:30 AM

View PosttlhIn`toq, on 30 January 2012 - 11:21 AM, said:

Let's talk about this constructor
public Soaps(string sname, double swt, double sprice, Image simage)
        {
            string _SoapName = sname;
            double _SoapWeight = swt;
            double _SoapPrice = sprice;
            Image _SoapImage = simage;
        }



You should set the property, not the field behind.


He's not even setting the field behind it. He is creating 4 new objects in the constructor that die when the constructor exits.

This is why he is getting blank data. He isn't actually setting the property values.

The constructor should be this..

public Soaps(string sname, double swt, double sprice, Image simage)
        {
            SoapName = sname;
            SoapWeight = swt;
            SoapPrice = sprice;
            SoapImage = simage;
        }



(notice that I don't have the types in-front of the object names)
Was This Post Helpful? 2
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,759
  • Joined: 02-June 10

Re: Pulling Data and displaying data from a "List"

Posted 30 January 2012 - 09:18 AM

wrekenWhat this shows us is that you aren't familiar with breakpoints and how to debug your own code.

Learning to debug one's own code is an essential skill. Sadly, one that apparently no college course teaches. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

This post has been edited by tlhIn`toq: 30 January 2012 - 09:18 AM

Was This Post Helpful? 1
  • +
  • -

#7 wreken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 12

Re: Pulling Data and displaying data from a "List"

Posted 30 January 2012 - 08:37 PM

You guys hit right on the head. Thank you So much. I also will take your advise and read the applicable FAQ instruction about debugging.
I am an older student and am eager to learn. I spent hours trying to find it on my own before asking you guys on this forum.
You both are fantastic.
Thanks for the honesty and Im sure I will be back again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1