IT Community - Software Programming, Web Development and Technical Support

Type conversion from arraylist to string in C#

This is a discussion on Type conversion from arraylist to string in C# within the C# Programming forums, part of the Software Development category; Hi everyone, I've been trying to convert from arraylist type to string type, but all my efforts have been ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-30-2007, 09:07 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Type conversion from arraylist to string in C#

Hi everyone,

I've been trying to convert from arraylist type to string type, but all my efforts have been in vain. Does anyone know how to this? I have already tried the .ToString(), the type converter, etc. but I always get the same result. Here is my code:

string CheckingString(string s)

{

ArrayList vector = new ArrayList();

string result = s;

for (int i = 0; i < s.Length; i++)

{

char x = System.Char.ToLower(sIdea [i]);

int j = Convert.ToInt32(x);

if ((j >= 47 && j <= 58) || (j >= 65 && j <= 90) || (j >= 97 && j <= 122))

{

vector.Add(x);

}

}

result = vector.ToString().Trim();

return result;

}

results get the value "System.Collections.ArrayList", which is not what I want. I want result to get the value of s modified.
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-30-2007, 09:17 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: Type conversion from arraylist to string in C#

You can't convert the type ArrayList to a string because an ArrayList is not an Object, it is a Collection( Collection of Objects )- You would have to retrive each string in the ArrayList, like this:

<code>
ArrayList a = new ArrayList();
string str = "";

foreach( string s in a )
{
str = s;
}

return str;

</code>

But you have to realize that this will only work if there is only a single string in the arraylist. If there is more than one string, this will return the last string value in the ArrayList.
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-30-2007, 09:18 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: Type conversion from arraylist to string in C#

Thank you for your replies. I finally used this:

<code>

result = System.String.Concat(vector.ToArray());


</code>

First, I convert the arraylist into an array, and then call this system.string which converts it into a string.
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...

Last edited by oxygen : 07-30-2007 at 09:21 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-30-2007, 09:47 AM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Thumbs up Re: Type conversion from arraylist to string in C#

hi....Oxy.....

I was looking for a way to convert an array my self exept non of the ways explained her could help...

So i made my own code:

ArrayList lines = new ArrayList();
foreach (string line in File.ReadAllLines(theDir1))
{
lines.Add(line);
}
foreach (string line in File.ReadAllLines(theDir2))
{
lines.Add(line);
}
foreach (string line in File.ReadAllLines(theDir3))
{
lines.Add(line);
}
File.WriteAllText(theDir4, "");
foreach (string line in lines)
{
temp = line;
temp += "\n";
File.AppendAllText(theDir4, temp);
}

This code fills "array lines" with text from files and then writes the array to file but you could do just about anything with the "string line". I had to transfer the value in "string line" to "string temp" since you would get this error trying to edit line:
Cannot assign to 'line' because it is a 'foreach iteration variable'.

Im no codewiz but i hope it helps ya.


<code>
public string ArrayListToString(System.Collections.ArrayList ar)
{
return ArrayListToString(ar, ',');
}
public string ArrayListToString(System.Collections.ArrayList ar, char delim)
{
return ArrayListToString(ar, delim.ToString());
}
public string ArrayListToString(System.Collections.ArrayList ar, string delim)
{
return string.Join(delim, (string []) ar.ToArray(typeof(string)));
}
</code>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Which is best Vector or ArrayList? leoraja8 Java Programming 7 09-25-2007 12:05 AM
How to do a type conversion in VC++ .NET? bluesky C and C++ Programming 2 08-10-2007 05:38 AM
Difference between Array and ArrayList leoraja8 Java Programming 1 05-11-2007 04:06 AM
Arraylist leoraja8 Java Programming 1 05-11-2007 03:47 AM
Convert Object to Arraylist and Arraylist to Object raja C# Programming 0 05-08-2007 03:26 AM


All times are GMT -7. The time now is 11:28 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0