小白又来请教大佬了
c#吧
全部回复
仅看楼主
level 4
yydgys2007 楼主
public struct data
{
public int[] version;
public string[] pathh;
}
private data GetPath(string p)
{
data d = new data();
RegistryKey hkcu = Registry.CurrentUser;
RegistryKey software = hkcu.OpenSubKey("SOFTWARE");
RegistryKey gstar = software.OpenSubKey("123");
RegistryKey soft = gstar.OpenSubKey(p);
string[] middle = soft.GetSubKeyNames();
for (int i = 0; i <= middle.Length; i++)
{
d.version[i] = int.Parse(middle[i]);
}
for (int i = 0; i <= soft.SubKeyCount; i++)
{
RegistryKey zhcn = soft.OpenSubKey("zh_CN");
d.pathh[i] = zhcn.GetValue("location").ToString();
}
soft.Close();
gstar.Close();
software.Close();
hkcu.Close();
return d;
}
调试时在“d.version[i] = int.Parse(middle[i]);”处报错,提示“Object reference not set to an instance of an object.”
查了报错信息,说是因为没有实例化,可是我middle还有d都实例化了呀?
2023年09月04日 01点09分 1
level 14
d 里面的 version 没有实例化
2023年09月04日 01点09分 2
可是整个struct都已经实例化为d了啊
2023年09月04日 02点09分
@yydgys2007 d 不等于 version 实例化化哈,继续学习
2023年09月04日 02点09分
level 12
数组是引用类型,默认是个null,你这里要用的话需要自己初始化的。
2023年09月04日 02点09分 3
level 4
yydgys2007 楼主
好吧,可我的不定数组在定义的时候是不确定数组长度的……
2023年09月04日 14点09分 5
先用list 接收 然后再 toarra()
2023年09月08日 10点09分
level 6
public struct data
{
public int[] version;
public string[] pathh;
}
private data GetPath(string p)
{
data d = new data();
RegistryKey hkcu = Registry.CurrentUser;
RegistryKey software = hkcu.OpenSubKey("SOFTWARE");
RegistryKey gstar = software.OpenSubKey("123");
RegistryKey soft = gstar.OpenSubKey(p);
string[] middle = soft.GetSubKeyNames();
d.version = new int[middle.Length]; // 初始化 version 数组
d.pathh = new string[soft.SubKeyCount]; // 初始化 pathh 数组
for (int i = 0; i < middle.Length; i++)
{
d.version[i] = int.Parse(middle[i]);
}
for (int i = 0; i < soft.SubKeyCount; i++)
{
RegistryKey zhcn = soft.OpenSubKey("zh_CN");
d.pathh[i] = zhcn.GetValue("location").ToString();
}
soft.Close();
gstar.Close();
software.Close();
hkcu.Close();
return d;
}
2023年09月12日 06点09分 6
谢谢,我后来清楚了,我确实没考虑到需要强制赋值。
2023年09月12日 06点09分
1