Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Dynamically declared
Message
From
21/06/2013 16:14:05
 
 
To
21/06/2013 13:12:12
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01576814
Message ID:
01576865
Views:
39
>>>Hi,
>>>
>>>I have code like this:
>>>
>>>
            NetworkCredential cred = new NetworkCredential(smtpUser, smtpPassword);
>>>            NetworkCredential cred1 = new NetworkCredential(justUser + "1" + justCredDomain, smtpPassword);
>>>            NetworkCredential cred2 = new NetworkCredential(justUser + "2" + justCredDomain, smtpPassword);
>>>            NetworkCredential cred3 = new NetworkCredential(justUser + "3" + justCredDomain, smtpPassword);
>>>            NetworkCredential cred4 = new NetworkCredential(justUser + "4" + justCredDomain, smtpPassword);
>>>
>>>
>>>How can I make this flexible where I can switch from 5 variables to 7 or whatever amount I want?
>>>
>>>I then need to use these in a loop where I send emails from different email address each pass through the loop. For example, first pass uses cred, second cred1, 3rd Cred2, etc and then when cred4 is reached the next loop would be cred again.
>>>
>>>I suppose some type of collection? Can anyone give me an example showing how to declare these and how to use them in later code? I am using C#.
>>>
>>>Thanks
>>
>>Try this:
>>
>>// Set CredentialCount to the number of credentials you want
>>var credentials = new List<NetworkCredential>(CredentialCount + 1);
>>credentials.Add(new NetworkCredential(smtpUser, smtpPassword));
>>for(int index = 1; index <= CredentialCount; index++)
>>	credentials.Add(new NetworkCredential(justUser + index.ToString() + justCredDomain, smtpPassword));
>>
>>foreach(var credential in credentials)
>>{
>>	// Send emails here.
>>}
>>
>
>Thanks. To turn the logic around a bit. How would I refer to the nth credential in the list of credentials?
>
>credentials[n]
>
>?

You could refer to it with the indexer, like you posted, but with the foreach loop it would put each one into the credential variable for that iteration.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform