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

  • SEARCH
  • Home
  • 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 8609921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:59:46+00:00 2026-06-12T03:59:46+00:00

I am still a beginner developer – so forgive me if I get this

  • 0

I am still a beginner developer – so forgive me if I get this question horibly wrong, but I have a class that builds a string based on the properties of another public struct. This used to work fine when the struct was named, but I wanted to allow this class to do the same with any kind of struct so I changed to using objects. This is the methoed I use to loop through the properties and construct the string – this is in a public class called constructors which I then call (without creating an instance of constructors) from the rest of my code.

    public string MyConstructor(object TheObject)
    {
        string S = "";

        Type t = TheObject.GetType();
        PropertyInfo[] PI = t.GetProperties();

        Constructors Cons = new Constructors();

        foreach (PropertyInfo info in PI)
        {
            S = MyConstructor(S, info, info.GetValue(TheObject, null););
        }

        return S;
    }

My problem is that it does not want to get out of the foreach loop. When I replaced it with

        for (int i = 0; i < PI.Count(); i++)
        {
            S = MyConstructor(S, PI[i], PI[i].GetValue(TheObject, null));
        }

and ran it through the debugger -> after each loop i goes 0, 1, 0, 1, 0, 1… MyConstructor has 2 overloads… the one above and another with (string, PropertyInfo, object). But even if I change the name of the second method to MyPropertyConstructor the same happens. At the moment the code is called from a form with no Threads so there isn’t any other threads that could in my mind interfere. Also for loop 0 and 1 the method returns empty strings “”. So how do I get out of this loop?

Here is the the rest of the code

    public static string MyConstructor(string CompiledString, PropertyInfo PropertyToAdd, object ThisValue)
    {
        string s = "";
        //object ThisValue = PropertyToAdd.GetValue(PropertyToAdd, null);

        //See if there is something to work with
        if(PropertyToAdd != null)
        {
            //Remove items which has been set not to record
            if (PropertyToAdd.GetCustomAttributes(typeof(DontRecord), true).Length > 0)
            {
                if(((DontRecord)PropertyToAdd.GetCustomAttributes(typeof(DontRecord), true)[0]).Record)
                {
                    return CompiledString;
                }
            }

            //see if it is the first time using the compile string
            if (CompiledString != "")
            {
                s += ";";
            }

            //For Testing
            int testint = 0;

            //only record items that where default value is different to their value
            Object[] Attr = PropertyToAdd.GetCustomAttributes(typeof(DefaultValueAttribute), true);

            //see if there is a default value set
            if (Attr.Length > 0)
            {
                //Get Constructorname value
                Object[] constructorname = PropertyToAdd.GetCustomAttributes(typeof(ConstructorName), true);

                if (constructorname.Length > 0)
                {
                    s += ((ConstructorName)constructorname[0]).Name.ToString() + "=";



                    //If value is a string
                    if (PropertyToAdd.PropertyType == typeof(string))
                    {
                        if (Convert.ToString(((DefaultValueAttribute)Attr[0]).Value) != (string)ThisValue) { s += ThisValue; } else { s = ""; }
                    }

                    //Incase value is an int
                    else if (PropertyToAdd.PropertyType == typeof(int) && int.TryParse(ThisValue.ToString(), out testint))
                    {
                        if (Convert.ToInt32(((DefaultValueAttribute)Attr[0]).Value) != (int)ThisValue) { s += Convert.ToString((int)ThisValue); } else { s = ""; }
                    }

                    //Incase value is a bool
                    else if (PropertyToAdd.PropertyType == typeof(bool))
                    {
                        if (Convert.ToBoolean(((DefaultValueAttribute)Attr[0]).Value) != (bool)ThisValue)
                        {
                            if ((bool)ThisValue) { s += "True"; } else { s += "False"; }
                        }
                        else { s = ""; }
                    }
                    else
                    {
                        s = "";
                    }
                }
                else
                {
                    //There is no ConstructorName so therefore cannot create
                    return CompiledString;
                }
            }
            else
            {
                return CompiledString;
            }
        }

        return CompiledString += s;
    }

}

Here is a part of the type of objects passed initially to MyConstructor the public struct example

public struct ODBCDataString
{
#region Variables
#region General Variables
static string _connection = “”;
static string _saveString = “”;
static string _unsaveString = “”;
#endregion General Variables

        #region Security Variables
            static string _userID = "";
            static string _password = "";
        #endregion Security Variables

        #region Source Variables
            static string _dsn = "";
            static string _driver = "";
        #endregion Source Variables
    #endregion Variables

    #region Properties
        #region General
            [Browsable(false)]
            public string ConnectionString { get { return _connection; } set { _connection = value; } }

            [Browsable(false)]
            public string SaveString 
            {
                get 
                {
                    Constructors Cons = new Constructors();
                    string MyS = Cons.MyConstructor((object)this);
                    return MyS; 
                } 
            } 

            [Browsable(false)]
            public string UnsaveString 
            { 
                get 
                {
                    Constructors Cons = new Constructors();
                    string MyS = Cons.MyConstructor((object)this);
                    //string MyS = Constructors.MyConstructor((object)this);
                    if (_password != "")
                    {
                        MyS += ";Password=" + _password;
                    }

                    return MyS; 
                } 
            }
        #endregion General

        #region Source
            [DisplayName("DSN")]
            [Description("The DSN to use when connecting to the Data Source")]
            [DefaultValue("")]
            [Category("Source")]
            [ConstructorName("Dsn")]
            public string DSN { get { return _dsn; } set { _dsn = value; } }

… This is basically just a brief overview of the type of structs / objects that is passed

  • 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-06-12T03:59:47+00:00Added an answer on June 12, 2026 at 3:59 am

    Ok sorry for wasting time here, but I found the problem. It is as HOBO suggested. one of my properties also called the same method (actually it was the second property of the exact class I set as the example above. I commented that one out and code was working. After an entire day spent – I feel like an idiot and wasted all you guys’ time… THANK You SO SO Much for all your help 🙂 I almost gave up on developing all together 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been wandering about this for some time now. I'm still a beginner,
hello i'm a beginner java developer and this is one question in a list
I'm very much still a beginner at programming but I have come across the
I'm still a beginner and I have been trying to solve this problem by
I am still a beginner to java and I have a question on an
I am still a beginner but I want to write a character-recognition-program. This program
I know how to use Xcode and everything but this is a beginner question.
First of all i want to say that I'm still a beginner in ASP.NET
I am a beginner in GAE and still evaluating if I should use this
I have only started learning python recently. I would still be considered a beginner.

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.