C#에서 작은 주소록 응용 프로그램을 만들어야합니다. 양식에 이름, 주소, 도시, 주 및 우편 번호가 입력됩니다. 저축을 저장하면 목록 상자에 들어갈 필요가 있습니다. 앱에 2 개의 맞춤 수업 (주소 및 친구)이 포함되어 있습니다. 클래스 다이어그램을 보면 Address 클래스가 Friend 클래스의 위치에 대한 데이터 유형 인 것처럼 보입니다. 친구 클래스의 데이터 유형으로 맞춤 클래스 주소를 사용할 수 있습니까? 주소 클래스에는 address, city, state, zip에 대한 필드가 있으며 friend 클래스에는 name 필드가 있습니다. Friend 클래스의 해당 필드 위치는 Address 클래스의 모든 필드에서 정보를 가져 오지만 Friend 클래스 및 목록 상자로 가져 오는 방법을 알지 못합니다. 바라기를이 질문은 너무 혼란스럽지 않습니다. 나는이 사진 부분에 대해 지금 너무 걱정하지 않는다. 작동하지 않는 폼과사용자 정의 클래스를 데이터 유형으로 사용하려면 어떻게해야합니까?
namespace Friends
{
public class Friend
{
#region [Fields]
private string _name;
private Address _location;
private string _photo;
#endregion
#region [Properties]
public string Name
{
get { return _name; }
set
{
if (value == null)
throw new ArgumentNullException("Name", "Please enter a name");
_name = value.Trim();
}
}
public Address Location { get; set }
public string Photo
{
get { return _photo; }
set { _photo = null;}
}
#endregion
#region Constructors
public Friend()
{
this.Name = String.Empty;
this.Photo = null;
}
public Friend(string name)
{
this.Name = name;
this.Photo = null;
}
public Friend(string name, Address location)
{
this.Name = name;
this.Location = location;
this.Photo = null;
}
#endregion
#region Methods
public override string ToString()
{
return this.Name + " -- " + this.Location;
}
#endregion
}
}
코드 :이 질문은 더 설명이 필요하거나 별도의 질문으로 나눌 수 필요가
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PersonInfo;
namespace Friends
{
public partial class Form1 : Form
{
List<Friend> myFriend = null;
public Form1()
{
InitializeComponent();
myFriend = new List<Friend>();
}
private void btnSave_Click(object sender, EventArgs e)
{
Friend f = new Friend()
{
f.Name = txtName.Text,
f.Location.Street = txtAddress.Text,
f.Location.City = txtCity.Text,
f.Location.State = txtState.Text,
f.Location.Zip5 = txtZip.Text
};
lstContacts.Items.Add(f);
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
있으면 알려 주시기 바랍니다 친구 클래스입니다.
당신이 우리에게 좀 더 많은 정보를 표시 할 수 있습니다 :
NullReferenceException
의이 발생되지 않도록하기 위해 첫 번째 생성자에Location
속성을 초기화? 예를 들어, 코드가 현재 작동합니까? 출력은 무엇입니까? 이상한 결과가 있습니까? 너는 무엇을 기대 하느냐? –죄송합니다. 질문을 이해하지 못합니다. – DidIReallyWriteThat
내 기대는 "Scott - 123 AnyPlace Park City, UT 12345"와 유사한 결과물을 목록 상자에 넣었습니다. 내 텍스트 상자에있는 정보를 Address 클래스를 통해 Field 클래스로 전달하고 내 목록 상자에서 끝내는 방법은 무엇입니까? 나는이 방식으로 2 개의 커스텀 클래스를 다루지 못했다. – ScottT