给出以下代码:
const myStr codesArr[] = {"AA","BB", "CC"};
Run Code Online (Sandbox Code Playgroud)
myStr是一个包装类char*.我需要遍历数组中的所有项目,但我不知道项目的数量.我不想定义一个const代表大小的值(在本例中为3)
使用类似的东西是否安全:
const int size = sizeof(codesArr) / sizeof(myStr);
Run Code Online (Sandbox Code Playgroud)
该怎么办?
我正在使用第三方Web服务客户端(使用"添加服务引用"创建)以检索某些数据.
在使用适当的数据填充Web服务对象后,我们需要向标头添加一些数据(加密密码和一些其他预定义数据)
然后,我们使用标准.net序列化发送到Web服务的每个请求XmlSerializer.
但是,在序列化的结果中,我没有看到请求的标头.我已经搜索了很长时间,也找不到任何方法来"打印"它们.
这是一些示例代码:
Ibooking proxy = new BookingManager();
/* Init proxy Data...*/
GetAvailabilityRequest request = new GetAvailabilityRequest();
/*Fill more data on the request...*/
GetAvailabilityResponse response = proxy.GetAvailability(request); //Send request to the web service
var xmlString2 = response.Serialize(); //only body, no headers in the XML
/* Extension class to Serialize any object */
public static class ExtensionUtil
{
public static string Serialize<T>(this T value)
{
try
{
XmlSerializer xmlserializer = new XmlSerializer(typeof(T));
var stringWriter = new StringWriter(); …Run Code Online (Sandbox Code Playgroud) 让我先解释一下情况 - 假设我有4个日期:BS,BE,PS,PE(S表示开始,E表示结束).我需要知道在给定这些日期时有多少天过度研磨.例如:BE-05.01,BS-10.01,PS-03.01,PE-07.01结果为:3(05.01,06.01和07.01重叠)
我写了下面的代码,但它似乎很乱,我想检查是否有更简单的方法来做到这一点:
private static double GetNumOfOverLappingDays(DateTime BS, DateTime BE, DateTime PS, DateTime PE)
{
//case 1:
// |--- B ---|
// |----P ---|
//case 2:
// |--- B ---|
// | --- P --- |
//case 3:
// |--- B ---|
// | --- P ---- |
//case 4:
// |--- B ---|
// | - P - |
//case 5:
// |--- B ---|
// | -------- P -------- |
double days = -1;
bool isNotOverLap = …Run Code Online (Sandbox Code Playgroud) 我写了一个小的 winforms 应用程序来搜索磁盘上的文件(就问题而言,哪个文件并不那么重要)。问题是它甚至可以是 100,000 个左右的文件。所以这个操作需要时间。
我想要实现的是将搜索操作作为异步操作进行,而不是阻塞 UI 线程,这样表单就不会卡住。
我可以用 backgroundWorker 做到这一点,但由于某种原因不能用 async\await 机制。
这是我的代码:
private async void button_FindFiles_Click(object sender, EventArgs e)
{
await SearchFilesUtil.SearchPnrFilesAsync(this.textBox_mainDirectory.Text);
MessageBox.Show("After SearchPnrFilesAsync");
}
public async static Task SearchPnrFilesAsync(string mainDir)
{
foreach (string file in Directory.EnumerateFiles(mainDir, ".xml", SearchOption.AllDirectories))
{
var fileContenet = File.ReadAllText(file);
var path = Path.Combine(@"C:\CopyFileHere", Path.GetFileName(file));
using (StreamWriter sw = new StreamWriter(path))
{
await sw.WriteAsync(fileContenet);
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么 UI 线程卡住了而不MessageBox立即显示?我错过了什么?
在我的代码中,我可以获得两种类型的表示dateTime的字符串:
1."2013-09-05T15:55"
2."09-05T19:10"
如何将其转换为有效的DateTime?
我尝试了以下代码,但它引发了第二种格式的异常:
String departureDateStr = "09-05T19:10";
DateTime dt = Convert.ToDateTime(departureDateStr);
Run Code Online (Sandbox Code Playgroud)
如何将第二种类型的字符串转换为有效的DateTime?我需要某种字符串操作吗?
谢谢,
阿米尔
我有2个表需要更新:
表A包括:ID,personName,日期,状态
表B包括:PersonID,日期,状态
对于A中的每一行,B中可以有多行具有相同的personID
我需要"循环"来自状态= 2的A的所有结果,并将日期和状态更新为1.
此外,对于A中status = 2的每一行,我需要更新B中具有相同personID的所有行(即A.ID == B.PersonID) - 我还需要将日期和状态更新为1.
所以基本上,如果我以编程方式(或算法)执行此操作,那就是这样的:
Foreach(var itemA in A)
If (itemA.status = 2)
itemA.status to 1
itemA.date = GetDate()
foreach(var itemB in B)
if(itemB.PersonID == itemA.ID && itemB.status != 2 )
Change itemB.status to 1
Change itemB.date = GetDate()
Run Code Online (Sandbox Code Playgroud)
我知道如何使用以下sql语句更新B中的所有行:
UPDATE
B
SET
status = 1,
date = GETDATE()
FROM
B
INNER JOIN
A
ON
B.PersonID = A.ID
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何更新表A,因为更新语句中不能有多个表
谢谢你的帮助
如何使用C#将"ThisIsMyTestString"转换为"This Is My Test String"?
有快速的方法吗?
我一直在想一个伪代码,但它复杂而丑陋:
String s = "ThisIsMyTestString";
List<String> strList = new List<String>();
for(int i=0; i < str->Length ; i++)
{
String tmp = "";
if (Char.IsUpper(str[i]))
{
tmp += str[i];
i++;
}
while (Char::IsLower(str[i]))
{
tmp += str[i];
i++;
}
strList .Add(tmp);
}
String tmp2 = "";
for (uint i=0 ; i<strList.Count(); i++)
{
tmp2 += strList[i] + " ";
}
Run Code Online (Sandbox Code Playgroud) 我有以下xml:
<?xml version="1.0" encoding="WINDOWS-1255"?>
<body>
<HotelBooking>
<customers>
<cust>
<custID>1111111</custID>
<title>MR</title>
<lastName>MASAREWH</lastName>
<firstName>AHMAD IRAKI</firstName>
</cust>
<cust>
<custID>22222222</custID>
<title>MRS</title>
<lastName>HAJ YAHYA IRAQI</lastName>
<firstName>HIMAT</firstName>
</cust>
</customers>
<Details>
<name>Dublin & South</name>
</Details>
</HotelBooking>
</body>
Run Code Online (Sandbox Code Playgroud)
当我尝试将它序列化为一个对象时,我得到一个例外. System.InvalidOperationException: There is an error in XML document
尝试使用notepad ++ XML插件工具编辑此XML后我明白问题是'&'char中的:
<name>Dublin & South</name>
Run Code Online (Sandbox Code Playgroud)
如果我不想更改xml本身(例如将'&'替换为'AND'或类似的东西),我在这里有什么选择?还有更多的字符可能会使我的序列化过程失败吗?
thx任何帮助!
我们正在将代码从C++升级到C#,而且很多地方我们正在编写字符串.例如,我们有类似的东西:
OurString.Format("amount = %0.2Lf", (long double)amount);
Run Code Online (Sandbox Code Playgroud)
如何将%0.2Lf转换为C#格式?我尝试了下面的代码,但它不一样
string formatString = String.Format("amount = {0}", (long double)amount));
Run Code Online (Sandbox Code Playgroud)
谢谢
是的,有点新,我不知道如何验证数组不为空。
我正在使用 React + Formik + yup + Material-ui
这是我创建的一个示例:
https://codesandbox.io/s/new-fire-29onf?file=/src/App.js
我尝试在validationSchema中只使用所需的方法:
validationSchema={Yup.object({ permissions : Yup.array().required('permission cant be empty') })}
Run Code Online (Sandbox Code Playgroud)
我尝试使用这样的测试方法添加我的功能:
validationSchema={Yup.object({ permission: Yup.array().test ('notEmptyArr', 'array is empty', (value) =>{ console.log(value); return value.length > 0; }) })}
Run Code Online (Sandbox Code Playgroud)
我还尝试向数组添加方法,如下所示:
Yup.addMethod(Yup.array, "notEmpty", function(message) { return this.test("notEmpty", message, function(arr) { return Boolean( arr.length > 0 ); }); });
Run Code Online (Sandbox Code Playgroud)
但这些都不适合我:(
如果我删除验证,我会看到 value.permission 确实是一个数组,带有值(如果选择)
我究竟做错了什么?
谢谢
我有(另一个)关于索引的问题。
我使用以下代码:
CREATE TABLE [dbo].[PnrDetails1](
[OId] [int] IDENTITY(1,1) NOT NULL ,
[file_name] [varchar](256) NOT NULL,
[gds_id] [int] NOT NULL,
[pnr_locator] [varchar](15) NOT NULL,
[first_cust_name] [varchar](50) NOT NULL,
[ticket_number] [varchar](20) NOT NULL,
[full_price] [decimal](18, 0) NOT NULL,
[currency_desc] [varchar](4) NOT NULL,
[user_name] [varchar](50) NOT NULL,
[save_time] [datetime] NOT NULL,
[update_time] [datetime] NOT NULL,
[clerk_id] [int] NOT NULL,
[isUpdated] [bit] NOT NULL,
[isDeleted] [bit] NOT NULL,
[pnr_file_id] [int] NOT NULL
) ON [PRIMARY]
ALTER TABLE [dbo].[PnrDetails1] ADD PRIMARY KEY CLUSTERED
(
[OId] …Run Code Online (Sandbox Code Playgroud)