Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
“ 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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda