Click to See Complete Forum and Search --> : How can I create objects during runtime?
autostrad
September 28th, 2009, 10:18 PM
Create object in runtime
I want to make a program which can allow the user to hire employees at regular bases. In other words, every time an employee is hired the program should be able to create an object by itself for that employee.
What I want is that; every time the user enters a new employee the program will create a new object for such employee.
The user can enter, for example, the name and number of a new employee and the program will create an object for such employee. The reason I want to do that is I do not want to create 50 objects and latter know that the user needs only 30 object or may be the user needs more than 50 object. At the end, as the program will be used by different users, every user can have exactly the number of objects needed.
darwen
October 1st, 2009, 05:57 PM
Huh ? How about :
public ref class Employee
{
public:
Employee(String ^name, int number)
: _name(name), _number(number)
{
}
property String ^ Name
{
String ^get() { return _name; }
}
property int Number
{
int get() { return _number; }
}
private:
String ^_name;
int _number;
} ;
void SomeOtherMethod()
{
Employee ^employee = gcnew Employee("fred jones", 100);
String ^name = employee->Name;
int number = employee->Number;
}
Can I suggest you learn C# instead ?
Darwen.
autostrad
October 1st, 2009, 07:19 PM
What you did is perfect. I can understand it. You have Fred # 100. The thing that I want is: I do not know how many employees there, their names or their numbers. I want to make a program for say a bank. When you do the program you do not know these things. These things will be done be the bank workers. To do that I can only do the following and it looks stupid. I am using a switch. You can see the objects there. I have only 9 objects. What I am going to do; if the company has 100 employees? The best thing to do is to make a program by which the user can decide how many objects he needs and what the name of these objects and their numbers.
Int32 temp = Convert::ToInt32(empNumBox->Text);
switch (temp)
{
case 1:
{
Time^ Employee1;
InBtnFncton(Employee1, “1Name.txt");
}
break;
case 2:
{
Time^ Employee2;
InBtnFncton(Employee2, "_2Name.txt");
}
break;
and so on till 9...............................
case 9:
{
Time^ Employee9;
InBtnFncton(Employee9, "_9Name.txt");
}
break;
default: MessageBox::Show(" This is an invalid employee number.\n Please try again! ",L" Error ");
}
}
lion.king
October 6th, 2009, 10:45 AM
Create object in runtime
I want to make a program which can allow the user to hire employees at regular bases. In other words, every time an employee is hired the program should be able to create an object by itself for that employee.
What I want is that; every time the user enters a new employee the program will create a new object for such employee.
The user can enter, for example, the name and number of a new employee and the program will create an object for such employee. The reason I want to do that is I do not want to create 50 objects and latter know that the user needs only 30 object or may be the user needs more than 50 object. At the end, as the program will be used by different users, every user can have exactly the number of objects needed.
i think u can write a function to create an one employ record object and returns that object, let it be employ_creator().
write a main function where u accept number of employees and call employ_creator that many times based on the input.
thanks
satya.
darwen
October 6th, 2009, 11:39 AM
You need a dynamically sized collection e.g. a List<Employee ^>.
Read up on collections - especially List.
Darwen.
autostrad
October 6th, 2009, 10:22 PM
It sounds good. Can you give a little bit of example?
darwen
October 7th, 2009, 03:14 AM
List<Employee ^> employees = gcnew List<Employee ^>;
employees->Add(gcnew Employee("fred", 100));
employees->Add(gcnew Employee("john", 101));
employees->Add(gcnew Employee("barbara", 102));
Darwen.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.