1

Topic: What data type to use?

The data type on similarity here the such is necessary to me:

Myclass my
{
Myclass1 left;
Myclass1 right
}

Also what was it is possible to do here so: smile

foreach (Myclass1 i in my)
{
//and here i refers on left and right :)
}
//And here so 
my.left
my.right

I hope clearly explained smile

2

Re: What data type to use?

using System;
using System. Collections. Generic;
using System. Text;
namespace TestCS {
public class MyClass1 {
public MyClass1 (string name) {
Name_ = name;
}
public string Name {
get {
return Name _;
}
}
private string Name _;
}
public class MyClass: IEnumerable <MyClass1> {
public MyClass1 Left {
get {
return Left _;
}
set {
Left_ = value;
}
}
public MyClass1 Right {
get {
return Right _;
}
set {
Right_ = value;
}
}
private MyClass1 Left _;
private MyClass1 Right _;
#region IEnumerable <MyClass1> Members
public IEnumerator <MyClass1> GetEnumerator () {
return new MyClassEnumerator (this);
}
#endregion
#region IEnumerable Members
System. Collections. IEnumerator System.Collections.IEnumerable.GetEnumerator () {
return new MyClassEnumerator (this);
}
#endregion
}
public class MyClassEnumerator: IEnumerator <MyClass1> {
public MyClassEnumerator (MyClass container) {
Position_ =-1;
Container_ = container;
}
#region IEnumerator <MyClass1> Members
public MyClass1 Current {
get {
switch (Position_) {
case 0:
return Container_. Left;
case 1:
return Container_. Right;
default:
throw new ArgumentOutOfRangeException ();
}
}
}
#endregion
#region IDisposable Members
public void Dispose () {
}
#endregion
#region IEnumerator Members
object System. Collections. IEnumerator. Current {
get {
return Current;
}
}
public bool MoveNext () {
Position _ ++;
return Position_ <2;
}
public void Reset () {
Position_ =-1;
}
#endregion
private int Position _;
private MyClass Container _;
}
class Program {
static void Main (string [] args) {
MyClass my = new MyClass ();
my. Left = new MyClass1 ("left");
my. Right = new MyClass1 ("right");
foreach (MyClass1 i in my) {
Console. WriteLine I. Name);
}
Console. WriteLine (my. Left. Name);
Console. WriteLine (my. Right. Name);
}
}
}

3

Re: What data type to use?

adLucem, 5/17/2007, 11:20, post1136120 wrote:

switch (Position_) {
case 0:
return Container_. Left;
case 1:
return Container_. Right;

That that to me not hunting so to do smile
And if I use more than two classes? Any standard means are? smile

4

Re: What data type to use?

Elfet, 5/17/2007, 08:36, post1136137 wrote:

Any standard means are?

If standard means of dynamic determination of all dependences of a specific class without presence of convergence on it at a compilation stage certainly is - Reflection mean.

Elfet, 5/17/2007, 08:36, post1136137 wrote:

And if I use more than two classes

Write other iterator.

5

Re: What data type to use?

Reflection? smile

6

Re: What data type to use?

using System;
using System. Diagnostics;
using System. Collections. Generic;
using System. Text;
using System. Reflection;
namespace TestCS {
public class MyClass1 {
public MyClass1 (string name) {
Name_ = name;
}
public string Name {
get {
return Name _;
}
}
private string Name _;
}
public class MyClass: IEnumerable <MyClass1> {
public MyClass1 Left {
get {
return Left _;
}
set {
Left_ = value;
}
}
public MyClass1 Right {
get {
return Right _;
}
set {
Right_ = value;
}
}
private MyClass1 Left _;
private MyClass1 Right _;
#region IEnumerable <MyClass1> Members
public IEnumerator <MyClass1> GetEnumerator () {
return new MyClassEnumerator (this);
}
#endregion
#region IEnumerable Members
System. Collections. IEnumerator System.Collections.IEnumerable.GetEnumerator () {
return new MyClassEnumerator (this);
}
#endregion
}
public class MyClassEnumerator: IEnumerator <MyClass1> {
public MyClassEnumerator (MyClass container) {
Reset ();
Debug. Assert (! ReferenceEquals (container, null));
Container_ = container;
Type containerType = container. GetType ();
FieldInfo [] fields = containerType. GetFields (
BindingFlags. NonPublic | BindingFlags. Instance);
Fields_ = new List <FieldInfo> ();
foreach (FieldInfo info in fields) {
if (info. FieldType. Equals (typeof (MyClass1))) {
Fields_. Add (info);
}
}
}
#region IEnumerator <MyClass1> Members
public MyClass1 Current {
get {
return Container_. GetType ().InvokeMember (Fields_ [Position_].Name;
BindingFlags. GetField | BindingFlags. NonPublic | BindingFlags. Instance;
Type. DefaultBinder, Container _;
null) as MyClass1;
//switch (Position_) {
//case 0:
//return Container_. Left;
//case 1:
//return Container_. Right;
//default:
//throw new ArgumentOutOfRangeException ();
//}
}
}
#endregion
#region IDisposable Members
public void Dispose () {
}
#endregion
#region IEnumerator Members
object System. Collections. IEnumerator. Current {
get {
return Current;
}
}
public bool MoveNext () {
return ++ Position_ <Fields_. Count;
}
public void Reset () {
Position_ =-1;
}
#endregion
private int Position _;
private MyClass Container _;
private List <FieldInfo> Fields _;
}
class Program {
static void Main (string [] args) {
MyClass my = new MyClass ();
my. Left = new MyClass1 ("left");
my. Right = new MyClass1 ("right");
foreach (MyClass1 i in my) {
Console. WriteLine I. Name);
}
Console. WriteLine (my. Left. Name);
Console. WriteLine (my. Right. Name);
}
}
}