hi guys i was trying to create an class which contain array of reference type
and i was trying to assign same type object to the array element but it was showng me some errors i dont know where i am doing wrong that code is as follow please check it out and tell me where i did wrong
using System;
namespace EnumExpl
{
class employee
{
public string firstname;
public string lastname;
}
class employees
{
employee prashant=new employee()
{
firstname="prashant",
lastname="jadhav"
};
employee prakash=new employee()
{
firstname="prakash",
lastname="jadhav"
};
public employee[] Empl = new employee[2];
Empl[0]=prashant;
}
class Program
{
static void Main(string[] args)
{
}
}
}
Error1Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)c:\users\prashant\documents\visual studio 2010\Projects\enumarator\enumarator\Program.cs2714enumarator
Error2Invalid token '=' in class, struct, or interface member declarationc:\users\prashant\documents\visual studio 2010\Projects\enumarator\enumarator\Program.cs2716enumarator
Error3Invalid token ';' in class, struct, or interface member declarationc:\users\prashant\documents\visual studio 2010\Projects\enumarator\enumarator\Program.cs2725enumarator
these are those error messege i was getting
Loading
VulpesPosted Aug 2, 2014, 3:05 PM
However, the constructor code may provide further initialization of the object's fields where this cannot conveniently be done where the fields are defined.
Prashant JadhavPosted Aug 2, 2014, 2:24 PM
it does not have relation with creating object instance with respective of their field and their values .
VulpesPosted Aug 2, 2014, 11:01 AM
Prashant JadhavPosted Aug 2, 2014, 9:11 AM
yes vulpes its weird.it means even x is static member they are allowing it default value to get assign to other static member of that class and according to this rule default values os instance variable should be allowed to get assign to other instance field
guide me if i am taking senseless
static public y=x <---if this is giving 0 value to y
static public x=5
then why not
public x=5
public y=x <----- this gives y 0 value
since even before object is created every fields value is initialized to 0
and why the execution is get stop here just because that same object of x variable is not created
and my another question is
how can we reference non static field value to other non static field in constructor
even when object does not get created before constructor does not get execute completely
as you replied
in this same post
However, you can of course do it at method level and so, if you add this constructor and put the line in there, it will work fine:
public employees()
{
Empl[0]=prashant;
}
i mean both instance fields belongs to same instance of class then how can we assign prashant which is instance member of object to empl[0] instance member of same object untill the whole constructor is not executed it mean that object it self is not created
VulpesPosted Aug 1, 2014, 11:24 AM
Prashant JadhavPosted Aug 1, 2014, 9:58 AM
it mean since whole object is not been created at the time while we are trying to initialize non static member of same object ('this') so it is not allowed to use instance field value of same object for initializing other instance fields.
VulpesPosted Aug 1, 2014, 9:09 AM
int x=5;
int y=x;
The basic rule is that an instance field initializer cannot reference the instance being created (i.e. 'this').
So it can't reference any other instance fields of the current instance. Nor can it reference any methods or properties of the current instance and so this wouldn't be allowed either:
The reason for this rule is that, whilst the fields are being initialized (which occurs in textual order), the instance is not considered to have been fully created and you could get some odd results if it were allowed
However, there's nothing to stop you using static members of the same class to initialize an instance field. So this is OK:
The point here is that static fields will always have been initialized before any instance fields and so allowing this doesn't cause any problems.
Incidentally, none of this is obvious and so this certainly isn't a stupid question, it's a good one :)
Prashant JadhavPosted Aug 1, 2014, 8:30 AM
it means even
int x=5;
int y=x;
wont be possible in class level
does it mean we can not use any other instance field value to instantiate other instance fields of same type.even when the value we need is equivalent.
and please guide me the way i was thinking was stupid or people may misunderstood in such way.
VulpesPosted Aug 1, 2014, 6:46 AM
Empl[0]=prashant;
If you remove it, the program will compile OK.
The reason for the error is that you're not allowed to assign values to individual array elements at class level.
However, you can of course do it at method level and so, if you add this constructor and put the line in there, it will work fine:
public employees()
{
Empl[0]=prashant;
}
Incidentally, you're not allowed to do this either:
public employee[] Empl = new employee[2]{prashant, prakash};
The reason this time is that you can't reference other fields when initializing the array at class level.