Click here to Skip to main content
14,336,226 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am working on C# windows form

Here is my problem:

Suppose I have a simple class:

Class Animal
{
int height;
DateTime date_of_birth;
float age;
int max_age;
}

And i have the following data in a file:


----------------------
for first animal
10
1997
23.4
12

for second animal
12
2017
33.9
21

and son on...
----------------------

My problem is:

I want to instantiate my class more than five times and set the attributes from the file shown above.

Is manually creating each class and setting the attribute one by one the right way ?

OR there is any other efficient way to do this . i.e. avoiding hard coding.


Any kind of suggestions would be apreciated
Posted
Updated 28-Jan-15 5:38am
v2
Comments
Richard Deeming 28-Jan-15 11:42am
   
How is the file formatted? Is it an XML file, a JSON file, or just a text file with a list of property values?
Sergey Alexandrovich Kryukov 28-Jan-15 11:46am
   
What do you call "attribute" here? .NET attributes is not what you think, their instances are not modifiable.
—SA
ZurdoDev 28-Jan-15 11:51am
   
You can overload the constructor for your class and make it accept all 5 attributes and then inside the constructor you set each property. Then to create 5 instances you just need 5 lines of code.
Sergey Alexandrovich Kryukov 28-Jan-15 11:54am
   
You should start learning programming in general and C# and .NET in particular, from the very, very beginning. You have no clue of the basics. You use the term "attribute" (a very special thing in .NET) but talking about fields. You don't have access to private fields. More importantly, your "I want to instantiate my class more than five times" and "is manually creating each class..?" contradict to each other and suggest you don't understand what are instances and what are types, their roles. Also, you need to learn static vs. instance members, as well as… pretty much everything.
—SA
Ramza360 28-Jan-15 12:10pm
   
I must agree here actually..
Rate this:
Please Sign up or sign in to vote.

Solution 1

So you need to create an Animal object for each animal you have. Depending on the answer to the comment for file formatting, you can easily instantiate a class simply using a constructor.

class Animal
{
    int height;
    DateTime date_of_birth;
    float age;
    int max_age;

    public Animal(int height, DateTime date_of_birth, float age, int max_age) {
        this.height = height;
        this.date_of_birth = dob;
        this.age = age;
        this.max_age = max_age;
    }
}


Then fill in the values with your data, which you would do from whatever file you have (i.e. XmlDocument, TextReader). Should be able to read in each node or line and through it into a loop to create animal objects as needed.
Animal first = new Animal(5, new DateTime(), 5, 10);
Animal second = new Animal(2, new DateTime(), 3, 11);
   
Comments
Sergey Alexandrovich Kryukov 28-Jan-15 12:34pm
   
5ed. (If only this along could help.)
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

The are properties, not attributes. In .NET, "attributes" means something completely different. Use the correct terminology, otherwise you're going to confuse people trying to help you.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Create properties of the class and a parameterized constructor which sets these properties. While instantiating the class call the constructor and pass the values in it to initialize the properties.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100