C# 如何将方法从一种类型转换为另一种类型
我正在使用ACCESS 2010上的VS2010为医院构建windows窗体应用程序。 我想创建一个将生成C# 如何将方法从一种类型转换为另一种类型,c#,.net,C#,.net,我正在使用ACCESS 2010上的VS2010为医院构建windows窗体应用程序。 我想创建一个将生成患者的方法: public string GetCurrentDate() { DateTime currentDate = new DateTime(); string todaydate = currentDate.ToShortDateString().ToString(); return todaydate; } public Int32 MalePatie
患者的方法
:
public string GetCurrentDate()
{
DateTime currentDate = new DateTime();
string todaydate = currentDate.ToShortDateString().ToString();
return todaydate;
}
public Int32 MalePatient()
{
return 1;
}
public Int32 FemalePatient()
{
return 2;
}
public Int32 RadomNum()
{
Random randomNumer = new Random();
Int32 number = randomNumer.Next(0, 1000);
return number;
}
public string GeneratePatientNumber();
{
patientNumber = Convert.ToString(GetCurrentDate())"+ "-" ????
}
此格式的数字:
"Date of Enrollment" + "PatientGender" +"randomNumber"
Example of Male patientNumber of type string:2012-1-10
Example of Female patientNumber of type string:2012-2-8
Female=2
Male=1
但是我对
GeneratePatientNumber
方法有一个问题,该方法应该将另一个方法转换为字符串,并像上面的两个示例那样将它们连接起来。string.Join
可能是您要找的:
public string GeneratePatientNumber()
{
return string.Join(
"-",
Convert.ToString(GetCurrentDate()),
GetGenderCode().ToString(),
RadomNum().ToString());
}
当然,您需要定义
GetGenderCode
方法,以便它根据患者的性别返回预期的代码。我认为您应该使用以下方法:
public string GetCurrentDate()
{
DateTime currentDate = DateTime.Now;
string todaydate = currentDate.ToShortDateString().ToString();
return todaydate;
}
你可以称之为
var generator = new PatientNumberGenerator();
string patientNumber = generator.GeneratePatientNumber(Gender.Female);
小心随机类。每次创建新实例时,它都会根据当前日期和时间生成一个种子值。这将确定生成的第一个随机数。如果您多次快速调用RanomNum()
,它可能会生成相同的随机数,因为您电脑中的内部时钟分辨率有限。这就是我将其声明为静态成员的原因。这只会创建一次随机的\u
一点重构:
public enum PatientGender
{
Male = 1,
Female
}
public int RandomNum()
{
return new Random().Next(0, 1000);
}
var patientNumber = string.Format("{0}-{1}-{2}",
DateTime.UtcNow.ToString("yyyy-MM-dd"),
(int)PatientGender.Male,
RandomNum());
提供以下输出:
2012-01-11-1-586
您的patientNumer示例与定义不匹配。患者编号不应该看起来像“2012-1-10 1 2234”吗?它像:YYYY-GENDER-RANDOMNUM;您可以编写“字符串类型的男性患者编号示例:”并显示日期,而不是完整的患者编号。请编辑示例。示例我的意思是:对于男性患者:2012-01-12-005对于今天登记的患者,但在感谢了精彩的帮助之后,我的代码现在很好不是2012-01-12-1-005吗?(性别缺失。)@Oliver Jacot-Descombes.这是greathow我可以将随机数从9位限制到4位。例如从4-16-2012-11-1238822431到4-16-2012-11-1238
Random
有三个下一个方法的重载Next()
返回范围为[0..2147483647]的数字<代码>下一个(最大)
=>[0..max-1]<代码>下一步(最小值,最大值)
=>[min..max-1]\u random.Next(10000)
返回0到9999之间的数字。最后\u random.Next(10000)。ToString(“0000”)
生成“0000”
和“9999”
之间的字符串,该字符串的长度始终为4,必要时用'0'
填充。
2012-01-11-1-586