c# 如何从类中返回值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11621672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:32:33  来源:igfitidea点击:

c# how can return value from class?

c#classregistryreturn-value

提问by deleted

I have class !

我有课 !

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;
    }
}

and this returns blank message box. How can solve this problem !? Thanks !

这将返回空白消息框。怎么才能解决这个问题!?谢谢 !

how can i return value from this class ?

我怎样才能从这个类返回值?

I tryed

我试过了

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);

    }

采纳答案by Brad Rem

You need to actually call the method:

您需要实际调用该方法:

private void kryptonButton1_Click(object sender, EventArgs e) 
{ 
    var value = reg_value(@"Control Panel\Desktop", "WheelScrollLines");
    MessageBox.Show(value); 
} 

Also consider some changes to your class:

还要考虑对您的班级进行一些更改:

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; 
    } 
} 

And then when you call this staticclass it is like this:

然后当你调用这个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");

Or, to keep it so that it is not static:

或者,保持它不是静态的:

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; 
    } 
} 

Then call it like this:

然后像这样调用它:

Reg_v_no_string obj = new Reg_v_no_string ();
var value = reg_value(@"Control Panel\Desktop", "WheelScrollLines");

回答by Alexei Levenkov

There is no code that calls reg_value.

没有调用reg_value.

As result you are getting default (also shared between instances) value of valuefield.

因此,您将获得value字段的默认值(也在实例之间共享)。

回答by Bishan

Reg_v_no_string.valueis not set yet.
You have to call reg_value(string key_place, string key)function that will return the value.

Reg_v_no_string.value尚未设置。
您必须调用reg_value(string key_place, string key)将返回值的函数。