作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有课!
class Reg_v_no_string
{
public static string key_place = "";
public static string key = "";
public static string value = "";
public string reg_value(string key_place, string key)
{
string value = string.Empty;
RegistryKey klase = Registry.CurrentUser;
klase = klase.OpenSubKey(key_place);
value = klase.GetValue(key).ToString();
klase.Close();
return value;
}
}
这将返回空白消息框。怎么解决这个问题!?谢谢!
我怎样才能从这个类返回值?
我试过了
private void kryptonButton1_Click(object sender, EventArgs e)
{
Reg_v_no_string.key_place = @"Control Panel\Desktop";
Reg_v_no_string.key_place = "WheelScrollLines";
MessageBox.Show(Reg_v_no_string.value);
}
最佳答案
您需要实际调用该方法:
private void kryptonButton1_Click(object sender, EventArgs e)
{
var value = reg_value(@"Control Panel\Desktop", "WheelScrollLines");
MessageBox.Show(value);
}
还要考虑对您的类进行一些更改:
public static class Reg_v_no_string
{
public static string reg_value(string key_place, string key)
{
string value = string.Empty;
RegistryKey klase = Registry.CurrentUser;
// todo: add some error checking to make sure the key is opened, etc.
klase = klase.OpenSubKey(key_place);
value = klase.GetValue(key).ToString();
klase.Close();
return value;
}
}
然后当你调用这个 static
类时,它是这样的:
// you don't need to `new` this class if it is static:
var value = Reg_v_no_string.reg_value(@"Control Panel\Desktop", "WheelScrollLines");
或者,让它不是静态的:
public class Reg_v_no_string
{
public string reg_value(string key_place, string key)
{
string value = string.Empty;
RegistryKey klase = Registry.CurrentUser;
// todo: add some error checking to make sure the key is opened, etc.
klase = klase.OpenSubKey(key_place);
value = klase.GetValue(key).ToString();
klase.Close();
return value;
}
}
然后这样调用它:
Reg_v_no_string obj = new Reg_v_no_string ();
var value = reg_value(@"Control Panel\Desktop", "WheelScrollLines");
关于c# 如何从类中返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11621672/
我是一名优秀的程序员,十分优秀!