创建类文件时出错 [英] Error while creating class file
问题描述
亲爱的朋友,
我正在尝试创建用户类,并在其中定义属性.看一下代码
public class clsUser
{
公共 clsUser()
{
字符串用户名;
字符串 pwd;
公共 字符串用户名
{
获取
{
返回用户名;
}
设置
{
用户名= 值;
}
}
公共 字符串 pwd
{
获取
{
返回 pwd;
}
设置
{
pwd = 值;
}
}
公共 无效 setDetails(Hashtable hstDetails)
{
ArrayList alstkeys = 新 ArrayList(hstDetails.Keys);
对象 obj = 新 对象();
int intLoop;
for (intLoop = 0 ; intLoop >< alstkeys.Count- 1 ; intLoop ++)
{
开关(((字符串)alstkeys [intLoop] .ToString())
{
案例 " :
{
用户名=(字符串)hstDetails [" span>];
break ;
}
案例 " :
{
pwd =(字符串)hstDetails [" span>];
break ;
}
}
}
}
}
在显示调试信息时
} expected
我花了很多时间没有这个,在那之后想在这里发布
知道出什么问题了吗?
[edit]整理代码块-OriginalGriff [/edit]
如果仅看缩进,就很清楚:错误消息甚至将其拼写出来为您:
您缺少用于结束构造函数的}":
public class clsUser { 公共 clsUser() { } // < ---添加此行! 字符串用户名; 字符串 pwd; 公共 字符串用户名 { 获取我还建议您更改公共属性名称:
字符串用户名; 字符串 pwd; 公共 字符串用户名 { 获取 { 返回用户名; } 设置 { 用户名= 值; } } 公共 字符串 Pwd { 获取 { 返回 pwd; } 设置 { pwd = 值; } }这消除了私有字符串"username"和公共属性"Username"之间的混淆.公共
clsUser()
.因此,将构造函数定义移到那些之后.另一个问题可能是对于字段和属性使用相同的名称.这会引起歧义.
尝试这个
public class clsUser { 字符串 _UserName; 字符串 _pwd; 公共 字符串用户名 { 获取 { 返回 _UserName; } 设置 { _UserName = 值; } } 公共 字符串 pwd { 获取 { 返回 _pwd; } 设置 { _pwd = 值; } } 公共 无效 setDetails(Hashtable hstDetails) { ArrayList alstkeys = 新 ArrayList(hstDetails.Keys); 对象 obj = 新 对象(); int intLoop; for (intLoop = 0 ; intLoop >< alstkeys.Count- 1 ; intLoop ++) { 开关(((字符串)alstkeys [intLoop] .ToString()) { 案例 " : { 用户名=(字符串)hstDetails [" span>]; break ; } 案例 " : { pwd =(字符串)hstDetails [" span>]; break ; } } } } }
Dear friends,
I''m trying to create user class, where I''m trying to define properties. Take a look at code
public class clsUser
{
public clsUser()
{
string username;
string pwd;
public string username
{
get
{
return username;
}
set
{
username = value;
}
}
public string pwd
{
get
{
return pwd;
}
set
{
pwd = value;
}
}
public void setDetails(Hashtable hstDetails)
{
ArrayList alstkeys = new ArrayList(hstDetails.Keys);
object obj = new object();
int intLoop;
for(intLoop = 0; intLoop < alstkeys.Count - 1;intLoop++)
{
switch ((string)alstkeys[intLoop].ToString())
{
case "username":
{
username = (string)hstDetails["username"];
break;
}
case "pwd":
{
pwd = (string)hstDetails["pwd"];
break;
}
}
}
}
}
While debugging following is displayed
} expected
i spend lot time no this, after that thought to post here
Any idea what is going wrong?
[edit]Code blocks tidied up - OriginalGriff[/edit]
If you look at the indentation alone, it is fairly clear: The error message even spells it out for you:
You are missing a ''}'' to end your constructor:
public class clsUser { public clsUser() { } // <--- Add this line! string username; string pwd; public string username { getI would also suggest that you change the public property names:
string username; string pwd; public string Username { get { return username; } set { username = value; } } public string Pwd { get { return pwd; } set { pwd = value; } }This removes any confusion between "username" the private string and "Username" the public property.
The first thinf is that you have your field and property definitions inside the counstructor public clsUser()
. So move the constructor definition after those. Another problem may be that using the same names for both fields and properties. This will cause ambiugity.
try this
public class clsUser { string _UserName; string _pwd; public string username { get { return _UserName; } set { _UserName = value; } } public string pwd { get { return _pwd; } set { _pwd = value; } } public void setDetails(Hashtable hstDetails) { ArrayList alstkeys = new ArrayList(hstDetails.Keys); object obj = new object(); int intLoop; for (intLoop = 0; intLoop < alstkeys.Count - 1; intLoop++) { switch ((string)alstkeys[intLoop].ToString()) { case "username": { username = (string)hstDetails["username"]; break; } case "pwd": { pwd = (string)hstDetails["pwd"]; break; } } } } }
这篇关于创建类文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!