将值分配给类的实例变量。
我有以下具有以下属性的类。它还包含两个静态属性。
public class Levels
{
public static string LevelWeek { get; private set; }
public static string LevelHours {get; private set; }
public int Start { get; set; }
public int Length { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string LevelType { get; set; }
}
我正在为方法中的属性赋值,如下所示:
Levels level = new Levels
{
Start = Convert.ToInt32(Request["start"]),
Length = Convert.ToInt32(Request["length"]),
Name = Request[("columns[3][search][value]")],
Address = Request[("columns[4][search][value]")],
LevelType = Request[("columns[6][search][value]")]
// I want to achieve this inside the object initialisation
if (string.IsNullOrEmpty(LevelType) {
LevelWeek = "Not set";
LevelHours = "Not set";
}
else {
if (LevelType.Equals("Junior") {
LevelWeek = LevelType;
} else
{
LevelHours = "Senior";
}
}
};
然后,我将对象level
传递给这样的方法。
AssignDetails(level);
我使用LevelWeek
和LevelHours
作为静态的原因是我想保留每个变量,即使在方法的第二次命中时不满足条件。
请注意,我将访问方法LevelWeek
中的变量LevelHours
和AssignDetails
。有人可以帮帮我吗?
EDITED
目前我有这个:
public static string levelTypeCountry { get; private set; }
public static string levelTypeWeek { get; private set; }
public static string RateCountry { get; private set; }
public static string RateWeek { get; private set; }
public ActionResult IndexPagination()
{
int start = Convert.ToInt32(Request["start"]);
int length = Convert.ToInt32(Request["length"]);
string name = Request["search[value]"];
string address = Request[("columns[3][search][value]")];
string levelType = Request[("columns[6][search][value]")];
string rateType = Request[("columns[7][search][value]")];
if (string.IsNullOrEmpty(levelType))
{
levelTypeCountry = "";
levelTypeWeek = "";
}
else
{
if (CheckDate(levelType))
{
levelTypeWeek = levelType;
}
else
{
levelTypeCountry = levelType;
}
}
if (string.IsNullOrEmpty(rateType))
{
RateCountry = "";
RateWeek = "";
}
else
{
if (CheckDate(rateType))
{
RateWeek = rateType;
}
else
{
RateCountry = rateType;
}
}
var items = AssignDetails(start, length, name, address, levelTypeWeek, levelTypeCountry, RateWeek, RateCountry);
return items;
}
函数CheckDate
只检查levelType
是否为日期。如果为true,则返回true,否则为false。
rateType
和levelType
会返回不同的值。有时候,他们会返回一个日期,有时会像#34; FRA"。
答案 0 :(得分:0)
我想您只需要setValue
和2 getValue
个方法:
public class Levels {
private string LevelWeek {
get;
set;
}
private string LevelHours {
get;
set;
}
public int Start {
get;
set;
}
public int Length {
get;
set;
}
public string Name {
get;
set;
}
public string Address {
get;
set;
}
public string LevelType {
get;
set;
}
public void setValue() {
if (string.IsNullOrEmpty(LevelType) {
this.LevelWeek = "Not set";
this.LevelHours = "Not set";
} else {
if (LevelType.Equals("Junior") {
this.LevelWeek = LevelType;
} else {
this.LevelHours = "Senior";
}
}
}
public string getLevelWeek() {
return this.LevelWeek;
}
public string getLevelHours() {
return this.LevelHours;
}
}
}
}
声明对象时:
var level = new Levels {
Start = Convert.ToInt32(Request["start"]),
Length = Convert.ToInt32(Request["length"]),
Name = Request[("columns[3][search][value]")],
Address = Request[("columns[4][search][value]")],
LevelType = Request[("columns[6][search][value]")]
}
// set value to LevelWeek / LevelHours
level.setValue();
// In AssignDetails function : get the value of LevelWeek and LevelHours:
var levelweek = level.getLevelWeek();
var levelhours = level.getLevelHours();