Reputation: 1981
Ok, i have a class which is as follows:
public class MT101
{
// Sequence A
public string tag20 { get; set; } // 16x
public string tag21R { get; set; } // 16x
public string tag28D { get; set; } // 16x
public List<String> tag50; // Option F, G, H, C, or L
}
i then have a method, which make a new instance of said class as follows:
public class CheckMessage
{
private List<MT101> Message101 = new List<MT101>();
public List<MT101> CheckMt101Message(string[] messageBody)
{
MT101 buildMessage = new MT101();
.
.
. perform all the neccessary logic to add into buildMessage and then return the
. Message101 object
Message101.Add(buildMessage);
return Message101;
}
}
I call the Method from another class as such:
GlobalClasses.CheckMessage gm = new GlobalClasses.CheckMessage();
and pass in the variables as such:
string[] _block4 = Regex.Split(trimmed, @"\r\n");
gm.CheckMt101Message(_block4);
foreach (GlobalClasses.CheckMessage item in gm)
{ }
In visual studio, i get the following errors when compiled:
Error 1 GlobalClasses.MT101.System.Collections.IEnumerable.GetEnumerator()': containing type does not implement interface 'System.Collections.IEnumerable'
Error 2 foreach statement cannot operate on variables of type 'GlobalClasses.CheckMessage' because 'SASMI.GlobalClasses.CheckMessage' does not contain a public definition for 'GetEnumerator'
I've googled and found reference to setting IEnumerable to the class, so 'ive tried this:
public class Mt101 : IEnumberable
same message being reported.
Upvotes: 0
Views: 253
Reputation: 216353
If you want to iterate on the internal List using the reference to the class CheckMessage then you need to implement a function that returns an IEnumerator like this
in CheckMessage class
public IEnumerator<MT101> GetEnumerator()
{
foreach (MT101 m in this.Message101)
{
yield return m;
}
}
in your code
foreach(MT101 m in gm)
{
.....
}
Of course you could execute the foreach also on the return value of CheckMt101Message but the yield keyword allows a couple of advantages. You need to have just the reference to the CheckMessage class, you don't need to make your internal list public, you could execute the loop at any moment using only the CheckMessage reference and you could add custom code before and after the yeld.
Upvotes: 1
Reputation: 29640
I guess you want to iterate over the MT101
objects in the Message101
field in the class. The easiest way to accomplish this, is to make Message101
public, e.g.:
public class CheckMessage
{
private List<MT101> message101 = new List<MT101>();
public List<MT101> Messge101 { get { return message101; } }
public List<MT101> CheckMt101Message(string[] messageBody)
{
MT101 buildMessage = new MT101();
.
.
. perform all the neccessary logic to add into buildMessage and then return the
. Message101 object
Message101.Add(buildMessage);
return Message101;
}
}
And then you can iterate over these objects:
foreach (MT101 item in gm.Message101)
{
// ...
}
Upvotes: 2
Reputation: 29
this is one of the procedures to iterate through the loop
EMP oeemp= new EMP();
List<EMP> tr= new List<EMP>();
foreach(var t in tr)
{
}
Upvotes: 1
Reputation: 68440
You have to do
foreach (MT101 item in gm.CheckMt101Message(_block4)) { ... }
I'm afraid you have a big confusion so I recommend you to check yourself about why your code is not working, this would be much more helpful for you than just copy paste any of provided solutions.
Upvotes: 0
Reputation: 172468
You are ignoring the return value of CheckMt101Message
and instead try to iterate on the class itself. That won't work. Save the return value and iterate over it:
var messages = gm.CheckMt101Message(_block4);
foreach (Mt101 item in messages)
{
...
}
Upvotes: 2