Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
“ Expression denotes a `variable', where a `method group' was expected “ what does this mean?

I am learning c# as a beginner and making a program that gives the user a random number from a dice until it gets a six. Here is my complete code:

using System;

class HelloWorld {
  static void Main() {
        Random numberGen = new Random();

        int roll = 0;
        int attempts = 0;

        Console.WriteLine("Press enter to roll the die");

        while (roll != 6) {
            Console.ReadKey();

            roll = numberGen(1, 7);
            Console.WriteLine("You rolled " + roll);
            attempts++;
        }

        Console.WriteLine("It took you " + attempts + " to roll a six");
        Console.ReadLine();
  }
}

What am I doing wrong and how can I debug it?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The issue is here:

roll = numberGen(1, 7);

The only time you can use variable(...) syntax is when variable is a typed delegate (in which case the compiler interprets it as variable.Invoke(...)). In all other cases, it is expected that you access some method/property/field/indexer/event via the variable, using one of variable.Foo(...), variable.Foo or variable[index] (or -> in place of . if variable is an unmanaged pointer).

In this case, you want:

roll = numberGen.Next(1, 7);
over 4 years ago · Santiago Trujillo Report

0

In your code, you have created a variable named - 'numberGen'. Since its a variable of class 'Random', you need to call a method of this class using this variable like -

numberGen.Next(1,7);

Here 'Next' is a method of class Random, which takes 2 parameters, min value and max value.

The error you were getting was because you were using the variable as a method.

over 4 years ago · Santiago Trujillo Report

0

The deafult approach to generate random numbers in rangs:

Random rnd = new Random();
rnd.Next(1, 10);

In your case hust update the variable

roll = numberGen(1, 7);

to:

roll = numberGen.Next(1, 7);

Random - it's a class. Next - it's a method.

  • More about Classes in C# here
  • More about Methods in C# here
  • More about Random class here
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!