 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 27th, 2005, 07:11 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
fillin array with letters from a to Z and numbers
hi,
I have an array that i need to fill with all characters from A to Z and also numbers from 1 to 9 ... how can i do this?
Offcourse there is a way to write down each of 36 manually or use a for loop! ... but i m looking for a shortcut if anybody knows ... like in ada, it shud be something like this:
type IntTest is array (A..Z) of character;
is there anything like (A..Z) or (1..2) in C# ?
Thx
|
|

April 27th, 2005, 07:17 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This may not be a real solution to the question.
Any how without loop it is possible in one line
char[] arr = ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p...").Split();
|
|

April 27th, 2005, 07:29 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I also used a still smaller one -> char[] arr = ("ABCD...").ToCharArray
|
|

April 27th, 2005, 08:31 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
isnt there anything like in ada? :
type IntTest is array (A..Z) of character;
this will include all 26 characters from A to Z
|
|

April 27th, 2005, 10:47 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
What is ada?
|
|

April 27th, 2005, 11:57 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
lolz ... ada is the name of a programming language ... like pascal, c++, etc
|
|

April 29th, 2005, 11:33 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Why don't you just loop through the char values and put them in the array?
Jacob.
|
|

April 29th, 2005, 12:08 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The code underneath will give you the an ArrayList of what you want. As you can see in the main method... you can both give the ASCII value or the char values. It is trivial to convert if you insist on using an array...
Code:
class SomeClass
{
[STAThread]
static void Main(string[] args)
{
ArrayList list = null;
FillChars('0', '9', (list = new ArrayList()));
FillChars('A', 'Z', list);
FillChars('a', 'z', list);
foreach(char c in list)
Console.Write(c);
Console.WriteLine();
FillChars(48, 57, (list = new ArrayList()));
FillChars(65, 90, list);
FillChars(97, 122, list);
foreach(char c in list)
Console.Write(c);
Console.WriteLine();
}
private static void FillChars(int start, int end, ArrayList chars)
{
int size = end - start + 1;
if(size > 0)
for(int i = 0, j = start; i < size; i++, j++)
chars.Add(Convert.ToChar(j));
}
private static void FillChars(char start, char end, ArrayList chars)
{
FillChars(Convert.ToInt32(start), Convert.ToInt32(end), chars);
}
}
Thats another solutions.
Jacob.
|
|

April 30th, 2005, 02:37 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Jacob,I OP wanted to put integers and chars in its collection
IMO it's better to have a class like below,
Code:
public class Items
{
IList m_items=new ArrayList(0);
private Items(int start,int end)
{
for(int i=start;i<=end;i++)
m_items.Add(i);
}
private Items(char start,char end)
{
for(char c=start;c<=end;c++)
m_items.Add(c);
}
public static IList GetItems(int start,int end)
{
Items temp=new Items(start,end);
return temp.m_items;
}
public static IList GetItems(char start,char end)
{
Items temp=new Items(start,end);
return temp.m_items;
}
}
_____________
Mehdi.
software student.
|
|

May 2nd, 2005, 07:09 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yep, lots of ways. I sometimes prefer to have these small static methods.
I would like to see how you fill the ArrayList (Items) with the values from the example? How do you merge the three ILists which you get out of the example, in an easy way (three intervals in the same list)? And BTW it seems like you put integers into the list (in one method), and therefore you have to typecast when you are going to use the values as char. As I see it, you do not get the same result from adding using int as adding using char?
However if you want to keep it in one class you could also inherrit the ArrayList. This way could look like this...
Code:
class ASCIIFiller : ArrayList
{
public void Add(int start, int end)
{
for(int i = start; i <= end; i++)
this.Add((char)i);
}
public void Add(char start, char end)
{
for(char c = start; c <= end; c++)
this.Add(c);
}
public override string ToString()
{
string str = "";
for(int i = 0; i < this.Count; i++)
str += this[i];
return str;
}
}
class MainTester
{
[STAThread]
static void Main(string[] args)
{
ASCIIFiller filler01 = new ASCIIFiller();
filler01.Add(48, 57);
filler01.Add(65, 90);
filler01.Add(97, 122);
Console.WriteLine(filler01.ToString());
ASCIIFiller filler02 = new ASCIIFiller();
filler02.Add('0', '9');
filler02.Add('A', 'Z');
filler02.Add('a', 'z');
Console.WriteLine(filler02.ToString());
Console.WriteLine((
(filler01.ToString().Equals(filler02.ToString()))?
"passed" : "failed"));
}
}
Naturally, one could leave out the ToString method to make the class even smaller ;)
Jacob.
|
|
 |