twilightown

i need to create an array of objects to act as a simple database. So the task is to:
Create an array of Student objects called
Students that can store:
Name, Course and Email of up to 50 students.

i am having a little bit of trouble though with the actual array.

this is what I have done so far:

public class student
{

student[] students = new student[50]; //an array with 50 elements
{
new string name[50];
new string email[50];
new string course[50];

}
}

the details that the user inputs will be via 2 text box and one combo box.

I think my syntax is badly wrong. could anyone point me in the right direction, thanks.
I am also using MS visual studio .net 2003




Re: Visual C# General creating arrays

Bruno Kova?i?

!

i think this is what you're looking for:

public class student
{
public string[] name, email, course;
public student ()
{
name = new string[50];
email = new string[50];
course = new string[50];
}
}

that's class, and this is how you should use it:

student[] students = new student[50];

and for example:
students[k].name[j] = something;






Re: Visual C# General creating arrays

mr_super_genius

What you are trying to do can be accomplished this way:

Student class:

public class Student

{

public string name = "";

public string email = "";

public string course = "";

public Student()

{

}

}

How to use it as array:

Student[] st = new Student[50];

st[0] = new Student();

st[0].name="TEST";

st[0].email = "TEST@TEST.COM";

st[0].course="TEST";

Ofcourse you can put that in a loop to ease things out

Hope it helps!!





Re: Visual C# General creating arrays

Kea

Your database approach calls for more flexibility than the array allows for, and it seems you don't know about the arraylist. It's inconvienent that you still use .NET 1.1, as generics in .NET 2.0 would provide substantial benefits for such and any future requirement.
Anyway, a class for holding student info could look something like this:

public class Student
{
string name;
string email;
string course;

// Default constructor:
public Student() { }

// Custom constructor:
public Student(string name, string email, string course)
{
this.name = name;
this.email = email;
this.course = course;
}

public string Name
{
get { return name; }
set { name = value; }
}

public string Email
{
get { return email; }
set { email = value; }
}

public string Course
{
get { return course}
set { course = value; }
}
}

For the "database", instead of using an array I would use an ArrayList (contained in the System.Collections namespace). However, this is not parameterized, meaning that you can store any type of object in it, which can, and will, lead to disastrous bugs eventually. Hence, I would derive from the class and override some of the methods to make absolutely sure you cannot store anything other than student object in it:

public class StudentList : ArrayList
{

public
StudentList() : base() { }

public
StudentList(int capacity) : base(capacity) { }

public
StudentList(ICollection c)
: base(c)
{
// This is really bad because we don't get notified before after the elements in the collection are added to the list, but we get the exception anyway:
foreach (object o in c)
{
if (!(o is Student))
throw new ArgumentException("One or more of the objects are incompatible.");
}
}

public override int Add(object value)
{
if (value is Student)
return base.Add(value);
else
throw new ArgumentException("The object is incompatible.");
}

public override void AddRange(ICollection c)
{
foreach (object o in c)
{
if (!(o is Student))
throw new ArgumentException("One or more of the objects are incompatible.");
}
base.AddRange(c);
}

public override Student this[int index]
{
get
{
return (Student)(base[index]);
}
set
{
base[index] = value;
}
}
}


Of course, more methods could be overriden to ensure better type safety, but this is the least you must do. Whenever you want a list that can store up to 50 students without resizing its internal array, simply write:

StudentList sl = new StudentList(50);

To access items in the list, just do as with any array, however you'll have to cast to Student:

Student s = sl[4];

As you see, it gets pretty messed up without generics, and there's a lot more traps to be aware of, but I think this could help you for a start.




Re: Visual C# General creating arrays

Kea

mr_super_genius, using initializers where you just initialize to the default values is completely redundant as the CLR does this for you, and public fields is bad coding practice and should not be encouraged, even for beginners.




Re: Visual C# General creating arrays

twilightown

thank you all for your help, I didn't think I would get such a response, you've all been very helpful. Cheers





Re: Visual C# General creating arrays

Kea

I don't know how, but I ended up pasting some of the wrong code I did in VC#, thus the StrangeList which should have been called StudentList, however I've fixed that now (and the tabbing became broken). How stupid of me. Hope it helps.