1

Again i run into an error i don't mean to bug anyone but I'm getting an error on this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Input_Program
{
    class Program
    {
       private static void Main()
        {

           char Y;
            char N;

           Console.WriteLine("Welcome to my bool program!");
           Console.WriteLine("Input a NON capital y or n when told to.");




            if(Y == 'y')
            {
                Console.WriteLine("Thank you,Please wait.....");
            }
        }
    }
}

Thanks for you answers!

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
unknown
  • 57
  • 3
  • 11

4 Answers4

1

Your variable char Y is not initialized before using. Try to give a default value when declaring.

EDIT It seems that you want the users to input something, and assign it to the variable Y. Try:

Y = Console.ReadKey().KeyChar;
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • But if it has a value then i can't do the if else's – unknown Mar 23 '11 at 03:16
  • `Console.ReadKey()` allows users input something, and assign it to the variable `y`, you can use `if-else` to do something according to the value of `y`. – Cheng Chen Mar 23 '11 at 03:21
1
if(Y == 'y')

Y is a local variable which isn't assigned anything. So, you assign it any value before the if statement to make any comparison.

Y = 'a';  // or some character 
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • If `Y` is assigned `a`, then `Y=='y'` yields to **false** and so the statements in `if` won't execute. – Mahesh Mar 23 '11 at 03:20
1

You're not setting Y to anything, and you're also not reading anything from the keyboard.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
1

You could explicitly set it to null.

char Y = '<whatever_is_the_default_char>';

That would get rid of the compiler error.

The root cause of the compiler error is that when it goes to compile the if conditional nothing as been assigned to Y. The above is considered an assignment.

JTew
  • 3,149
  • 3
  • 31
  • 39