2017-08-28 15 views
0

HI私は、クラスライブラリプロジェクトを作成して3つのCreated(Questions、Answers、Categories)クラスを作成し、そのDLLを使用して以下のコードのようにサービスを作成しますWCF同じ外部エラーを使用するとエラーが発生する

public Answers InsertAnswer(Answers answer) 
{ 
    try 
    { 
     answer_ = new Answers(); 
     cs = ConfigurationManager.ConnectionStrings[csName].ConnectionString; 
     using (con = new SqlConnection(cs)) 
     { 
      cmd = new SqlCommand("InsertAnswer", con); 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.AddWithValue("@answer", answer.Answer); 
      cmd.Parameters.AddWithValue("@questionId", answer.QuestionId); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
      answer_ = GetAnswer(answer); 
     } 
    } 
    catch (Exception ex) 
    { 

    } 
    finally 
    { 
     con.Close(); 
     con.Dispose(); 
     cmd.Dispose(); 
    } 
    return answer_; 
} 

answer_は今、私は私のクライアントで同じ名前空間を使用してメソッドを呼び出すために同じサービスを使用しています

using QuestionsAnswers; 

名前空間の下にあるが、私は以下にエラー与えクラス回答のグローバルオブジェクトでありますクライアント側のコード

public void InserQuestionAnswer(QA qaParam) 
{ 
    try 
    { 
     Answers answer = new Answers(); 
     answer.QuestionId = 1; 
     answer.Answer = qaParam.Answer.ToString(); 
     QuestionsAnswersService.QuestionAnswerServiceClient questionAnswerService = new QuestionsAnswersService.QuestionAnswerServiceClient("BasicHttpBinding_IQuestionAnswerService"); 
     questionAnswerService.InsertAnswer(answer); 
    } 
    catch (Exception ex) 
    { 

    } 
} 

私は、私は以下のエラーを取得しています

using QuestionsAnswers; 

同じ参照を使用しています

Severity Code Description Project File Line Suppression State 
Error CS0120 An object reference is required for the non-static field, 
method, or property 'QAaspx.InserQuestionAnswer(QA)' QASamapleUI E:\Rohit 
Gupta\PracticeProjects\WebApp\QASamapleUI\QASamapleUI\QAaspx.aspx.cs 25 
Active 

は、誰もがあなたがメソッドため、このメッセージを取得している私に

+0

[CS0120:オブジェクト参照が非静的フィールド、メソッド、またはプロパティ「FOO」のために必要とされる]の可能な重複(https://stackoverflow.com/questions/498400/cs0120-an-object-reference - 非 - 静的フィールド法または小道具に必要) –

答えて

0

を助けてくださいあなたはQAaspx.InserQuestionAnswer(QA)と呼んでいますが静的ではない、明らかに私はオブジェクト参照を持っていないあなたがそれを呼んでいるときには、クラスです。

これは、そのクラスのインスタンスを作成してメソッドを呼び出すことで解決できます。コード内にどのクラスが含まれているかはわかりませんが、そのクラスがFooだとしましょう。そして、あなたはこれを行うことができます。その方法は、そのクラスの他のプロパティへの参照を必要としない場合

Foo myFoo = new Foo(); 
myFoo.InserQuestionAnswer(QA); 

あるいは、あなたはちょうどそれが静的にすることができます。

public static void InserQuestionAnswer(QA qaParam) {} 
関連する問題