简体   繁体   中英

C# string to list

I am really new to C# and am trying to troubleshoot a test of some code that uses Amazon SES to send email.

    [TestMethod()]
    public void SendEmailTest()
    {
        SESEmailProvider target = new SESEmailProvider();

        string ToEmailAddresses = "someone@gmail.com";
        string FromEmailAddress = "no-reply@mydomain.com";
        string Subject = "Test";
        string EmailBody = "Hello world.";
        string expected = string.Empty;
        string actual;
        actual = target.SendEmail(ToEmailAddresses, FromEmailAddress, Subject, EmailBody);
        Assert.AreEqual(expected, actual);
        Assert.Inconclusive("Verify the correctness of this test method.");
    }

The error message in Visual Studio is:

string SESEmailProvider.SendEmail(
 System.Collections.Generic.List<string> ToEmailAddresses,
 string FromEmailAddress, string Subject, string EmailBody)

Error:

 The best overloaded method match for 'MyServices.SESEmailProvider.SendEmail(
   System.Collections.Generic.List<string>, string, string, string)'
  has some invalid arguements.

I think the issue is that it's expecting ToEmailAddresses to be a list, not a single string, but I'm struggling to find a way to convert/handle this.

Thanks!

well you've defined the variable ToEmailAddresses as a string, but you want it to be a list.

there are many ways to define and fill a list, but the most compact is:

var toEmailAddresses = new List<string> { "someone@gmail.com" }

the above creates a new list, and initializes it with a single string "someone@gmail.com"

SendMail expects a List as it's first argument - not a string.

So, do:

List<String> toAddresses = new List<String>();
toAddresses.Add("someone@mail.com");

pass toAddresses as the first arg

The clue was in the error message. It expects List, string, string, string

The best overloaded method match for 'MyServices.SESEmailProvider.SendEmail(
System.Collections.Generic.List, string, string, string)'
has some invalid arguements.

[TestMethod()] 
public void SendEmailTest() 
{ 
    SESEmailProvider target = new SESEmailProvider(); 

    List<string> ToEmailAddresses = new List<string>() {"someone@gmail.com"}; 
    string FromEmailAddress = "no-reply@mydomain.com"; 
    string Subject = "Test"; 
    string EmailBody = "Hello world."; 
    string expected = string.Empty; 
    string actual; 
    actual = target.SendEmail(ToEmailAddresses, FromEmailAddress, Subject, EmailBody); 
    Assert.AreEqual(expected, actual); 
    Assert.Inconclusive("Verify the correctness of this test method."); 
} 

Try that. I have changed your variable from a string to a List<string> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM