Skip to content

Dynamically creating objects

Morgan Hein

Asked Apr 24, 2008, 4:03 PM EDT1 answer
Share
LinkedInX
I know this is something that is common, but I just can't find a suitable answer anywhere on the internet. The keywords that I think pertain to my problem are not correct, so I can't figure out what to search for! Even someone linking me to a page about this would be helpful!

Let's say my program takes a text file (a story) as input, and I want to create an instance of the custom class "Person" for every person named in the text file. How do I do this? How can I dynamically create the objects, without knowing what the names are beforehand?

How can I later call those objects and their functions/variables w/o knowing the name of the object(s)?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    Create a new text file and fill it as bellow:

    ; Person first name

    FirstName =  djfdhfj

    ; Person last name

    LastName = jhjhjhjhj

    ; Person region

    Region = jhjhjhjhj

    ; Person education

    Level = jhjhjhhj

    ; Person profession

    Profession = jhjhjhjhjh

    use this code to generate a class from this text file
    C#
    using Res = System.Resources.Tools;
    using Microsoft.CSharp;
    /// <summary>
    ///
    /// </summary>
    /// <param name="errors">Array that contains
    unmatchables elements</param>
    /// <param
    name="MaximumDisplayed">The maximum dimension of the errors array</param>
    private static
    void GenerateWrapper(string[]errors)
    {
       
    try
       
    {
            Res.StronglyTypedRecourceBuilder(@"C:\myResourcesTextFiles.txt", "ResourcesClass", "Resources", new CSharpCodeProvider(), false,
    out errors);
       
    }
    
    
    /* This code is done by bejaoui bechir the exception will be thrown when one
    among elements in the
         * resource file can't be wrapped for a
    given problem during
         * the generation process
         */
       
    catch (StrongTypingException
    caught)
       
    {
            MessageBox.Show("Source: " + caught.Source + "Message: " + caught.Message);
        }
    }
    look at your project you'll find a new generated class from this text
    0