Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 06:32

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Constructor - Noobie Question

Discussion on Constructor - Noobie Question within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
»Barney«'s Avatar
 
elite*gold: 0
Join Date: May 2012
Posts: 868
Received Thanks: 947
Constructor - Noobie Question

Hello everyone, I am not a professional developer but I am working on it.

I need your help, please.

I have this code, which is the constructor of the User class.


Look at the last line.
users.Add(this);

users is a list<user> [public static List<User> users = new List<User>();]

by using 'this', I want to say the value created by the constructor.

Is it ok like this, or is it really really bad?
»Barney« is offline  
Old 03/02/2013, 01:16   #2
 
Schlüsselbein's Avatar
 
elite*gold: 0
Join Date: Feb 2013
Posts: 1,137
Received Thanks: 869
Quote:
Is it ok like this, or is it really really bad?
Imho, its really bad practice. Why would a class contain Objects of itself? Create a class called Userbase or so which holds user and information about them.

Another thing i noticed:
Quote:
int uid = new Random().Next(500);
foreach (User u in users)
if (u.UserID == uid)
uid = new Random().Next(500);
uid should be a unique identifier? Its not guaranteed to be unique when doing it like this.
Example:
user1 id: 15
user2 id: 12
user3 id: 19
user4 id: 07
first generated uid = 12
iterating over list:
compate with uid of first user -> ok
compate with uid of second user -> match -> not ok!
new generated uid = 15
compare with uid of third user -> ok
compare with uid of 4th user -> ok
-> everything seems fine, but uid of user1 equals the newly generated uid.
Schlüsselbein is offline  
Thanks
1 User
Old 03/02/2013, 07:08   #3
 
»Barney«'s Avatar
 
elite*gold: 0
Join Date: May 2012
Posts: 868
Received Thanks: 947
Don't worry, I will change the way to create uid's, it was not final, I just wanted it to work for now. I still have a lot of work to do.
»Barney« is offline  
Old 03/06/2013, 12:02   #4
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
You can't do that, otherwise you could end up in a endless loop. By design, constructors are called when allocating a class to a variable (eg. var blah = new User()) so the variable (blah) would contain your new User instance. Why not add it to "users" from the code that creates the instance?

EG.
Code:
User _user = new User(username, password);
users.Add(_user);
-jD
»jD« is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
[Help] Lotf noobie question :D
02/17/2010 - CO2 Private Server - 5 Replies
First of all please don`t flame me , im new at this . So , i added a new npc in the database Id 5556 , spawned him but when i go to the database i can`t see him at the NPC table . (i want to remove it) So , what must i do ?
Agbot noobie question...
05/10/2008 - Silkroad Online - 2 Replies
I clicked on the agbot.exe after the proxy thing, but nothing happened....Whats the problem? Thanks in advance!
Noobie question
04/27/2008 - Dekaron - 2 Replies
hi all :-) wtf is with 2moons all of a sudden? I haven't played in like 3 months and now all i get when i log in and i try to choose char is "the client version does not match"! I've heard that something is going on but how can i play again? I live in germany,does that have to do with anything?



All times are GMT +2. The time now is 06:32.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.