Can i Box values in arrays

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

Is there a way to put my class vals to arrays !

RecordSet Track1 = new RecordSet("It aint fear",1);
RecordSet Track2 = new RecordSet("Gotta go",2);
RecordSet Track3 = new RecordSet("My life",3);
RecordSet Track4 = new RecordSet("Amazing",4);

Data d = new Data();

d.RecordAdd(Track1);
d.RecordAdd(Track2);
d.RecordAdd(Track3);
d.RecordAdd(Track4);

//Here i like put these on array so i can pass it as array is this do
able?
can i do boxing here to put Tracks on array ?
string [] strRecords = new String[5];
strRecords[0] = Track1;
string x = (string)strRecords[0];

d.GetRec2(strRecords);
 
Matt said:
Is there a way to put my class vals to arrays !

RecordSet Track1 = new RecordSet("It aint fear",1);
RecordSet Track2 = new RecordSet("Gotta go",2);
RecordSet Track3 = new RecordSet("My life",3);
RecordSet Track4 = new RecordSet("Amazing",4);

Data d = new Data();

d.RecordAdd(Track1);
d.RecordAdd(Track2);
d.RecordAdd(Track3);
d.RecordAdd(Track4);

//Here i like put these on array so i can pass it as array is this do
able?
can i do boxing here to put Tracks on array ?
string [] strRecords = new String[5];
strRecords[0] = Track1;
string x = (string)strRecords[0];

d.GetRec2(strRecords);

That's not boxing - that's converting records to strings. You could do
that if you defined conversion operators in RecordSet to and from
string, but I wouldn't recommend it. Why do the conversion to strings?
Why not just declare an array of RecordSets?
 
i thought i had to convert.
That would work for me but evertime i did it i got error
How can i put these tracs on array so i can pass it to my method below.

string [] strRecords = new String[5];
strRecords[0] = Track1; // this did not work.


public void GetRec2(string[] Records)
{
foreach (string str in Records)
{
Console.WriteLine("Records: " + str);
}
}
 
Matt said:
i thought i had to convert.

No, you can create an array of any type.
That would work for me but evertime i did it i got error
How can i put these tracs on array so i can pass it to my method below.

string [] strRecords = new String[5];
strRecords[0] = Track1; // this did not work.

No, it wouldn't, because Track1 isn't a string. Do:

RecordSet[] records = new RecordSet[5];
records[0] = Track1;

Then change the parameter for GetRec2 to be a RecordSet[].
 
This is what i have but i dont get the values ? what am i doing wrong
public void GetRec2(RecordSet[] Recs)
{
foreach (RecordSet Rec in Recs)
{
Console.WriteLine("Records: " + Rec);
}
}

RecordSet[] records = new RecordSet[5];
records[0] = Track1;
records[1] = Track1;

d.GetRec2(records);
 
Console.WriteLine(records[1]);
values is Recordset. Its not filling the array.

RecordSet Track1 = new RecordSet("Its raining",1);
RecordSet Track2 = new RecordSet("Amazing in the wild",2);
RecordSet Track3 = new RecordSet("Amazing in the milg",3);
RecordSet Track4 = new RecordSet("Amazing in the Sour",4);
RecordSet Track5 = new RecordSet("Amazing in the Hot",5);

Data d = new Data();
d.RecordAdd(Track1);
d.RecordAdd(Track2);
d.RecordAdd(Track3);
d.RecordAdd(Track4);
d.RecordAdd(Track5);

RecordSet[] records = new RecordSet[5];
records[0] = Track1;
records[1] = Track1;

d.GetRec2(records);

Console.WriteLine(records[1]);
 
Matt said:
This is what i have but i dont get the values ? what am i doing wrong
public void GetRec2(RecordSet[] Recs)
{
foreach (RecordSet Rec in Recs)
{
Console.WriteLine("Records: " + Rec);
}
}

RecordSet[] records = new RecordSet[5];
records[0] = Track1;
records[1] = Track1;

d.GetRec2(records);

That is definitely passing the values.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Here it goes Jon

