2017-09-26 65 views
1

所以我的节目让我发送邮件使用WSDL请求类下面是由WSDL提供的类:如何创建在C#中使用自定义名称的循环

CreateCustomerNoteRequest createCustomerNotesRequestInfo = new CreateCustomerNoteRequest(); 

使用这个类我必须设置变量像这样:

 //FIRST WRITING NOTE TO OLD ACCOUNT TO SAY ITS BEEN COMPRIMISED AND SHOW NEW CUSTOMER NUMBER: 
      createCustomerNotesRequestInfo.UserName = username; 
      createCustomerNotesRequestInfo.Password = password; 
      createCustomerNotesRequestInfo.SystemToken = "sysToken"; 
      createCustomerNotesRequestInfo.Note = new CustomerNote(); 
      createCustomerNotesRequestInfo.Note.CustomerNumber = cloneCustomerNumber; 
      createCustomerNotesRequestInfo.Note.Category = new CustomerServiceWSDL.LookupItem(); 
      createCustomerNotesRequestInfo.Note.Category.Code = "GEN"; 
      createCustomerNotesRequestInfo.Note.Details = "Account Takeover – Fraud. Acc – " + customerNumberTextBox.Text + " closed as compromised and new account " + newCloneCustomerNumber + " created matching existing data"; 

而且玩完我用这让我的回应:

createCustomerNotesResponse = soapClient.CreateCustomerNote(createCustomerNotesRequestInfo); 

,一切工作正常。我现在想要做的是因为我有多个Notes我想循环这个过程,所以取决于多少注意它会创建多个实例。

我顺利拿到所有的笔记到像这样使用notecount提供的票据有(由WSDL给出)多少数量的列表,以便所有的已经很不错了:

 try 
      { 
       for (int i = 0; i <= notesCount; i++) 
       { 
        customerNotesArrayList.Add(getCustomerNotesResponse.Notes.Items[i]); 
        //i++; 
       } 
      } 

我想什么这样做:现在取决于票据指望我想创造,许多这样的:

CreateCustomerNoteRequest createCustomerNotesRequestInfo = new CreateCustomerNoteRequest(); 

我尝试这样做:

 for (int i=0; i<=notesCount;i++) 
      { 
       CreateCustomerNoteRequest a[i] = new CreateCustomerNoteRequest(); 
      } 

但它不那么容易,所以我怎样才能做到这一点? 所以我想a1,a2,a3在哪里,然后循环所有的笔记在后面这应该不成问题。但首先创建这些是问题。

[编辑]

//Create Notes and copy over array contents... 
       CreateCustomerNoteRequest request = new CreateCustomerNoteRequest(); 
       for (int i = 0; i <= notesCount; i++) 
       { 
        request.UserName = username; 
        request.Password = password; 
        request.SystemToken = systemToken; 
        request.Note = new CustomerNote(); 
        request.Note.CustomerNumber = newCloneCustomerNumber; 
        request.Note.Category = new CustomerServiceWSDL.LookupItem(); 
        request.Note.Category.Code = customerNotesArrayList[i].NoteCategory.Code.ToString(); 
        request.Note.Details = customerNotesArrayList[i].NoteText; 

        var response = soapClient.CreateCustomerNote(request); 
       } 

回答

3

你声明中阵列循环,这意味着它将无法使用之后。此外,你需要事先声明数组大小:不是数组的

CreateCustomerNoteRequest[] a = new CreateCustomerNoteRequest[notesCount]; 
for (int i = 0; i < notesCount; i++) 
{ 
    a[i] = new CreateCustomerNoteRequest(); 
} 

// now you can use the array outside the loop as well 

,你可以选择使用List<CreateCustomerNoteRequest>,不首先需要一个大小声明。

请注意,如果你打算获得相同的循环内的音符,你将不再需要在阵列全天:

for (int i = 0; i < notesCount; i++) 
{ 
    CreateCustomerNoteRequest request = new CreateCustomerNoteRequest(); 
    var response = soapClient.CreateCustomerNote(request); 
    // todo process response 
} 
+0

尝试循环只发生一次以上,IV尝试了一个小时在解决绑定错误后仍然无法弄清楚。请参阅编辑 – Tantrix1

+0

在您的示例中,您已在'for'循环中使用'i == notesCount'作为条件 - 循环只在条件为'true'时继续。 –

+0

将其更改为for(int i = 0; i <= notesCount; i ++)仅运行一次。只创建一个音符。当设置int i = 1;它没有创造任何东西。 – Tantrix1

相关问题