5
using System;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace LearningJustCode
{
    class Program
    {
        static void Main(string[] args)
        {
            Update();
        }

        static void Update()
        {
            var quote1 = new { Stock = "DELL", Quote = GetQuote("DELL") };
            var quote2 = new { Stock = "MSFT", Quote = GetQuote("MSFT") };
            var quote3 = new { Stock = "GOOG", Quote = GetQuote("GOOG") };

            var quotes = new object[] { quote1, quote2, quote3 };

            foreach (var item in quotes)
            {
                Console.WriteLine(item.Stock);
                Console.WriteLine(item.Quote.ToString());
            }
        Console.ReadKey();

        }

        static string GetQuote(string stock)
        {
            try
            {
                return InnerGetQuote(stock);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                return "N/A";
            }
        }

        static string InnerGetQuote(string stock)
        {
            string url = @"http://www.webservicex.net/stockquote.asmx/GetQuote?symbol={0}";
            var request = HttpWebRequest.Create(string.Format(url, stock));

            using (var response = request.GetResponse())
            {
                using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
                {
                    return reader.ReadToEnd();
                }
            }


        }
    }
}

I am getting an squiggley over item;Variable 'item' is only assigned to. This code has been slightly modified from Paul Kimmel's book C# Unleashed by Sams.

dannyrosalex
  • 1,794
  • 4
  • 16
  • 25

2 Answers2

14

Your array needs to be of the type of your stock quote. Your stock quote is an anonymous type, so we need to define the array anonymously as well. This can be done cleanly as:

    var quotes = new[]{  new { Stock = "DELL", Quote = "123" },
                         new { Stock = "MSFT", Quote = "123" },
                         new { Stock = "GOOG", Quote = "123" }};

    foreach (var item in quotes)
    {
        Console.WriteLine(item.Stock);
        Console.WriteLine(item.Quote.ToString());
    }
seraphym
  • 1,126
  • 1
  • 8
  • 21
  • What if my elements are coming from an array with an unknown length? Possible to define your items (with stock and quote) dynamicly from 2 arrays? – C4d May 31 '16 at 13:57
5

i guess the problem is in that line:

var quotes = new object[] { quote1, quote2, quote3 };

quotes is a object array, not an array of that anonymous type. the foreach also has just the object value. you could try to form the array within one line or within a lambda expression

A very dirty workaround is changing 'var' to 'dynamic'

        var quote1 = new { Stock = "DELL", Quote = ("DELL") };
        var quote2 = new { Stock = "MSFT", Quote = ("MSFT") };
        var quote3 = new { Stock = "GOOG", Quote = ("GOOG") };

        var quotes = new object[] { quote1, quote2, quote3 };

        foreach (dynamic item in quotes)
        {
            var r = item.Stock;

        }

a cleaner solution is leaving out 'object', so the compiler can generate an anonymous typed array

        var quote1 = new { Stock = "DELL", Quote = ("DELL") };
        var quote2 = new { Stock = "MSFT", Quote = ("MSFT") };
        var quote3 = new { Stock = "GOOG", Quote = ("GOOG") };

        var quotes = new [] { quote1, quote2, quote3 };

        foreach (var item in quotes)
        {
            var r = item.Stock;

        }
user287107
  • 9,286
  • 1
  • 31
  • 47
  • Thank you for not trashing me. Other responders have attitude problems!Not sure how to implement what you are suggesting. – dannyrosalex May 21 '12 at 22:09
  • That seems silly though ;) Better make use of type inference and do `var quotes = new[] { quote1, quote2, quote3 };`. This way you can keep the `var` keyword in the foreach loop. EDIT: Ok, I take it back. Saw your edit ;) – skarmats May 21 '12 at 22:17