gpt4 book ai didi

c# 如何从类中返回值?

转载 作者:行者123 更新时间:2023-11-30 19:44:54 24 4
gpt4 key购买 nike

我有课!

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com