5 Replies - 353 Views - Last Post: 03 January 2013 - 11:59 AM Rate Topic: -----

#1 Kobernicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-January 13

Array problem

Posted 03 January 2013 - 06:34 AM

Hi everyone, first of all sorry for my english and thank you for the responses.

I'm a new programmer in c#. At the moment I'm busy with arrays. A month ago, I create a program, where the user has to put a character, after that the program had to see if it was an upppercase or a lowercase, it also can/ should what be able to recognize a number or special sign. You can specifie this, but I wanted to this with arrays. It won't work and I've been looking a couple of days now, but I can find what's wrong. Ofcourse it shows an error. But thats what I can't get right.
It's created in sharpdevelop 4.2, but I've also got visual studio 2010 ultimate.

When I try to run it, it underlines "c", in the if-statement and else-if.

This is the code.

using System;

namespace LetterArrayCA
{
	class Program
	{
		public static void Main(string[] args)
		{
			Vergelijken();
		}
		
		private static char[] Uppercase =
		{
			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
		};
		
		private static char[] Lowercase =
		{
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
		};
		
		private static char[] Number = 
		{
			'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
		};
		
		public static void Vergelijken()
		{
			char c;
			Console.WriteLine("Insert an character:");
			c = Convert.ToChar(Console.ReadLine());
			
			if (c == Uppercase)
			{
				Console.WriteLine(c + " is an Uppercase");
			}
			else if (c == Lowercase)
			{
				Console.WriteLine(c + " is a Lowercase");
			}
			else if (c == Number)
			{
				Console.WriteLine(c + " is a Number");
			}
				
			Console.Read();
		}
	}
}

This post has been edited by tlhIn`toq: 03 January 2013 - 07:26 AM
Reason for edit:: code tags added


Is This A Good Question/Topic? 0
  • +

Replies To: Array problem

#2 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: Array problem

Posted 03 January 2013 - 06:53 AM

you can not use "if(c == Uppercase)" as Uppercase is an array and you have to specify the position of the array you would like to compare to "c".

That means that you have to loop trough the array. For example like this:
foreach(char MyArrayChar in Uppercase)
{
    if(c == MyArrayChar)
        Console.WriteLine(C + " is an Uppercase");
}


This check has to be done on every array, if you work like you are doing now. That's why there exist multidimensional arrays. But in C# i suggest you use Lists(). Perhaps study how they work, they are very similair to arrays.

This post has been edited by Anthonidas: 03 January 2013 - 06:53 AM

Was This Post Helpful? 1
  • +
  • -

#3 Kobernicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-January 13

Re: Array problem

Posted 03 January 2013 - 07:02 AM

Thank you for youre help and I really appriciate your advice, I'll definitily study lists().

This post has been edited by tlhIn`toq: 03 January 2013 - 07:27 AM

Was This Post Helpful? 0
  • +
  • -

#4 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: Array problem

Posted 03 January 2013 - 08:09 AM

I think this could be helpful: http://msdn.microsof...y/6sh2ey19.aspx

There are thousends of other websites with tutorials to lists, perhaps with easyer explanations of how to use them. Anyway lists are very powerful, espacially if you use own classes into them. For example you could use something like that:

public class LetterCase
{
    public String Uppercase { get; set; }
    public String Lowercase { get; set; }
    public String Integer { get; set; }
}

List<LetterCase> MyCase = new List<LetterCase>();
.
.
.



And afterwards you can fill your list with all letters and numbers you want.

This post has been edited by Anthonidas: 03 January 2013 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4488
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: Array problem

Posted 03 January 2013 - 08:48 AM

As long as you're using .NET 3.5 or higher, you can include the namespace System.Linq and use the Enumerable.Contains function.

//other usings
using System.Linq;

//in your actual code
if(Uppercase.Contains(c))
  Console.Write("upper case");



This is a handy function to know how to use, because it works on any type of collection (that implements IEnumerable, which is most of the built in ones), and it works with any types. You can do the same thing with an array of strings, a list of integers, or a linked list of custom objects; it makes no difference.

However, this is the more complicated way of solving this problem. The simple way is to skip your arrays altogether and use the built in functions of the char type:

IsUpper
IsLower
IsDigit
Was This Post Helpful? 0
  • +
  • -

#6 Kobernicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-January 13

Re: Array problem

Posted 03 January 2013 - 11:59 AM

View PostCurtis Rutland, on 03 January 2013 - 08:48 AM, said:

As long as you're using .NET 3.5 or higher, you can include the namespace System.Linq and use the Enumerable.Contains function.

//other usings
using System.Linq;

//in your actual code
if(Uppercase.Contains(c))
  Console.Write("upper case");



This is a handy function to know how to use, because it works on any type of collection (that implements IEnumerable, which is most of the built in ones), and it works with any types. You can do the same thing with an array of strings, a list of integers, or a linked list of custom objects; it makes no difference.

However, this is the more complicated way of solving this problem. The simple way is to skip your arrays altogether and use the built in functions of the char type:

IsUpper
IsLower
IsDigit


Thank you for your reply, I'm aware of the IsUpper commands, I've used them. But I just wanted to make it with arrays, because I'm not good at thinking programs that I can create and if I know something, it's to difficult. I'm been in c# for 2 months, so I don't know to alot. It's also my first programminglanguage. I've only written batch and bash scrripts. But thanks you. I really can use contains, I've heard of it and tried it but it couldnt find that command. And now I know that I can need to create a new system, tnQ
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1