Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5951311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:31:07+00:00 2026-05-22T17:31:07+00:00

How loop sub classes properties from the main class? public class GroupA { public

  • 0

How loop sub classes properties from the main class?

public class GroupA
{
  public string FullName = "", BirthDay = "";
}
public class GroupB
{
  public string Email = "";
}
public class GroupC
{
  public string Phone;
}

public class MainGroup
{
  public GroupA GroupA;
  public GroupB GroupB;
  public GroupC GroupC;
}


    protected void Page_Load(object sender, EventArgs e)
    {

      GroupA  NewGroupA = new GroupA();
              NewGroupA.FullName="TEST MASTER";
              NewGroupA.BirthDay="02/20/1984";
      GroupB  NewGroupB = new GroupB();
              NewGroupB.Email="noreply@test.com"; 
      GroupC  NewGroupC=new GroupC();
              NewGroupC.Phone="555 123 4567";

      //Assign new class instances to the main class
      MainGroup NewMainGroup= new MainGroup();
                NewMainGroup.GroupA=NewGroupA;
                NewMainGroup.GroupB=NewGroupB;                 
                NewMainGroup.GroupC=NewGroupC;

      //Loop through the main class

      foreach (var Group in typeof(MainGroup).GetFields())
      {
        Response.Write("<BR>MainGroupName=  " + Group.Name + " Value=  " + Group.GetValue(NewMainGroup).ToString());
        //PROBLEM IS HERE. Can't loop through the subclass We need to display the GroupA, GroupB, GroupC below.
        foreach (var SubGroup in Group)
        {
          Response.Write("<BR>");
        }


      }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:31:07+00:00Added an answer on May 22, 2026 at 5:31 pm

    What you should to is store a reference to the group-variable (not just the type). So, if you take your code and do something like this:

    foreach (var GroupType in typeof(MainGroup).GetFields())
    {
        object Group = GroupType.GetValue(NewMainGroup);
        Response.Write("<BR>MainGroupName=  " + GroupType.Name + " Value=  " + Group.ToString());
        foreach(var SubGroupType in Group.GetType().GetFields()) {
            object SubGroup = SubGroupType.GetValue(Group);
            Response.Write("<BR>SubGroupName= " + SubGroupType.Name + " Value= " + SubGroup.ToString());
        }
    }
    

    Haven’t tested it, but I think that should about work. At least get you started.

    Oh, and by the way, I think I have a better method for you, try this one:

    public Dictionary<string, object> GetObjectFields(object obj) {
        var dict = new Dictionary<string, object>();
        var t = obj.GetType();
        foreach(var f in t.GetFields()) {
            dict[f.Name] = f.GetValue(obj);
        }
        return dict;
    }
    

    Then you can simply do this:

    var groupVars = GetObjectFields(NewMainGroup);
    foreach(var gv in groupVars) {
        Response.Write(<BR>MainGroupName=  " + gv.Key + " Value=  " + gv.Value.ToString());
        var subGroupVars = GetObjectFields(gv.Value);
        foreach(var sgv in subGroupVars) {
            Response.Write(<BR>SubGroupName=  " + sgv.Key + " Value=  " + sgv.Value.ToString());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a super-class named ClassA and two sub-classes Class1 and Class2. I have
I have an abstract class and two sub classes that extend it. I have
How do I loop into all the resources in the resourcemanager? Ie: foreach (string
I know I can loop over the string or build a regex or invert
I've got an inheritance chain of three classes: Baz inherits from Bar inherits from
I have one Private sub that runs in a loop. I want the sub
Is there a way to start same Thread form foreach loop Sub For Each
How can I replace a sub string with some other string for all items
I would like some feedback regarding the IService class listed below. From what I
Loop unwinding is a common way to help the compiler to optimize performance. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.