2012-11-06 4 views
0

내 아이디어는 텍스트 기반 어드벤처 게임을 만드는 것입니다.... 형식입니다 만 변수처럼 사용됩니다

클래스를 주 수업에서 사용하려고합니다. 'MyAdventure.Window'

는 '유형'하지만 내가하는 방법에 대한 확실하지 해요 '변수'

처럼 사용됩니다 I가 시도하고 있지만, 그것은 나에게 오류를 제공 이것을 해결하십시오. 클래스의 새 인스턴스를 만들려고했지만 작동하지 않는 것 같습니다. 나는 이것에 상당히 새롭다, 그러나 누군가는 도울 수 있었다?

미리 감사드립니다. 여기

내 메인 클래스 (Program.cs)에 대한 코드입니다 :

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

namespace MyAdventure 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 

     Console.WriteLine("You cannot feel your body. You don't know where you are.\nThe room you're in is dark. You cannot see much."); 
     Console.WriteLine("You decide to stand up, and take a look around. You see a door and a window.\nWhich do you check out first?"); 
     Console.WriteLine("A. Window\nB. Door"); 

     string userInput = Console.ReadLine(); 
     //Window theWindow = new Window(); 

     if (userInput.StartsWith("A", StringComparison.CurrentCultureIgnoreCase)) 
     { 
      Window(); 
     } 
     else if (userInput.StartsWith("B", StringComparison.CurrentCultureIgnoreCase)) 
     { 
      Console.WriteLine("You chose the door."); 
     } 

     Console.ReadKey(); 

    } 
} 

}

그리고 이것은 다른 클래스 Window.cs에 대한 코드 (지금까지)입니다 :

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

namespace MyAdventure 
{ 
public class Window 
{ 
    static void theWindow() 
    { 

     Console.WriteLine("You approach the window."); 
     Console.ReadKey(); 

    } 
} 

}

+1

당신이 원하는을'Window.theWindow()'제거해야합니다. – CodesInChaos

+0

또한 공용으로 만듭니다. public static void theWindow() – alfoks

+0

코드와 데이터를 구분할 수도 있지만 다른 교훈이라고 생각합니다. – neeKo

답변

3

올바른 구문은

if (userInput.StartsWith("A", StringComparison.CurrentCultureIgnoreCase)) 
    { 
     Window.theWindow(); 
    } 

당신은 클래스의 단순히 이름을 사용할 수 없습니다 다음되는 클래스의 정적 메소드를 호출합니다,하지만 당신은 전화를 정적 방법을 지정해야합니다 잠재적으로 많은 방법)도

Calling static methods

방법은 'theWindow'이루어져야 공개 그렇지 private by default inside a class

이다,

access level for class members and struct members, including nested classes and structs, is private by default

public static void theWindow() 
{ 
    Console.WriteLine("You approach the window."); 
    Console.ReadKey(); 
} 
+0

감사합니다. 나는 지금 당장 그것을 시도했지만 여전히 효과가 없다. 이제 나에게 이렇게 : 'MyAdventure.Window.theWindow()'보호 수준으로 인해 액세스 할 수 있습니다. myWindow 메서드를 정적이 아닌 다른 것으로 변경해야합니까? – user1779342

+0

다른 오류가 발생합니까? – Steve

+0

@ user1779342 public 메서드를 공개해야합니다 public static void theWindow() – alfoks

0

당신이 호출 할 필요가 있습니다

(하나를
.... 
... 
if (userInput.StartsWith("A", StringComparison.CurrentCultureIgnoreCase)) 
{ 
     Window.theWindow(); 
} 
... 
... 
0

theWindow()은 정적 방법입니다. 정적 멤버는 Window theWindow = new Window();했을 때 Window.theWindow()

, 당신은 Window 클래스의 인스턴스를 생성 한 사용자의 경우, ClassName.MethodName() 같이이라고합니다. 정적 멤버는 클래스의 인스턴스에서 액세스 할 수 없습니다.

인스턴스에서 해당 메소드를 호출하려면 static 수정

public class Window 
{ 
    public void theWindow() 
    { 
     Console.WriteLine("You approach the window."); 
     Console.ReadKey(); 

    } 
} 

을 사용

Window w = new Window(); //creating an instance of Window 
w.theWindow(); 
관련 문제