Hans Rudel
Hans Rudel

Reputation: 3611

Generic List<T> class level variable

I'm trying to create a List<T> where the user specifies what the data type is by selecting it from a ComboBox on the UI. I've managed to create an object of the CustomEntity, which holds the List<T>, but once I exit from the Button1_Click event handler, the CustomEntity will go out of scope. Is it possible to create a class level variable? I tried but had to comment it out as it caused an error.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CustomClassWithGenericList
{
    public partial class Form1 : Form
    {
        //The following error is created: Cannot implicitly convert type
        //CustomClassWithGenericList.CustomEntity<decimal> to
        //CustomClassWithGenericList.CustomEntity<object>


        //private CustomEntity<object> input1;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (cb1.SelectedItem.ToString().ToUpper() == "DECIMAL")
            {
                input1 = new CustomEntity<decimal>();

                string[] temp = textBox1.Text.Split(',');

                foreach (string s in temp)
                {
                    decimal number;

                    if (decimal.TryParse(s, out number))
                    {
                        input1.inputValue.Add(number);
                    }
                    else
                    {
                        MessageBox.Show("Error occured.");
                    }
                }
            }
            else if(cb1.SelectedItem.ToString().ToUpper() == "INT")
            {
            }
            else if(cb1.SelectedItem.ToString().ToUpper() == "TIMESPAN")
            {
            }
        }
    }

    public class CustomEntity<T>
    {
        public List<T> inputValue; 

        public CustomEntity()
        {
            inputValue = new List<T>();
        }
    }
}

Upvotes: 0

Views: 1609

Answers (2)

Ph0en1x
Ph0en1x

Reputation: 10087

There are plenty of techniques could be used there. First of all you could create Object or dynamic type variable and just casting it any you want.

Also you could store your data in some state in memory (like some session or application state, or cache, stuff like that) and in such case just take it from this source.

Upvotes: 1

OnePie
OnePie

Reputation: 423

The commented out line uses type T, which is not a defined type. Not that you need to specify the type argument list of the generic at the point of decleration, and not anywhere else.

If you want the generic to be able to hold any object, you should simple pass the type "object" as the type argument into it.

If it can only a few different types though, creating one member variable per type might be a prefered option, depending on how you plan to use it later.

Upvotes: 1

Related Questions