Hi,

It is possible convert ArrayList object to bytes in CF?

I know that with full framework its possible...

--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL

Re: ArrayList to bytes by Neil

Neil
Wed Jul 02 04:54:09 CDT 2008

On 2008-06-26 18:08:54 +0100, "Armando Rocha"
<armandorocha@ifthensoftware.com> said:

> It is possible convert ArrayList object to bytes in CF?

Do this:


byte[] array = (byte[])list.ToArray(typeof (Byte));

--
Neil Cowburn MVP
Principal Partner
OpenNETCF Consulting


Re: ArrayList to bytes by Armando

Armando
Wed Jul 02 09:08:41 CDT 2008

hi,

when a try run your code ( byte[] array = (byte[])list.ToArray(typeof
(Byte));) its give me a InvalidCastException.

--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL
"Neil Cowburn" <neilc@[nospam]opennetcf.[nospam]com> escreveu na mensagem
news:2008070210540916807-neilc@nospamopennetcfnospamcom...
> On 2008-06-26 18:08:54 +0100, "Armando Rocha"
> <armandorocha@ifthensoftware.com> said:
>
>> It is possible convert ArrayList object to bytes in CF?
>
> Do this:
>
>
> byte[] array = (byte[])list.ToArray(typeof (Byte));
>
> --
> Neil Cowburn MVP
> Principal Partner
> OpenNETCF Consulting
>


Re: ArrayList to bytes by Neil

Neil
Wed Jul 02 09:37:11 CDT 2008

On 2008-07-02 15:08:41 +0100, "Armando Rocha"
<armandorocha@ifthensoftware.com> said:

> when a try run your code ( byte[] array = (byte[])list.ToArray(typeof
> (Byte));) its give me a InvalidCastException.

The InvalidCastException tells me that the ArrayList, list, contains
types that are not of type Byte.

If you want to convert an ArrayList that's populated like this:

ArrayList list = new ArrayList();
list.Add("one");
list.Add(2);
list.Add(true);

then you need to enumerate each element in the ArrayList and manually
convert to bytes.

--
Neil Cowburn MVP
Principal Partner
OpenNETCF Consulting


Re: ArrayList to bytes by Armando

Armando
Wed Jul 02 10:19:51 CDT 2008

Hi,

my arraylist collect string types.

I read lines from a text file, and i add to arraylist each line:

TextReader sr = new StreamReader(PATH+ @"\LOG.txt");
String sLine = "";
aList = new ArrayList();

while ((sLine = sr.ReadLine()) != null)
{
if (Count > 0)
{
//### ARRAYLIST ####
aList.Add(sLine);
}
Count++;
}
sr.Close();

//After i want convert my ArrayList (aList) to byte array like you tould;
if (aList.Count > 0)
byte[] array = (byte[])aList.ToArray(typeof(string));

but its give me a InvalidCastException.

I hope that sample above helps you to help me... :)


--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL
"Neil Cowburn" <neilc@[nospam]opennetcf.[nospam]com> escreveu na mensagem
news:2008070215371143658-neilc@nospamopennetcfnospamcom...
> On 2008-07-02 15:08:41 +0100, "Armando Rocha"
> <armandorocha@ifthensoftware.com> said:
>
>> when a try run your code ( byte[] array = (byte[])list.ToArray(typeof
>> (Byte));) its give me a InvalidCastException.
>
> The InvalidCastException tells me that the ArrayList, list, contains types
> that are not of type Byte.
>
> If you want to convert an ArrayList that's populated like this:
>
> ArrayList list = new ArrayList();
> list.Add("one");
> list.Add(2);
> list.Add(true);
>
> then you need to enumerate each element in the ArrayList and manually
> convert to bytes.
>
> --
> Neil Cowburn MVP
> Principal Partner
> OpenNETCF Consulting
>


Re: ArrayList to bytes by Neil

Neil
Wed Jul 02 10:51:50 CDT 2008

On 2008-07-02 16:19:51 +0100, "Armando Rocha"
<armandorocha@ifthensoftware.com> said:

> Hi,
>
> my arraylist collect string types.
>
> I read lines from a text file, and i add to arraylist each line:
>
> TextReader sr = new StreamReader(PATH+ @"\LOG.txt");
> String sLine = "";
> aList = new ArrayList();
>
> while ((sLine = sr.ReadLine()) != null)
> {
> if (Count > 0)
> {
> //### ARRAYLIST ####
> aList.Add(sLine);
> }
> Count++;
> }
> sr.Close();
>
> //After i want convert my ArrayList (aList) to byte array like you tould;
> if (aList.Count > 0)
> byte[] array = (byte[])aList.ToArray(typeof(string));
>
> but its give me a InvalidCastException.
>
> I hope that sample above helps you to help me... :)

If all you want to do is read a file to a byte array, then why not use
FileStream instead of StreamReader?

byte[] buffer;
using(FileStream fs = File.OpenText(Path.Combine(PATH, "Log.txt")))
{
buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
}

--
Neil Cowburn MVP
Principal Partner
OpenNETCF Consulting