C# 我应该使用私有函数作为从case语句返回值的最佳方式吗?
在我的程序中,我编写了以下私有变量和函数C# 我应该使用私有函数作为从case语句返回值的最佳方式吗?,c#,C#,在我的程序中,我编写了以下私有变量和函数 private string _viewName; private string _refValue; private void getContentDetails(string id) { switch (id.Substring(2, 2)) { case "00": _refValue = "14"; _viewName = "Menu"; break;
private string _viewName;
private string _refValue;
private void getContentDetails(string id) {
switch (id.Substring(2, 2))
{
case "00": _refValue = "14"; _viewName = "Menu"; break;
case "01": _refValue = "18"; _viewName = "Topic"; break;
default: _refValue = "00"; _viewName = "Menu"; break;
}
}
我正在寻找一种更好的编码方法,我正在考虑用两个私有函数来代替它
getViewName(id);
getRefValue(id);
有人能建议我编写代码的最佳方式吗 使用一个类:
class ContentDetails
{
public string ViewName { get; set; }
public string RefValue { get; set; }
}
像这样使用它:
switch (id.Substring(2, 2))
{
case "00": return new ContentDetails { RefValue = "14", ViewName = "Menu" };
case "01": return new ContentDetails { RefValue = "18", ViewName = "Topic" };
default: return new ContentDetails { RefValue = "00", ViewName = "Menu" };
}
// Static factory method creates a new ContentDetails from the specified `id`
ContentDetails details = ContentDetails.GetContentDetails(id);
Console.WriteLine(details.RefValue); // writes RefValue
Console.WriteLine(details.ViewName); // writes ViewName
使用一个类:
class ContentDetails
{
public string ViewName { get; set; }
public string RefValue { get; set; }
}
像这样使用它:
switch (id.Substring(2, 2))
{
case "00": return new ContentDetails { RefValue = "14", ViewName = "Menu" };
case "01": return new ContentDetails { RefValue = "18", ViewName = "Topic" };
default: return new ContentDetails { RefValue = "00", ViewName = "Menu" };
}
// Static factory method creates a new ContentDetails from the specified `id`
ContentDetails details = ContentDetails.GetContentDetails(id);
Console.WriteLine(details.RefValue); // writes RefValue
Console.WriteLine(details.ViewName); // writes ViewName
虽然我不认为在类中使用私有全局变量有什么问题,但是如果您想要一个替代的全局变量,可以通过引用并作为out参数来传递它们
private getContentDetails(string id, ref string refValue, ref string viewName)
private getContentDetails(string id, out string refValue, out string viewName)
虽然我不认为在类中使用私有全局变量有什么问题,但是如果您想要一个替代的全局变量,可以通过引用并作为out参数来传递它们
private getContentDetails(string id, ref string refValue, ref string viewName)
private getContentDetails(string id, out string refValue, out string viewName)
在一个开关中放入多个语句当然没有错,但这里似乎有一些重复,特别是当与ID耦合的所需值发生更改时 在我看来,您必须为映射id和相应值的
viewName
和refValue
使用map collection对象。然后使用yourCollection.get(id)代码>。这肯定会使您的代码保持最佳的可维护性和一致性
Dictionary<string, string> _refValues = new Dictionary<string, string>();
Dictionary<string, string> _viewNames = new Dictionary<string, string>();
fillDictionaries();
void fillDictionaries()
{
_refValues.add("00","14"); //one way to add a value
_viewNames["00"] = "Menu"; //another way to add a value
}
string getRefValue(string id)
{
return _refvalues[id]; //there may be a get method but I'm not using an IDE atm
}
//same function for viewNames
Dictionary\u refValues=newdictionary();
字典_viewNames=新字典();
填写字典();
void filldictionares()
{
_refValues.add(“00”,“14”);//添加值的一种方法
_viewNames[“00”]=“菜单”;//另一种添加值的方法
}
字符串getRefValue(字符串id)
{
return _refvalues[id];//可能有get方法,但我没有使用IDE atm
}
//ViewName的函数相同
这甚至还不是最好的解决方案。最好是使用字典将refvalue与viewNames耦合,然后使用另一个字典将id映射到refvalue。然后,可以使用id作为键获取refValue,然后使用返回的refValue作为键获取viewName。希望我说得很清楚:)在一个开关中放入多个语句肯定没有错,但看起来这里可能有一些重复,特别是如果与ID耦合的所需值发生了更改
在我看来,您必须为映射id和相应值的viewName
和refValue
使用map collection对象。然后使用yourCollection.get(id)代码>。这肯定会使您的代码保持最佳的可维护性和一致性
Dictionary<string, string> _refValues = new Dictionary<string, string>();
Dictionary<string, string> _viewNames = new Dictionary<string, string>();
fillDictionaries();
void fillDictionaries()
{
_refValues.add("00","14"); //one way to add a value
_viewNames["00"] = "Menu"; //another way to add a value
}
string getRefValue(string id)
{
return _refvalues[id]; //there may be a get method but I'm not using an IDE atm
}
//same function for viewNames
Dictionary\u refValues=newdictionary();
字典_viewNames=新字典();
填写字典();
void filldictionares()
{
_refValues.add(“00”,“14”);//添加值的一种方法
_viewNames[“00”]=“菜单”;//另一种添加值的方法
}
字符串getRefValue(字符串id)
{
return _refvalues[id];//可能有get方法,但我没有使用IDE atm
}
//ViewName的函数相同
这甚至还不是最好的解决方案。最好是使用字典将refvalue与viewNames耦合,然后使用另一个字典将id映射到refvalue。然后,可以使用id作为键获取refValue,然后使用返回的refValue作为键获取viewName。希望我能说清楚:)有几种方法可以返回多个值
您可以使用out
参数:
private void getContentDetails(string id, out string viewName, out string refValue) {
switch (id.Substring(2, 2)) {
case "00": refValue = "14"; viewName = "Menu"; break;
case "01": refValue = "18"; viewName = "Topic"; break;
default: refValue = "00"; viewName = "Menu"; break;
}
}
可以返回包含两个值的对象:
private KeyValuePair<string, string> getContentDetails(string id) {
switch (id.Substring(2, 2)) {
case "00": return new KeyValuePair<string, string>("Menu", "14");
case "01": return new KeyValuePair<string, string>("Topic", "18");
default: return new KeyValuePair<string, string>("Menu", "00");
}
}
有几种方法可以返回多个值
您可以使用out
参数:
private void getContentDetails(string id, out string viewName, out string refValue) {
switch (id.Substring(2, 2)) {
case "00": refValue = "14"; viewName = "Menu"; break;
case "01": refValue = "18"; viewName = "Topic"; break;
default: refValue = "00"; viewName = "Menu"; break;
}
}
可以返回包含两个值的对象:
private KeyValuePair<string, string> getContentDetails(string id) {
switch (id.Substring(2, 2)) {
case "00": return new KeyValuePair<string, string>("Menu", "14");
case "01": return new KeyValuePair<string, string>("Topic", "18");
default: return new KeyValuePair<string, string>("Menu", "00");
}
}
试试这个实现映射的类(正如Mario所建议的)
idValueDictionary
将id
映射到refValue
,而valueNameDictionary
将refValue
映射到nameValue
。字典是硬编码的,不过如果数据适合您的需要,您可以在运行时加载数据
using System;
using System.Collections.Generic;
class ContentDetails
{
// Maps id to RefValue.
private static readonly Dictionary<string, string> idValueDictionary =
new Dictionary<string,string>()
{
{ "00", "14" },
{ "01", "18" },
{ "XX", "00" }
};
// Maps RefValue to ViewName
private static readonly Dictionary<string, string> valueNameDictionary =
new Dictionary<string,string>()
{
{ "00", "Menu" },
{ "14", "Menu" },
{ "18", "Topic" }
};
// Private constructor. Use GetContentDetails factory method.
private ContentDetails(string refValue, string viewName)
{
this.RefValue = refValue;
this.ViewName = viewName;
}
// Gets the RefValue.
public string RefValue
{
get;
private set;
}
// Gets the ViewName.
public string ViewName
{
get;
private set;
}
// Creates a new ContentDetails from the specified id.
public static ContentDetails GetContentDetails(string id)
{
// Extract key from id.
string key = id.Substring(2,2);
// If key not in dictionary, use the default key "XX".
if (!idValueDictionary.ContainsKey(key))
{
key = "XX";
}
// Get refValue and viewName from dictionaries.
string refValue = idValueDictionary[key];
string viewName = valueNameDictionary[refValue];
// Return a new ContentDetails object with properties initialized.
return new ContentDetails(refValue, viewName);
}
}
使用GetContentDetails
方法创建新的ContentDetails
后,您可以访问这两个属性以获取RefValue
和ViewName
。属性是只读的(private set
),因此在创建类实例后不能更改值(它是不可变的)。每次有不同的id
要查找时,创建一个新的。试试这个实现映射的类(如Mario所建议的)
idValueDictionary
将id
映射到refValue
,而valueNameDictionary
将refValue
映射到nameValue
。字典是硬编码的,不过如果数据适合您的需要,您可以在运行时加载数据
using System;
using System.Collections.Generic;
class ContentDetails
{
// Maps id to RefValue.
private static readonly Dictionary<string, string> idValueDictionary =
new Dictionary<string,string>()
{
{ "00", "14" },
{ "01", "18" },
{ "XX", "00" }
};
// Maps RefValue to ViewName
private static readonly Dictionary<string, string> valueNameDictionary =
new Dictionary<string,string>()
{
{ "00", "Menu" },
{ "14", "Menu" },
{ "18", "Topic" }
};
// Private constructor. Use GetContentDetails factory method.
private ContentDetails(string refValue, string viewName)
{
this.RefValue = refValue;
this.ViewName = viewName;
}
// Gets the RefValue.
public string RefValue
{
get;
private set;
}
// Gets the ViewName.
public string ViewName
{
get;
private set;
}
// Creates a new ContentDetails from the specified id.
public static ContentDetails GetContentDetails(string id)
{
// Extract key from id.
string key = id.Substring(2,2);
// If key not in dictionary, use the default key "XX".
if (!idValueDictionary.ContainsKey(key))
{
key = "XX";
}
// Get refValue and viewName from dictionaries.
string refValue = idValueDictionary[key];
string viewName = valueNameDictionary[refValue];
// Return a new ContentDetails object with properties initialized.
return new ContentDetails(refValue, viewName);
}
}
使用GetContentDetails
方法创建新的ContentDetails
后,您可以访问这两个属性以获取RefValue
和ViewName
。属性是只读的(private set
),因此在创建类实例后不能更改值(它是不可变的)。每次你有不同的id
要查找时,创建一个新的。该代码不会编译,你能发布你的实际代码吗。@AshBurlaczenko将返回类型“void”添加到方法中,它会。该代码不会编译,你能发布你的实际代码吗。@AshBurlaczenko添加返回类型“void”我认为map的想法听起来很好,但我一点也不确定如何实现这一点。你能不能就一个变量建议一下我该怎么做。谢谢。我认为地图的想法听起来很好,但我一点也不确定如何实施。你能不能就一个变量建议一下我该怎么做。谢谢