小编Pos*_*Guy的帖子

Nullable Enum投射到Int16

我一直收到错误System.InvalidCastException:指定的强制转换无效.在运行时.数据库中的RewardJoinType可以为null.

这是强制转换失败的代码行:

c.rewardJoinType = (RewardJoinType)reader.GetInt16();
Run Code Online (Sandbox Code Playgroud)

'reader.GetInt16()'引发了一个'System.InvalidCastException'类型的异常short {System.InvalidCastException}

在课堂上,我有以下几行代码:

private RewardJoinType? rewardJoinType; 
Run Code Online (Sandbox Code Playgroud)

......其他一些代码

c.rewardJoinType = (RewardJoinType?)reader.GetInt16();
Run Code Online (Sandbox Code Playgroud)

......其他一些代码

conn.AddParam("@rewardJoinType", (int?)rewardJoinType);
Run Code Online (Sandbox Code Playgroud)

......其他一些代码

public RewardJoinType? RewardJoinType
{
     get { return rewardJoinType; }
     set { rewardJoinType = value; }
}
Run Code Online (Sandbox Code Playgroud)

这是枚举本身

public enum RewardJoinType
{
    Auto,
    Manual
}
Run Code Online (Sandbox Code Playgroud)

是因为默认情况下枚举是Int32,即使我已经可以为空,它也无法转换为null Int16?

我们在读者中处理了Int16的DBNull:

    public short GetInt16()
    {
        columnIndex++;
        return reader.IsDBNull(columnIndex) ? (short)0 : reader.GetInt16(columnIndex);
    }
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
1534
查看次数

在下拉列表中获取最后一项的更简单方法

必须有一种更简单的方法来完成我在这里想到的事情:

int lastDayInList = ddlBirthDay.Items.IndexOf(ddlBirthDay.Items[ddlBirthDay.Items.Count -1]);
Run Code Online (Sandbox Code Playgroud)

asp.net

0
推荐指数
1
解决办法
4091
查看次数

如何判断某人是否已下载最新代码

有没有一种方法可以告诉我(除了在SVN日志中...因为它只显示我有人提交)最近更新了以便我知道他们已经下载了我的代码?我正在专门研究更新(不是在SVN日志中可以看到的提交).

svn version-control tortoisesvn

0
推荐指数
1
解决办法
245
查看次数

如何在转换十进制之前检查有效值

我假设如果total为null,这将会爆炸(我不记得小数是否在开头被初始化为零或零)

public int SomePropertyName
{
    get { return Convert.ToInt32(Decimal.Round(total)); }
}
Run Code Online (Sandbox Code Playgroud)

那么我应该检查null还是> 0?

c#

0
推荐指数
1
解决办法
2792
查看次数

重写多个if语句

我觉得这是一堆你知道我的意思.它的工作原理,但我觉得我在页面生命周期(加载和回发)方面做得太过分了,甚至我在每个if语句中都有冗余.

这是怎么回事:

  1. 在非常页面加载时调用此方法(无论是回发还是其他)
  2. 如果用户提交表单,则会减少他们的totalPoints(这些radiobuttons下面有一个按钮,允许他们提交和声明积分).

因此,我也在用户声明点(提交)之后立即调用此方法,该点将从下一次的总数中删除这些点.因此,基于其帐户中的总分,我需要在页面从上次提交刷新后启用/禁用这些单选按钮

