using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Album al = new Album(5);
photo ph1 = new photo();
ph1.Name = "杭州";
photo ph2 = new photo();
ph2.Name = "北海";
photo p = al["杭州"];
Console.WriteLine(p.Name.ToString());
Console.ReadLine();
}
} class Album
{
photo[] phs; internal photo[] Phs
{
get { return phs; }
set { phs = value; }
}
public Album(int count)
{
phs = new photo[count];
}
public photo this[string name]
{
get
{
photo ph = null;
for (int i = 0; i < phs.Length; i++)
{
if (phs[i].Name == name)
{
ph = phs[i];
}
}
return ph;
}
}
} class photo
{
string _name; public string Name
{
get { return _name; }
set { _name = value; }
}
}
}在这段索引器代码中“if (phs[i].Name == name)”会报错说没有实例化对象是怎么回事啊?我明明已经实例化了啊
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Album al = new Album(5);
photo ph1 = new photo();
ph1.Name = "杭州";
photo ph2 = new photo();
ph2.Name = "北海";
photo p = al["杭州"];
Console.WriteLine(p.Name.ToString());
Console.ReadLine();
}
} class Album
{
photo[] phs; internal photo[] Phs
{
get { return phs; }
set { phs = value; }
}
public Album(int count)
{
phs = new photo[count];
}
public photo this[string name]
{
get
{
photo ph = null;
for (int i = 0; i < phs.Length; i++)
{
if (phs[i].Name == name)
{
ph = phs[i];
}
}
return ph;
}
}
} class photo
{
string _name; public string Name
{
get { return _name; }
set { _name = value; }
}
}
}在这段索引器代码中“if (phs[i].Name == name)”会报错说没有实例化对象是怎么回事啊?我明明已经实例化了啊
解决方案 »
- datagridviewcombobox加入预设值
- C#操作PPT时已经引用了PowerPoint组件,在OPen该ppt时传第二个参数是为啥找不到Microsoft.Office.Core.MsoTriState?
- VS2005中datalist绑定的问题????
- C#中如何在TextBox控件中显示一个表格
- 打印机页面设置的问题!
- 有关 Explorer
- 一个关于MSN奇怪的现象!请大家看看自己的MSN有没有这种现象.
- 未将对象引用设置到对象的实例。这是什么意思
- 请问在C#里面操作EXCEL生成数据时怎么实现强制分页???
- 强烈要求aspcn(飞刀)写写自己的程序人生历程,各位兄弟一起帮我推一下!!!
- 关于动态载入DLL的一个问题
- 银行POS 系统一般用什么语言开发
static void Main(string[] args)
{
Album al = new Album(5);
photo ph1 = new photo();
ph1.Name = "杭州";
photo ph2 = new photo();
ph2.Name = "北海";
photo p = al["杭州"];
Console.WriteLine(p.Name.ToString());
Console.ReadLine();
}
里面加上
al.Phs[0] = ph1;
al.Phs[1] = ph2;
变成
static void Main(string[] args)
{
Album al = new Album(5);
photo ph1 = new photo();
ph1.Name = "杭州";
photo ph2 = new photo();
ph2.Name = "北海";
al.Phs[0] = ph1;
al.Phs[1] = ph2;
photo p = al["杭州"];
Console.WriteLine(p.Name.ToString());
Console.ReadLine();
}
还是不行啊。
当然不行了,你那个循环到2的时候依然数组Phs[2~5]依然是null
{
get
{
foreach (photo ph in phs)
{
if (ph != null)
{
if (ph.Name == name)
{
return ph;
}
}
}
return null;
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;namespace ConsoleApplication1
{ class Program
{
static void Main(string[] args)
{
Album al = new Album();
photo ph1 = new photo();
ph1.Name = "杭州";
photo ph2 = new photo();
ph2.Name = "北海";
al.AddPhoto(ph1);
al.AddPhoto(ph2);
photo p = al["杭州州"];
if (p != null)
{
Console.WriteLine(p.Name.ToString());
}
else
{
Console.WriteLine("对象不存在");
} Console.ReadLine(); }
}
class Album
{
List<photo> phs=new List<photo>();
public Album()
{
} public void AddPhoto(photo ph)
{
FindPhoto f = new FindPhoto(ph.Name);
if (!this.phs.Exists(new Predicate<photo>(f.hasPhoto)))
{
this.phs.Add(ph);
}
} public photo this[string name]
{
get
{
FindPhoto f = new FindPhoto(name);
if (this.phs.Exists(new Predicate<photo>(f.hasPhoto)))
{
return this.phs.Find(new Predicate<photo>(f.hasPhoto));
}
return null;
}
}
} class FindPhoto
{
private string photoName=String.Empty;
public FindPhoto(string pName)
{
this.photoName = pName;
} public bool hasPhoto(photo ph)
{
if (ph.Name.Equals(this.photoName))
{
return true;
}
else
{
return false;
}
}
} class photo
{
string _name; public string Name
{
get { return _name; }
set { _name = value; }
}
}
}