Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
I've been working on an app to manage my iTunes collection, and have run into a bit of a snag. I've created two classes:

Column – Describes a column of information in the iTunes database
Track – Contains an array of Columns

When I instantiate a new Track object and attempt to build the array of Columns, each "new Column" statement overwrites the value of the previous array element.

I'm sure I'm missing something obvious – any ideas?


C#
// this statement...
Track t = new Track();
// produces this output:
// array element 0 = Name
// array element 0 = Artist
// array element 1 = Artist

// Begin Column class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestBed
{
    public class Column
    {
        private static string _name;
        private static string _datatype = "string";
        private static Int16 _length = 0;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public string DataType
        {
            get { return _datatype; }
            set { _datatype = value; }
        }
        public Int16 Length
        {
            get { return _length; }
            set { _length = value; }
        }
        public Column(string name, string datatype, Int16 length)
        {

            _name = name;
            _datatype = datatype;

            if (datatype == "string" && length > 0)
            {_length = length; }
            else
            {_length = 0;}
        }

    }
}
// Begin Track class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestBed
{
    public class Track
    {
        private static Column[] _trackcolumns = new Column[2];
        public static Column[] TrackColumns
        {
            get {return _trackcolumns;}
            set { _trackcolumns = value; }
        }
        public Track()
        {

            _trackcolumns[0] = new Column("Name", "string", 30);
            Console.WriteLine("array element 0 = {0}",_trackcolumns[0].Name);

            _trackcolumns[1] = new Column("Artist", "string", 30);
            Console.WriteLine("array element 0 = {0}", _trackcolumns[0].Name);
            Console.WriteLine("array element 1 = {0}", _trackcolumns[1].Name);

        }
    }
Posted

static member variables, why oh Lord tell me why?

Regards,

Manfred
 
Share this answer
 
This will explain why your static variables are giving you problems:

Static Keyword Demystified[^]
 
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