Array Question - Help

A

Ash

Hi,

I've created a class named Customers, and proved accessor and mutator
methods for it to manipulate two private member variables named firstname,
lastname.

After doing that, I created an Array of Customers with the following line:

Customers [] CustomerList = new Customer[5];

How do i go about initializing each customer and using the mutator methods
and accessor methods? The Methods are:

public void setNames(string firstname,string lastname);
public string getFirstname();
public string getLastname();

Will appreciate your help...

Thanks,

Ash
 
C

C# Learner

Ash said:
Hi,

I've created a class named Customers, and proved accessor and mutator
methods for it to manipulate two private member variables named firstname,
lastname.

After doing that, I created an Array of Customers with the following line:

Customers [] CustomerList = new Customer[5];

How do i go about initializing each customer and using the mutator methods
and accessor methods? The Methods are:

I believe this is what you're looking for:

1: Customer[] customerList = new Customer[5];
2: for (int i = 0; i < customerList.Length; ++i) {
3: customerList = new Customer();
4: }

In line 1, we declare an array which will hold references to 5 Customer
objects. The loop that follows creates the actual objects and stores
the references to them in the array.

<snip>
 
A

Ash

Hi,

Thank you for the assistance csharp. I want to specify the individual
elements of the array manually without the use of a loop, so that i can give
each element different argument text. I tried the following code, but it
gives me an error. I've got the error pasted below the code.

Customers CustomerList = new Customers[5];
public AddressBook()

{

InitializeComponent();

CustomerList[0]= new Customers("Residential","Jane Doe","123 Any
Street","Anytown","FL","32765");

CustomerList[1]= new Customers("Business","Mary Sue","543 Somewhere
Avenue","Sometown","ME","12345");

CustomerList[2]= new Customers("Public Health","Jim Bob","1600 Pennslivania
Avenue","Washingon DC","MD","99999");

CustomerList[3]=new
Customers("Unknown","Unknown","Unknown","Unknown","Unknown","12346");

CustomerList[4]= new Customers("Government","Harvey Hoosit","765 Anywhere
Avenue","SomeLand","CA","65432");


}

I tried the above code, but it gives me error C:\Documents and
Settings\User\My Documents\Visual Studio
Projects\Project3\CSharpProject4\AddressBook.cs(26): Cannot apply indexing
with [] to an expression of type 'CSharpProject4.Customers'

Another things I dont know is, Lets say all the members have been
initialized and assigned the values. How do i access my class public
interface methods, for example, is this code alright:

CustomerList[1].getFullname() ??? I want to access the full name variable
via the getFullName() accessor method.

Thanks,

Ash



C# Learner said:
Ash said:
Hi,

I've created a class named Customers, and proved accessor and mutator
methods for it to manipulate two private member variables named firstname,
lastname.

After doing that, I created an Array of Customers with the following line:

Customers [] CustomerList = new Customer[5];

How do i go about initializing each customer and using the mutator methods
and accessor methods? The Methods are:

I believe this is what you're looking for:

1: Customer[] customerList = new Customer[5];
2: for (int i = 0; i < customerList.Length; ++i) {
3: customerList = new Customer();
4: }

In line 1, we declare an array which will hold references to 5 Customer
objects. The loop that follows creates the actual objects and stores
the references to them in the array.

<snip>
 
J

Jon Skeet [C# MVP]

Ash said:
Thank you for the assistance csharp. I want to specify the individual
elements of the array manually without the use of a loop, so that i can give
each element different argument text. I tried the following code, but it
gives me an error. I've got the error pasted below the code.

Customers CustomerList = new Customers[5];

That's the problem. You need

Customers[] CustomerList = new Customers[5];

instead.
 
C

C# Learner

Ash said:
[...]
How do i access my class public
interface methods, for example, is this code alright:

CustomerList[1].getFullname() ???
[...]

Yes, that should work. Note that *properties* would be ideal for this
situation.

i.e.:

public class Customers // side note: shouldn't this be called Customer?
{
private string fullName;
// ...
public string FullName // this is a property
{
get {
return fullName;
} set {
fullName = value;
}
}
}

Also note that you might run into comprehension difficulties when using
initial upper-case characters for local variable names.

e.g.:

CustomerList // is this a class name or a local variable name?

Further, you might make a class called "CustomerList". In this case,
what will the variable name be?

The common standard for C# is to use lower-case initial characters here.

e.g.:

CustomerList customerList;

Now it's easy to tell that CustomerList is the class name and
customerList is the local variable name.
 
A

Ash

Hi,

I'm trying to write onto an instance of a class, that is, update the data, here is the implementation code for it:

public string fullName
{
get
{
return FullName;
}

set
{
this.fullName=value;
}

}


The following is the code i use to call the set method for the class:

customers[1].fullName=textBox2.Text;

When i do this, it gives me an error "An unhandled exception of type 'System.StackOverflowException' occurred in Customers.exe"

Any Idea what wrong i am doing?? I want to be able to update the details of that particular customer in the array.

Thanks,

Ash




C# Learner said:
Ash said:
[...]
How do i access my class public
interface methods, for example, is this code alright:

CustomerList[1].getFullname() ???
[...]

Yes, that should work. Note that *properties* would be ideal for this
situation.

i.e.:

public class Customers // side note: shouldn't this be called Customer?
{
private string fullName;
// ...
public string FullName // this is a property
{
get {
return fullName;
} set {
fullName = value;
}
}
}

Also note that you might run into comprehension difficulties when using
initial upper-case characters for local variable names.

e.g.:

CustomerList // is this a class name or a local variable name?

Further, you might make a class called "CustomerList". In this case,
what will the variable name be?

The common standard for C# is to use lower-case initial characters here.

e.g.:

CustomerList customerList;

Now it's easy to tell that CustomerList is the class name and
customerList is the local variable name.
 
A

Adam W Root

You have problems with casing. You are creating a stack overflow by recursion:

+-->public string fullName {
| get
| {
| return FullName;
| }
| set {
+----- this.fullName=value;
}
}

Assuming your private member is called "FullName", try this:

public string fullName
{
get
{
return FullName;
}

set
{
this.FullName=value; // notice this is no longer fullName, which caused recursive calls back to this set accessor
}

}

Adam

Hi,

I'm trying to write onto an instance of a class, that is, update the data, here is the implementation code for it:

public string fullName
{
get
{
return FullName;
}

set
{
this.fullName=value;
}

}


The following is the code i use to call the set method for the class:

customers[1].fullName=textBox2.Text;

When i do this, it gives me an error "An unhandled exception of type 'System.StackOverflowException' occurred in Customers.exe"

Any Idea what wrong i am doing?? I want to be able to update the details of that particular customer in the array.

Thanks,

Ash




C# Learner said:
Ash said:
[...]
How do i access my class public
interface methods, for example, is this code alright:

CustomerList[1].getFullname() ???
[...]

Yes, that should work. Note that *properties* would be ideal for this
situation.

i.e.:

public class Customers // side note: shouldn't this be called Customer?
{
private string fullName;
// ...
public string FullName // this is a property
{
get {
return fullName;
} set {
fullName = value;
}
}
}

Also note that you might run into comprehension difficulties when using
initial upper-case characters for local variable names.

e.g.:

CustomerList // is this a class name or a local variable name?

Further, you might make a class called "CustomerList". In this case,
what will the variable name be?

The common standard for C# is to use lower-case initial characters here.

e.g.:

CustomerList customerList;

Now it's easy to tell that CustomerList is the class name and
customerList is the local variable name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top