PDA

View Full Version : چرا این خروجی null میده برنامه؟



RIG000
پنج شنبه 24 مهر 1393, 07:58 صبح
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
public class Person
{
// سازنده همیشه وجود دارد به طور مخفی اما ما باید بنویسیمش
public Person()
{
this.Name = "Amin";
this.Family = "Saadati";
}
// این یک فیلد است


private string _name;
private string _family;
// این یکپروپرتی است
public string Name { get { return _name; } set { value= _name; } }
public string Family { get { return _family; } set { value = _family; } }
//این یک پرورتی readonly است
public string ShowInfo { get { return String.Format("{0}{1}",this.Name,this.Family); } }
}
}





using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Person person_1 = new Person();

Console.WriteLine("{0}",person_1.ShowInfo);
Console.ReadKey();
}


}
}

warning داره .!!
null میکنه مقدار رو.....

Warning 1 Field 'ConsoleApplication1.Person._name' is never assigned to, and will always have its default value null D:\Projeha khob\Csharp Introduction\Chapter 01\ConsoleApplication1\ConsoleApplication1\Person. cs 19 24 ConsoleApplication1

RIG000
پنج شنبه 24 مهر 1393, 07:59 صبح
عملا میگه که این فیلد ها هیچ و قت استفاده نمیشن.!!

private string _name;
private string _family;

plus
پنج شنبه 24 مهر 1393, 08:03 صبح
شما در تعریف Property ها فیلدها رو درست مقدار دهی نکردین.بجای اینکه value رو به فیلدها انتساب بدین، فیلدها رو به value انتساب دادین.
وقتی فیلدی داشته باشین که هیچ جا مقدار دهی نگه با اون warning مواجه میشین.

public string Name { get { return _name; } set { _name = value; } }
public string Family { get { return _family; } set { _family = value ; } }

RIG000
پنج شنبه 24 مهر 1393, 08:54 صبح
یه ارور دیگه دارم . !!!

public Adress Address { get { return _personaddress; } set { _personaddress = value; } }
اینو کلس person منه. و ارور میده

Error 2 Cannot implicitly convert type 'ConsoleApplication2.Adress' to 'string' D:\Projeha khob\Csharp Introduction\Chapter 01\ConsoleApplication2\ConsoleApplication2\Person. cs 27 86 ConsoleApplication2


اما کلاس adress منم کامل نوشتمش. چه ربطی به کانورت داره!!
اگه int هم داشته باشم اونور باز string از این ور میخونه!! متوجه ارورش نمیشم تو این مرحله از برنامه


using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication2
{
public class Adress
{
public Adress()
{


}
public Adress( int id,string address)
{
this.ID = id;
this.Address = address;
}
private int _id;
private string _address;




public int ID { get { return _id; } set { _id = value; } }
public string Address { get { return _address; } set { _address = value; } }






public string ShowAddress { get { return string.Format("{0}{1}", this.ID, this.Address); } }
}
}

RIG000
پنج شنبه 24 مهر 1393, 09:02 صبح
حل شد.

private Adress _personaddress;
private Phone_Person _phone;