请问一个很弱的问题:如何在构造函数中初始化成员

syhan 2006-01-01 06:04:08
比如说:
class Graph
{
private string[] NodeList;
private int[,] Edge;
public Graph(int sz)
{
string[] NodeList = new string[sz];
for (int j = 0; j < sz; j++)
{
NodeList[j] = "";
}
int[,] Edge = new int[sz, sz];
for (int i = 0; i < sz; i++)
{
for (int j = 0; j < sz; j++)
{
Edge[i, j] = 0;
}
}
}
}
}
编译之后也能通过,但是有如下两个警告:
Warning 1 Field 'Traffic.Graph.NodeList' is never assigned to, and will always have its default value null E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Graph.cs 10 26 Traffic
Warning 2 Field 'Traffic.Graph.Edge' is never assigned to, and will always have its default value null E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Graph.cs 11 24 Traffic

然后我运行时程序就抛出异常了System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="Traffic"
StackTrace:
at Traffic.Graph.InsertNode(String name) in E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Graph.cs:line 98
at Traffic.Form1.button2_Click(Object sender, EventArgs e) in E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Form1.cs:line 65
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Traffic.Program.Main() in E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Program.cs:line 19
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


我是green hand ,大家多帮忙啊,谢谢了
...全文
162 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
syhan 2006-01-01
  • 打赏
  • 举报
回复
已经给分了,谢谢几位,我是个初学者,希望能和大家多多交流
syhan 2006-01-01
  • 打赏
  • 举报
回复
哦,原来是这样啊,我感觉顿悟
谢谢二位了,噢不,应该是三位,呵呵
xxuu503 2006-01-01
  • 打赏
  • 举报
回复
回复人: xxuu503(学会糜烂和挥霍,恐惧不安和堕落!) ( ) 信誉:100 2006-01-01 20:24:00 得分: 0

回复人: crossbowvic(漫步的兔子) ( ) 信誉:100 2006-01-01 20:24:00 得分: 0



真巧啊!赫赫!
xxuu503 2006-01-01
  • 打赏
  • 举报
回复
我是说他写的对

回复人: Snowdust(雪尘) ( ) 信誉:100 2006-01-01 19:29:00 得分: 0
疯狂Rabbit 2006-01-01
  • 打赏
  • 举报
回复
private string[] NodeList;
private int[,] Edge;
public Graph(int sz)
{
string[] NodeList = new string[sz];

=====================================================
你已经定义了类成员:“private string[] NodeList;”
可是你在构造函数里又定义了一个同名的:“string[] NodeList = new string[sz];”
所以编译的时候才报那样的警告,因为C#有作用域的问题,构造函数里的NodeList把外部的同名变量“NodeList”给遮盖了,也就是说,你虽然在这里赋了值,但只是对这个局部变量的,而成员变量NodeList 仍旧是null,所以在运行时当访问此变量时就发生未将引用未实例化的错误了。
xxuu503 2006-01-01
  • 打赏
  • 举报
回复
private string[] NodeList;//此处定义了类成员NodeList
private int[,] Edge;
public Graph(int sz)
{
string[] NodeList = new string[sz];//此处定义了函数内的临时变量NodeList
//应该是:NodeList = new string[sz];


两个NodeList并不是同一个东西!

这是变量的作用域的问题

实际上类成员NodeList和Edge没有被初始化

我楼上的那位改的正确的
syhan 2006-01-01
  • 打赏
  • 举报
回复
请问楼上的是什么意思,不好意思我没有看懂。

当我设置断点进去看的时候的确都按照我想的给初始化过了,但是出来之后,发现NodeList和Edge都是NULL这就让我很郁闷了,问什么会这样子呢?
Snowdust 2006-01-01
  • 打赏
  • 举报
回复
class Graph
{
private string[] NodeList;
private int[,] Edge;
public Graph(int sz)
{
NodeList = new string[sz];
for (int j = 0; j < sz; j++)
{
NodeList[j] = "";
}
Edge = new int[sz, sz];
for (int i = 0; i < sz; i++)
{
for (int j = 0; j < sz; j++)
{
Edge[i, j] = 0;
}
}
}
}
}
syhan 2006-01-01
  • 打赏
  • 举报
回复
哎呀,怎么不能编辑呢,那个难道成员不是在构造函数中初始化的吗?

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