一个简单的代码错误,Use of unassigned local variable 'x'

weity 2003-02-12 04:33:52
// 05 - Classes 101\Ref and Out Parameters
// copyright 2000 Eric Gunnerson
// error
using System;
class Point
{
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
// get both values in one function call
public void GetPoint(ref int x, ref int y)
{
x = this.x;
y = this.y;
}

int x;
int y;
}

class Test
{
public static void Main()
{
Point myPoint = new Point(10, 15);
int x;
int y;

// illegal
myPoint.GetPoint(ref x, ref y);
Console.WriteLine("myPoint({0}, {1})", x, y);
}
}
H:\-=Visual Studio Projects=-\Empty\File_2.cs(32): Use of unassigned local variable 'x'

Please help me.
...全文
3751 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
weity 2003-02-13
  • 打赏
  • 举报
回复
不好意思,这么简单也要麻烦各位,其实我没有中文的资料,想多学习一些解决方案和系统架构,这可是语言无关性的。

其实,接触coding也很久了,老想做一些更高级的东西。
好了不多说了。

to:feilng(feilng)
你的方法没有成功,总是告诉我非法的变量,如果写成out int x这样,也不行。
不知道为何?

好了,结账。
有闲聊的发这里的消息或者写信,我不在国内。
alexlimingjun 2003-02-12
  • 打赏
  • 举报
回复
要对x,y初始化,例如:
int x = 0;
int y = 0;
feilng 2003-02-12
  • 打赏
  • 举报
回复
局部变量未初始化就使用,改为:
myPoint.GetPoint(out x, out y);

-_-中英文数字 2003-02-12
  • 打赏
  • 举报
回复
Hi,weity ()

You should initialize local variables manually before using them.

In class Test:
int x,y should be assigned to a value.

110,533

社区成员

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

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

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