using System;
using System.IO;
using System.Collections;
class RecordSet
{
private string Item;
private int val;
public RecordSet(string item,int ival)
{
Item = item;
val = ival;
}
public string GetRecord
{
get{return Item;}
set{Item = value;}
}
}
class Data
{
ArrayList List = new ArrayList();
public void RecordAdd(RecordSet r)
{
List.Add(r);
}
public void GetRec2(RecordSet[] Recs)
{
foreach (RecordSet Rec in Recs)
{
Console.WriteLine("Records: " + Rec);
}
}
public static void Main(string[] args)
{
RecordSet Track1 = new RecordSet("Its raining",1);
RecordSet Track2 = new RecordSet("Amazing in the wild",2);
RecordSet Track3 = new RecordSet("Amazing in the milg",3);

Data d = new Data();

d.RecordAdd(Track1);
d.RecordAdd(Track2);
d.RecordAdd(Track3);

RecordSet[] records = new RecordSet[5];
records[0] = Track1;
records[1] = Track1;
d.GetRec2(records);
}
}
 
Here it goes Jon

using System;
using System.IO;
using System.Collections;
class RecordSet
{
private string Item;
private int val;
public RecordSet(string item,int ival)
{
Item = item;
val = ival;
}
public string GetRecord
{
get{return Item;}
set{Item = value;}
}
}
class Data
{
ArrayList List = new ArrayList();
public void RecordAdd(RecordSet r)
{
List.Add(r);
}
public void GetRec2(RecordSet[] Recs)
{
foreach (RecordSet Rec in Recs)
{
Console.WriteLine("Records: " + Rec);
}
}
public static void Main(string[] args)
{
RecordSet Track1 = new RecordSet("Its raining",1);
RecordSet Track2 = new RecordSet("Amazing in the wild",2);
RecordSet Track3 = new RecordSet("Amazing in the milg",3);

Data d = new Data();

d.RecordAdd(Track1);
d.RecordAdd(Track2);
d.RecordAdd(Track3);

RecordSet[] records = new RecordSet[5];
records[0] = Track1;
records[1] = Track1;
d.GetRec2(records);
}
}
 
Matt said:
Here it goes Jon

Okay. Now, what are you expecting it to do that it's not doing?

Bear in mind the fact that you're never *using* the records you're
adding to the list - you're not using the list at all.
 
First of all sorry about the triple post. I have no idea how that
happend.
Just like to pass the
Pass the records array to d.GetRec2 function to print out. It prints
"Recorset".
 
Matt said:
First of all sorry about the triple post. I have no idea how that
happend.
Just like to pass the
Pass the records array to d.GetRec2 function to print out. It prints
"Recorset".

Well, it prints out:

Records: RecordSet
Records: RecordSet
Records:
Records:
Records:

The third, fourth and fifth elements in the array are all null, hence
the empty result. The first two just say "RecordSet" because you
haven't overridden ToString() in RecordSet - .NET doesn't know what you
want to do to convert it to a string.

A couple of other points:

1) Your array contains two references to the same RecordSet (Track1) -
is that what you intended?

2) Your property is called GetRecord, when normally it would be called
Record - GetRecord sounds more like the name of a method than a
property.
 
Matt said:
Thanks Jon
Just gave up. Will find different approach.

Why? You're managing to pass the RecordSets in as an array - you now
just need to use them properly.
 
This will do the jobs. Fill the string array with values
string[] records = new string[3];
records[0] = Track1.GetRecord;
records[1] =
records[2] =
cont.
pass the func as string will print the values.
public void GetRec2(string[] Recs)
{
foreach (string Rec in Recs)
{
Console.WriteLine("Records: " + Rec);
}
}
 
Matt said:
This will do the jobs. Fill the string array with values
string[] records = new string[3];
records[0] = Track1.GetRecord;
records[1] =
records[2] =
cont.
pass the func as string will print the values.
public void GetRec2(string[] Recs)
{
foreach (string Rec in Recs)
{
Console.WriteLine("Records: " + Rec);
}
}

That would certainly pass the value of GetRecord for each record, but
why use that when you can pass the records in an array and call
GetRecord on each one?
 
Got it !
Once i fill the the Array, pass it as recordset and use the Propety to
print out.
Wow Thanks mate !
public void GetRec2(RecordSet[] Recs)
{
foreach (RecordSet Rec in Recs)
{
Console.WriteLine("Records: {0}",Rec.GetRecord);
}
}
 
Back
Top