강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của songyuseung237872
songyuseung237872

câu hỏi đã được viết

[Loạt bài phát triển game MMORPG thực hiện bằng C# và Unity] Phần 1: Giới thiệu lập trình cơ bản C#

Sự kiện

static 에러

Viết

·

435

0

using System;

namespace ConsoleApp1

{

    class Program

    {

        public delegate void OnInputKey(); 

        public static event OnInputKey InputKey;

        static void TargetFunc() => Console.WriteLine("input received");

        static void Main()

        {

            InputKey += TargetFunc; 

            while (true)

            {

                Update();

            }

        }

        public static void Update()

        {

            if (Console.KeyAvailable == false)

            {

                return;

            }

            ConsoleKeyInfo info = Console.ReadKey();

            if (info.Key == ConsoleKey.A)

            {

                InputKey(); 

            }

        }

    }

}

제가 예문의 InputManager 클래스 안의 것을 전부 Program 클래스 안으로 몰아 넣어서 기능하다록 코드를 재구성 해봤는데, 여기서 InputKey와 TargetFunc 함수 선언부에 static을 붙이지 않으면 

"static이 아닌 필드 메서드 또는 속성 에 개체 참조가 필요합니다" 라는 에러가 뜨더라구요 

왜그런건가요?

C#

Câu trả lời 2

0

songyuseung237872님의 프로필 이미지
songyuseung237872
Người đặt câu hỏi

감사합니다!

0

Rookiss님의 프로필 이미지
Rookiss
Người chia sẻ kiến thức

유사한 상황에 대한 질문(static 관련)이 올라온 기억이 있어,
답변을 링크로 대체 드립니다.

https://www.inflearn.com/questions/97965

Hình ảnh hồ sơ của songyuseung237872
songyuseung237872

câu hỏi đã được viết

Đặt câu hỏi