我有一个值为盒装(任意)数字类型或可以为空的数字类型.我需要检查该值是否为零.
基本上我有这个:
public bool IsZero(object value)
{
if (value == null)
return false;
//lifted from the mvc sources, so I guess it's exhaustive
HashSet<Type> numericTypes = new HashSet<Type>(new Type[] {
typeof(byte), typeof(sbyte),
typeof(short), typeof(ushort),
typeof(int), typeof(uint),
typeof(long), typeof(ulong),
typeof(float), typeof(double),
typeof(decimal)
});
Type type = Nullable.GetUnderlyingType(value.GetType()) ?? value.GetType();
if (!numericTypes.Contains(type))
return false;
// how do I get the value here ?
}
Run Code Online (Sandbox Code Playgroud)
我没有看到一种简单的方法来比较int值和int零,以及一个字节值为0的字节值.
我看到的一个解决方法是将正确键入的零值与每种类型相关联并检查,但是如果我最终需要一个"IsOne"方法,我想重用该解决方案.
我有列表,我想映射到列表.但是,没有编译器错误,但在运行时结果为null.获得泛型List<T>将帮助我使我的代码通用并重用于不同的对象.
private static List<T> GetIndexData(DataAdapter dataAdapter)
{
Type type = typeof(T);
List<T> result = new List<T>();
if (typeof(Category).IsAssignableFrom(typeof(T)))
{
var output = ItemStoreDataManager.GetAllCategoryNames(dataAdapter);
result = output as List<T>;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码结果为null.
internal static List<Category> GetAllCategoryNames(DataAdapter dataAdapter)
{
List<Category> result = new List<Category>();
...........
return result;
}
Run Code Online (Sandbox Code Playgroud)
请帮忙.
谢谢
这是来自使用2d数组的C#项目的一段代码.出于某种原因,我不明白我的程序编译完美,但在运行期间崩溃.
public class Tile_Info
{
public int id;
public Tile_Info(int _id)
{
id = _id;
}
}
class Program
{
public static void Main(string[] args)
{
int width = 20;
int height = 30;
Tile_Info[,] my_tile;
my_tile = new Tile_Info[width, height];
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
my_tile[x, y].id = 0;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据调试器,它是因为"对象引用未设置为对象的实例",但我很确定这就是我在这里所做的事情:my_tile = new Tile_Info[width, height];.
谁能告诉我哪里错了?感谢您的支持!
嗨我试图复制这个mysql查询
SELECT a.id, a.title, a.description, a.categories_id, c.name, d.title
FROM ads AS a
INNER JOIN locations AS b
ON a.locations_id = b.id
INNER JOIN areas AS c
ON b.areas_id = c.id
INNER JOIN categories AS d
ON a.categories_id = d.id
WHERE a.title LIKE '%mini%'
AND c.name = 'Fyn'
LIMIT 10
Run Code Online (Sandbox Code Playgroud)
这是在LINQ
var query = (from a in db.ads
join b in db.locations on a.locations_id equals b.id
join c in db.areas on b.id equals c.id
join d in db.categories on a.categories_id equals …Run Code Online (Sandbox Code Playgroud) 所述object.ToString()方法用于任何对象转换为可读文本的字符串.但是如果对象是null,它会抛出一个NullReferenceError.所以我想ToString()用扩展方法覆盖这个方法,如:
public static class StringExt
{
public string ToString(this object str)
{
if (str == null)
return System.Convert.ToString(str);
return str.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
但似乎这样从来没有覆盖ToString()的的Object类.
Myclass cls;
//doing something on the other hand.
cls.ToString();
Run Code Online (Sandbox Code Playgroud)
这里cls.ToString()总是使用Object.ToString()方法.有没有办法实现这个目标?
我有这样的代码
First name : <input type="text" name="txtFirstName" /> <br><br>
Last name : <input type="text" name="txtLastName" /> <br><br>
Full name : <input type="text" name="txtFullName" > <br><br>
Run Code Online (Sandbox Code Playgroud)
如果我在名字文本框中输入abc,在名字文本框中输入def,则结果应在全名文本框中显示为abcdef。这该怎么做?
在以下行"this.dgvReport.Invoke(delegate")中收到错误
"无法将匿名方法转换为'System.Delegate'类型,因为它不是委托类型"
public void FillProductGrid()
{
ProductSP productSP = new ProductSP();
DataTable dtbl = new DataTable();
string productname = "";
dtbl = productSP.StockReport(productname, this.cbxPrint.Checked);
this.dgvReport.Invoke(delegate
{
this.dgvReport.DataSource = dtbl;
});
}
Run Code Online (Sandbox Code Playgroud) 在 Angular 2 应用程序中,我有一个数据服务,它将 http observables 转换为 Promise,以便使用 async/await 的优点,如下所示:
async getCustomer(id: number): Promise<Customer> {
return await this._http.get<Customer>(`${this.serverUrl}/customer/${id}`).toPromise();
}
Run Code Online (Sandbox Code Playgroud)
这是可操作的,看起来和工作都很棒。
我之前的单元测试使用了MockBackend类似的东西
mockBackend.connections.subscribe(c => {
expect(c.request.url).toBe(`${serverUrl}/customer/${id}`);
let response = new ResponseOptions({ body: mockResponseBody });
expect(c.request.method).toBe(RequestMethod.Get);
c.mockRespond(new Response(response));
});
let actual = await service.getCustomer(id);
Run Code Online (Sandbox Code Playgroud)
然而,现在当我尝试这样的事情时
httpMock = TestBed.get(HttpTestingController);
// ... 1
let actual = await service.getCustomer(id);
// ... 2
Run Code Online (Sandbox Code Playgroud)
我陷入了先有鸡还是先有蛋的境地。在提供模拟请求之前,该getCustomer方法不会返回,并且在触发 http 调用之前我无法使用httpMock.expectOneor 。
因此,如果我将调用放入 [1],我会收到预期失败,如果我将其放入 [2],则会收到超时错误:(httpMock.matchhttpMock
有没有解决的办法?
我有这个存储过程
ALTER procedure [dbo].[editUserInfo]
(@userid int, @firstName varchar(50), @lastName varchar(50), @contact varchar(50), @address varchar(50), @info varchar(100), @username varchar(50), @password varchar(50), @rid int)
as
update dbo.userLogin
set u_firstName=@firstName, u_lastName=@lastName, u_contact=@contact, u_address=@address, u_info=@info, u_username=@username, u_password=@password, r_id=@rid
where u_id=@userid
Run Code Online (Sandbox Code Playgroud)
我有以下代码
当save按钮被点击以下事件trriggered
protected void saveBtn_Click(object sender, EventArgs e)
{
String i = Session["id"].ToString();
userInfo usr = new userInfo();
usr.setFirstName(firstNameTxt.Text);
usr.setLastName(lastNameTxt.Text);
usr.setAddress(addressTxt.Text);
usr.setContact(contactTxt.Text);
usr.setInfo(infoTxt.Text);
usr.setRole(rolebox.SelectedValue);
usr.setUserName(userNameTxt.Text);
usr.setPassword(passwordTxt.Text);
usr.setId(i);
userEditService usredit = new userEditServiceImp();
Boolean ok = usredit.editUserInfo(usr);
if (ok == true)
{
Response.Redirect("viewUser.aspx"); …Run Code Online (Sandbox Code Playgroud) 好的,所以我用我的PHP脚本构建了这个查询:
SELECT *
FROM sale_properties
WHERE advert_heading LIKE '%harnham%'
OR main_advert LIKE '%harnham%'
OR advert2 LIKE '%harnham%'
OR advert3 LIKE '%harnham%'
OR street LIKE '%harnham%'
OR district LIKE '%harnham%'
OR town LIKE '%harnham%'
OR county LIKE '%harnham%'
OR area LIKE '%harnham%'
OR postcode LIKE '%harnham%'
AND numeric_price >= 150000.00
AND numeric_price <= 152000.00
Run Code Online (Sandbox Code Playgroud)
现在,这确实给了我一些结果,但是,我希望基本上在数据库中搜索关键字并在价格范围内。上面的查询确实为我提供了关键字,但似乎忽略了大部分价格范围。
这看起来正确吗?还是可以更好地建造?