I have this...

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter name of DVD");
            object objectname = Console.ReadLine();

            Program2 myob = new Program2(objectname);

        }
        public int number = 0;
        public string myString = "DVD";
        public double myDouble = 2.0;
    }
    class Program2
    {
        public Program2(object objectname)
        {
            objectname = new Program();
        }
    }

I want an object to be made with the name of the title of the DVD that the user inputs, so for example I type in "the matrix" I want an object named this to be created that will hold it's different attributes..

How would I go about this?

Recommended Answers

All 3 Replies

Without performing some very complicated code using Emit you can't. But what you can do is use a Dictionary<Tkey,Tvalue> you can do something more useful. For example let's create a DVD class:

class DVD {
    public String Title { get; private set; }
    public int RunTime { get; private set; }
    public int Region { get; private set; }

    public DVD (String title, int runtime, int region) {
        Title = title;
        RunTime = runtime;
        Region = region;
    }
    // more attributes here
}

Now you'd create code to gather DVD information and save it in a Dictionary of DVDs:

Dictionary<String, DVD> myCatalog = new Dictionary<String, DVD>();

// ask user for Title, runtime and region. Not going to write this code, you know
// how to do this already. Values will be stored in 'ti', 'rt' and 'rg' for 
// this example.
    ... code goes here
// Now that we have this information, create a DVD object and place it in the dictionary.

DVD myDVD = new DVD (ti, rt, rg);
myCatalog.Add(ti, myDVD);

... more code here for whatever purpose you need

Now you'd have an object, myCatalog, that contains all the DVD information you need. To retrieve a DVD so you can display information (or whatever you want) you just need to know the title. Let's say we have "Iron Man" in there:

DVD selectedDVD = myCatalog["Iron Man"];
commented: Great answer! +1

Thank you Momerath! Always give clear simple answers! Thanks

So the other way to store information aside from this would be to use a database?

And would it be a better way?

If you wanted to store the information outside of the instance of the program, I would recommend using a database. Once the program ends with the entries in a dictionary, insert the new items into a database table to store it outside of the program, then you can just call that data back whenever you need it. Or you can just use the database directly to retrieve, insert, update, delete any movies and related information about it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.