private void SetPointsOptions()
{
    int totalPoints = customer.TotalPoints;

    rbn200Points.Text = "200 pts";
    rbn250Points.Text = "250 pts";
    rbn400Points.Text = "400 pts";
    rbn500Points.Text = "500 pts";
    rbn600Points.Text = "600 pts";

    // clear state of radio buttons & disable submit
    if (totalPoints < 200)
    {
        rbn200Points.Enabled = false;
        rbn250Points.Enabled = false;
        rbn400Points.Enabled = false;
        rbn500Points.Enabled = false;
        rbn600Points.Enabled = false;

        rbn200Points.Checked = false;
        rbn250Points.Checked = false;
        rbn400Points.Checked = false;
        rbn500Points.Checked = false;
        rbn600Points.Checked = false;

        btnClaimRewardPoints.Enabled = false; …
Run Code Online (Sandbox Code Playgroud)

asp.net optimization user-interface control-flow

0
推荐指数
1
解决办法
623
查看次数

数组比词典更高效

我的代码中有以下方法.我的老板问"你为什么要使用字典",你可以使用数组,效率更高:

    public static Dictionary<string, string> GetListOfMonths()
    {
        Dictionary<string, string> months = new Dictionary<string, string>();

        months.Add("1", "Jan");
        months.Add("2", "Feb");
        months.Add("3", "Mar");
        months.Add("4", "Apr");
        months.Add("5", "May");
        months.Add("6", "Jun");
        months.Add("7", "Jul");
        months.Add("8", "Aug");
        months.Add("9", "Sep");
        months.Add("10", "Oct");
        months.Add("11", "Nov");
        months.Add("12", "Dec");

        return months;
    }
Run Code Online (Sandbox Code Playgroud)

我使用它来代码重用.所以我可以绑定到应用程序各个部分的一些月下拉菜单.

c#

0
推荐指数
2
解决办法
2103
查看次数

为什么我要远离DataSet,有哪些替代方案?

我在这里找到了一个有趣的讨论

http://www.linkedin.com/groupAnswers?viewQuestionAndAnswers=&gid=43315&discussionID=12708606&goback=.anh_43315

引用:

DataSet是用于编码数据层的业余解决方案......停止使用它们并学习代码!:)

您对DataSet的看法是什么?只是简化自定义类并与之合作?还有什么其他选择?

c# asp.net dataset

0
推荐指数
1
解决办法
659
查看次数

枚举字符串C#和JavaScript

在我的.aspx中我定义了以下javascript变量:

var action = <%=ProdView %>
Run Code Online (Sandbox Code Playgroud)

在代码隐藏中,它返回一个自定义枚举值:

protected ProductView ProdView { get; private set; }
Run Code Online (Sandbox Code Playgroud)

我想这会自动转换为javascript中的字符串?看起来没有,因为我得到运行时错误"Item is not defined",其中Item是值ProdView.Item.最终我希望动作的值为"Item"作为值.

这是Enum:

  public enum ProductView
  {
   Product,
   Item
  }
Run Code Online (Sandbox Code Playgroud)

javascript c#

0
推荐指数
1
解决办法
794
查看次数

字符串未定义

我在.aspx的代码隐藏中有以下属性:

    protected string CurrentProductView
    {
        get
        {
            string viewName = string.Empty;
            viewName = Enum.GetName(typeof(ProductView), currentProdView);
            return viewName;
        }
    }
Run Code Online (Sandbox Code Playgroud)

在我的.aspx中我有一些试图引用这个字符串的Javascript:

$(document).ready(function ()
{
    var action = <%=CurrentProductView %>

             $("#recommendations").load("recommendationsHandler.ashx?action=" + action + "item&csid=" + csid + "&productID=" + productID, function()
        {
            $("#recommendationsView").show();
        });
});
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,我得到"项目未定义".

当我调试这个时,我肯定会看到一个字符串返回viewName.那么为什么它会抱怨如果一个字符串回来?!?!

javascript c# jquery

0
推荐指数
1
解决办法
1029
查看次数

检查bool是否正在检查对象

我在JS中创建了这个实用工具方法:

function IsAuthenticated(userID)
{
    var isAuthed = false;

    if (userID.length == 0)
        return false;

    // more logic
    if(SomeLogic)
       isAuthed = true;

    return isAuthed;
}
Run Code Online (Sandbox Code Playgroud)

当我运行这样的东西时,我得到一个对象而不是键入bool:

if(IsAuthenticated)
    //code here
Run Code Online (Sandbox Code Playgroud)

我想我需要把它变成一个布尔?

javascript

0
推荐指数
1
解决办法
113
查看次数