我不能在c#中调用方法

时间:2013-11-12 12:04:56

标签: visual-studio c#-4.0 visual-studio-2012

我最近选择了一个项目......

我无法在C#中调用方法。

在Visual Studio中进行了一些设置吗?我正在使用Visual Studio 2012 Express。

查看下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Classes;

namespace Web.Business
{
    public class TicketDB
    {


        Random r = new Random();

        r.Next(4); // Error 

    }  
}

3 个答案:

答案 0 :(得分:2)

您必须先定义一个方法。只是一堂课是不够的。

以下是Main()方法的示例,但您也可以使用其他方法(甚至是构造函数):

private static void Main()
{
  Random r = new Random();
  r.Next(4);
}

答案 1 :(得分:0)

您的班级代码应该更像:

public class TicketDB
{
  int rNum = 0;

  // Constructor - called when created
  public TicketDB()
  {
    Random r = new Random();
    rNum = r.Next(4); // Error 
  }
}

答案 2 :(得分:0)

能。我忘了在方法中打电话。我的错。 谢谢您的帮助。帮我记住了OOP。

相关问题