Jump to content

C# array not quite working right

Go to solution Solved by RononDex,

 

You're replacing your first object with a different object.

clients[ccount] = new client { clientid = ccount}; // creating new object, setting the idclients[ccount] = new client {Name = name}; // creating another new, different object, and setting the name (id isn't set)

The ID is always zero because that is the default value of an int property. When you declare your second object, it wont have a value assigned to the id. To fix it, assign all properties at once or create the object before you add it to the array.

clients[ccount] = new client {clientid = ccount, Name = name};

I replaced ccount with i (the for loop counter) and missed to replace some ccount calls. Edited and should be fixed.

 

EDIT: Ah sry, I thought you were talking to me lol

clients class

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Squash{    class client    {        public int clientid { get; set; }        public string Name { get; set; }        public int limit { get; set; }        }}

The array Part

int ccount = 0; var clients = new client[100];Console.WriteLine("Enter client name");                        string name = Console.ReadLine();                        clients[ccount] = new client { clientid = ccount};                        clients[ccount] = new client {Name = name};                        Console.WriteLine("ID :" + clients[ccount].clientid + " Name :" + clients[ccount].Name + " Added.");                        ccount++;                        Console.ReadKey();

ID is always 0 when I writing clients to the console.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/388284-c-array-not-quite-working-right/
Share on other sites

Link to post
Share on other sites

Is there a for or foreach loop missing in your code?

As the code stands right now, you always assign 0 to the clientid as ccount is always = 0

My beast (PC):

  • ASUS R.O.G. Rampage IV Black Edition, X79, LGA2011, AC4
  • 2x ASUS Fury X
  • Intel Core i7 4930K BOX (LGA 2011, 3.40GHz) OC @4.4GHz
  • Corsair H100i, CPU Cooler (240mm)
  • Samsung SSD 830 Series (512GB)
  • Samsung SSD 850 Pro 1TB
  • Western Digital 512GB HDD
  • TridentX 4x 8GB 2400MHz
  • Corsair Obsidian 900D

Link to post
Share on other sites

Is there a for or foreach loop missing in your code?

As the code stands right now, you always assign 0 to the clientid as ccount is always = 0

under the writeline there is ccount++; which does add 1 to the number as i have it printed at the top of my menu. It is adding the name to a new entity but the ID just isn't going up like i expect it to

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Yes because you are raising ccount AFTER you asigned it to the property clientid.

My beast (PC):

  • ASUS R.O.G. Rampage IV Black Edition, X79, LGA2011, AC4
  • 2x ASUS Fury X
  • Intel Core i7 4930K BOX (LGA 2011, 3.40GHz) OC @4.4GHz
  • Corsair H100i, CPU Cooler (240mm)
  • Samsung SSD 830 Series (512GB)
  • Samsung SSD 850 Pro 1TB
  • Western Digital 512GB HDD
  • TridentX 4x 8GB 2400MHz
  • Corsair Obsidian 900D

Link to post
Share on other sites

Yes because you are raising ccount AFTER you asigned it to the property clientid.

yeah the problem is the next entry also has the ID of 0

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

yeah the problem is the next entry also has the ID of 0

Because the default ID is 0 and you only write to the first element in the array because you never repeat it.

 

You need a for loop.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Don't know about C#, but in C++ :

#include <iostream>using namespace std;struct client {int ID;string name;};struct client clients[100];int main(){    int n;    cout << "Number of clients : ";    cin >> n;    for(int i = 0; i < n;i++)    {        clients[i].ID = i;        cin >> clients[i].name;    }    for(int i = 0; i < n;i++)        cout << clients[i].ID << ' ' << clients[i].name << "\n";    return 0;} 

.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

This is how your code should look like with a loop (Nineshadow's code is C++, not C#

var clients = new client[100];for ( var i = 0; i < clients.Length; i++){    Console.WriteLine("Enter client name");    string name = Console.ReadLine();    clients[i] = new client { clientid = i, Name = name};    Console.WriteLine("ID :" + clients[i].clientid + " Name :" + clients[i].Name + " Added.");}Console.ReadKey();

My beast (PC):

  • ASUS R.O.G. Rampage IV Black Edition, X79, LGA2011, AC4
  • 2x ASUS Fury X
  • Intel Core i7 4930K BOX (LGA 2011, 3.40GHz) OC @4.4GHz
  • Corsair H100i, CPU Cooler (240mm)
  • Samsung SSD 830 Series (512GB)
  • Samsung SSD 850 Pro 1TB
  • Western Digital 512GB HDD
  • TridentX 4x 8GB 2400MHz
  • Corsair Obsidian 900D

Link to post
Share on other sites

You're replacing your first object with a different object.

clients[ccount] = new client { clientid = ccount}; // creating new object, setting the idclients[ccount] = new client {Name = name}; // creating another new, different object, and setting the name (id isn't set)

The ID is always zero because that is the default value of an int property. When you declare your second object, it wont have a value assigned to the id. To fix it, assign all properties at once.

clients[ccount] = new client {clientid = ccount, Name = name};

Just for reference, you could also modify the object after it was created the first time.

clients[ccount] = new client { clientid = ccount}; // create objectclients[ccount].Name = name; // modify object
Link to post
Share on other sites

 

You're replacing your first object with a different object.

clients[ccount] = new client { clientid = ccount}; // creating new object, setting the idclients[ccount] = new client {Name = name}; // creating another new, different object, and setting the name (id isn't set)

The ID is always zero because that is the default value of an int property. When you declare your second object, it wont have a value assigned to the id. To fix it, assign all properties at once or create the object before you add it to the array.

clients[ccount] = new client {clientid = ccount, Name = name};

I replaced ccount with i (the for loop counter) and missed to replace some ccount calls. Edited and should be fixed.

 

EDIT: Ah sry, I thought you were talking to me lol

My beast (PC):

  • ASUS R.O.G. Rampage IV Black Edition, X79, LGA2011, AC4
  • 2x ASUS Fury X
  • Intel Core i7 4930K BOX (LGA 2011, 3.40GHz) OC @4.4GHz
  • Corsair H100i, CPU Cooler (240mm)
  • Samsung SSD 830 Series (512GB)
  • Samsung SSD 850 Pro 1TB
  • Western Digital 512GB HDD
  • TridentX 4x 8GB 2400MHz
  • Corsair Obsidian 900D

Link to post
Share on other sites

Nope, but your code also has the same issue I explained.

 

Urm, I don't think so?

 

EDIT: Ah nevermind, I see what you meant now and fixed it. Damn copy pasting lol

My beast (PC):

  • ASUS R.O.G. Rampage IV Black Edition, X79, LGA2011, AC4
  • 2x ASUS Fury X
  • Intel Core i7 4930K BOX (LGA 2011, 3.40GHz) OC @4.4GHz
  • Corsair H100i, CPU Cooler (240mm)
  • Samsung SSD 830 Series (512GB)
  • Samsung SSD 850 Pro 1TB
  • Western Digital 512GB HDD
  • TridentX 4x 8GB 2400MHz
  • Corsair Obsidian 900D

Link to post
Share on other sites

Nope, but your code also has the same issue I explained.

Sometimes you miss the most f*cking obvious issues...happens so often though.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×