locked
Using the generic type "System.Collections.ObjectModel.Collection<T> require 1 type argument RRS feed

  • Question

  • Hi, I'm quite new to C# programming and I'm unable to figure out through this error. I'm trying to do a make an array at t and replacing the array with new numbers with a timer to plot a graph that varies over time. Much appreciated if someone is able to help me or give me some ideas.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System.Windows.Threading;
    using System.Collections.ObjectModel;
    using System.Collections;

    namespace Project
    {
    public partial class temp : PhoneApplicationPage
    {

        double Min = 50;
        double Max = 0;
        double[] AvgTemp = new double[101];

        public temp()
        {
            InitializeComponent();
        }

        private void Pivot_Loaded(object sender, RoutedEventArgs e)
        {
            {
                DispatcherTimer time = new DispatcherTimer();
                time.Tick += time_Tick;
                time.Interval = TimeSpan.FromSeconds(1);
                time.Start();
            }
        }

        int a = 0,t=0;
        void time_Tick(object sender, EventArgs e)
        {
            DispatcherTimer time = new DispatcherTimer();
            Random rnd = new Random();
            double TempRandom = rnd.Next(100, 500); //generate random number
            double TempRandom2 = TempRandom / 10;


            CurrentTemp.Text = TempRandom2.ToString("0.0");
            ViewModel.Collection.Add(new Model(t++, TempRandom2));
            var Collection = new List<Collection>();
                if(Collection[t] == 10)
                {
                    Collection.RemoveAt(t);
                    t--;
                }


            if (TempRandom2 > Max)
            {
                Max = TempRandom2; //Highest Temp = Current Temp

                HighestTemp.Text = Max.ToString("0.0");
            }

            if (TempRandom2 < Min)
            {
                Min = TempRandom2;
                LowestTemp.Text = Min.ToString("0.0"); //Lowest Temp = Current Temp
            }

            AvgTemp[a++ % 100] = TempRandom2;
            if (a > 100)
                AverageTemp.Text = AvgTemp.Average().ToString("0.0");
            else
                AverageTemp.Text = (AvgTemp.Sum() / a).ToString("0.0");
        }
    }

    public class Model
    {
        public double X { get; set; }
        public double Y { get; set; }

        public Model(double x, double y)
        {
            X = x;
            Y = y;
        }
    }

    public class ViewModel
    {
        public static ObservableCollection<Model> Collection { get; set; }
        public ViewModel()
        {
            Collection = new ObservableCollection<Model>();
        }

    }
        }

            
    Monday, September 22, 2014 9:26 AM

Answers

  • You could change the t++ to t = (t + 1) % 10.

    % is the modulo operator that calculates the remainder of the number after dividing by 10.

    E.g. 1 / 10 = 0, remainder 1, 11 / 10 = 1, also remainder 1. So t = (t + 1) % 10 would set t to 0 as soon as t + 1 equals 10.

    • Marked as answer by JCTANN Thursday, September 25, 2014 2:02 AM
    Wednesday, September 24, 2014 7:42 AM

All replies

  • You shouldn't be using the word Collection as a property name at all. It's a reserved class name (http://msdn.microsoft.com/en-us/library/ms132397(v=vs.110).aspx). Instead refactor your property name to something more custom, such as: MyCollection or ModelsCollection.

    Cheers, Fred

    If my post helped you please mark it as helpful or answer.


    • Proposed as answer by Freddixx Wednesday, September 24, 2014 11:14 AM
    Monday, September 22, 2014 10:25 AM
  • Whenever I change the name Collection into other names, my program still runs but I won't be able to view the models. I'm currently trying out a charting extension (sparrowchart).
    Tuesday, September 23, 2014 3:37 AM
  • Your problem is here:

    CurrentTemp.Text = TempRandom2.ToString("0.0");
            ViewModel.Collection.Add(new Model(t++, TempRandom2));
            var Collection = new List<Collection>();
                if(Collection[t] == 10)

    You've used Collection as a variable name (var Collection), Collection as a property name (ViewModel.Collection) and a type name (List<Collection>).

    This particular error is related to List<Collection> - Collection is a generic type, so <Collection> isn't a valid type in the .Net type system. You need List<Collection<Model>> for your code to work here.

    I'd highly look at fixing up your naming, though ;)


    http://about.me/bc3tech

    • Proposed as answer by Brandon H_ Wednesday, September 24, 2014 12:53 AM
    Wednesday, September 24, 2014 12:49 AM
  • I've renamed Collection to myCollection and removed these 2 lines

            var Collection = new List<Collection>();
                if(Collection[t] == 10)

    Program works fine just that I want the t to reset itself after t++ reaches 10 but can't seem to think of a way to do so even with watching tutorials on YouTube.

    I've just started to learn C# on my own not long ago, so I'm not adept at writing codes in this language but the help people give me in forums are useful and much appreciated!

    Wednesday, September 24, 2014 1:48 AM
  • You could change the t++ to t = (t + 1) % 10.

    % is the modulo operator that calculates the remainder of the number after dividing by 10.

    E.g. 1 / 10 = 0, remainder 1, 11 / 10 = 1, also remainder 1. So t = (t + 1) % 10 would set t to 0 as soon as t + 1 equals 10.

    • Marked as answer by JCTANN Thursday, September 25, 2014 2:02 AM
    Wednesday, September 24, 2014 7:42 AM