C
C#2y ago
Redoxi

Why does this not work? [Answered]

Hey, pretty new to coding and trying some things out, someone knows why it says that my var "randomNumber" is not in the context?
using System;

namespace Awesome_Program
{
class Program
{
static void Main(string[] args)
{
Typewriter("Hello... Wizard!");
}

public static int NumberGen()
{
Random numberGen = new Random();
int number = numberGen.Next(50,300);
}

public static void Typewriter(string word)
{
for (int i = 0; i < word.Length; i++)
{
Console.Write(word[i]);
int randomNumber = NumberGen();
System.Threading.Thread.Sleep(randomNumber);
}
}
}
}
using System;

namespace Awesome_Program
{
class Program
{
static void Main(string[] args)
{
Typewriter("Hello... Wizard!");
}

public static int NumberGen()
{
Random numberGen = new Random();
int number = numberGen.Next(50,300);
}

public static void Typewriter(string word)
{
for (int i = 0; i < word.Length; i++)
{
Console.Write(word[i]);
int randomNumber = NumberGen();
System.Threading.Thread.Sleep(randomNumber);
}
}
}
}
7 Replies
canton7
canton72y ago
randomNumber = NumberGen; tries to assign a new value to an existing variable. However the variable randomNumber was never declared, so it doesn't exist at that point. I think you probably meant int randomNumber = NumberGen(); (You also need to call the NumberGen method, by putting () after the method name)
Redoxi
Redoxi2y ago
ahh i see okay i changed it, but now it says that it doesnt get a return value
Anu6is
Anu6is2y ago
you don't return the number that's generated in NumberGen()
Redoxi
Redoxi2y ago
Ohhh i understand so basically NumberGen has no var until i use return number; right?
Anu6is
Anu6is2y ago
right, it has no value until you actually return something in your case, yes number
Redoxi
Redoxi2y ago
Aight, thanks a lot!
Accord
Accord2y ago
✅ This post has been marked as answered!