473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem instantiating custom class as array elements

2
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? (complete code is attached)

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

Expand|Select|Wrap|Line Numbers
  1.     public class Column
  2.         private static string _name;
  3.         private static string _datatype = "string";
  4.         private static Int16 _length = 0;
  5.         public Column(string name, string datatype, Int16 length)
  6.         {
  7.             _name = name;
  8.             _datatype = datatype;
  9.             if (datatype == "string" && length > 0)
  10.             {_length = length; }
  11.             else
  12.             {_length = 0;}
  13.         }
  14.  
  15. // Begin Track class
  16.     public class Track
  17.         private static Column[] _trackcolumns = new Column[2];
  18.         public static Column[] TrackColumns
  19.         {
  20.             get {return _trackcolumns;}
  21.             set { _trackcolumns = value; }
  22.         }
  23.         public Track()
  24.         {
  25.             _trackcolumns[0] = new Column("Name", "string", 30);
  26.             Console.WriteLine("array element 0 = {0}",_trackcolumns[0].Name);
  27.             _trackcolumns[1] = new Column("Artist", "string", 30);
  28.             Console.WriteLine("array element 0 = {0}", _trackcolumns[0].Name);
  29.             Console.WriteLine("array element 1 = {0}", _trackcolumns[1].Name);
  30.         }
  31.  
Attached Files
File Type: txt fullcode.txt (1.8 KB, 345 views)
Feb 7 '12 #1
2 1951
GaryTexmo
1,501 Expert 1GB
I'd imagine it's because your member variables are static. This means that there is only one instance of that object across all instances of that class.

Make your class members non-static and that should fix you up.
Feb 8 '12 #2
kombu
2
That did the trick. Thanks, Gary!
Feb 8 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mick | last post by:
Hello, I know C++ but am new to C# so bear with me. I want to create an array of my own class. Something like this: /////////////////////////////////////// class CLoginInfo { public string...
2
by: Bülent Üstün | last post by:
Hi, i've a DLL file which contains a class i wrote named up2date (its not windows.forms). Which contains a constructor public up2date(string url,string domain) Now i want to load this DLL & use...
3
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in...
3
by: Nathan Sokalski | last post by:
I have an array declared as follows: Dim ButtonList() As NavButtonInfo and a Class defined as follows: Public Class NavButtonInfo Public Shared name As String
3
by: Andrew MacLean | last post by:
Hello all, I am fairly new to .NET, though highly experienced in older versions of VB. I am having a problem with a class I created, though. I have created a class called clsListItem based...
8
by: ljlevend2 | last post by:
If a property's type is an array and the property is exposed in a PropertyGrid, then the property can be expanded so that the user can view and edit the array elements. But, I've noticed that if...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
2
by: Steven Blair | last post by:
Hi, I want to know if this is the most efficient method of fixing my problem: I have a custom class and I need to maintain an array of these items. Initially I used MyCustom a = new MyCustom;...
5
by: Ben | last post by:
Does anyone have any example code showing how to sort a datagrid that is bound to a custom class array? My code calls a web method that returns an array of type LeadListDS. LeadListDS consists of...
0
by: =?Utf-8?B?Z2hwMTY=?= | last post by:
Hi, I am working on a web service method while will accept custom class as array and output antoher custom class. My code is public class a public str1 as string public str2 as string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.