【问题标题】:Why does Visual Studio say my input string is not in a correct format?为什么 Visual Studio 说我的输入字符串格式不正确?
【发布时间】:2013-09-20 01:00:50
【问题描述】:

以下代码:

string year = textBoxYear.Text;
string award = comboBoxAwards.Text;
string cat = comboBoxCategory.Text;
string title = textBoxTitle.Text;
string author = textBoxAuthor.Text;
string kindleASIN = textBoxKindleASIN.Text;
string hardboundASIN = textBoxHardboundASIN.Text;
string paperbackASIN = textBoxPaperbackASIN.Text;
string imgSrc = getImgSrc(kindleASIN, hardboundASIN, paperbackASIN);
string newRec = // Keeping year as int for LINQ querying (where year > or < such and such), and yearDisplay spans two years for some awards
    string.Format(
        "new BookClass{Award=\"{0}\", Year={1}, YearDisplay=\"{1}\", Category=\"{2}\", Title=\"{3}\", Author=\"{4}\", KindleASIN=\"{5}\", HardboundASIN=\"{6}\", PaperbackASIN=\"{7}\", ImgSrc=\"{8}\"},", award, year, cat, title, author, kindleASIN, hardboundASIN, paperbackASIN, imgSrc);

...使用这些数据:

year = "2013"
award = "Hugos"
cat = "Best Novel"
title == "Redshirts"
author == "John Scalzi"
kindleASIN == "B0079XPUOW"
hardboundASIN == "0765316994"
paperbackASIN == "0765334798"
imgSrc == "http://images.amazon.com/images/P/B0079XPUOW.01.MZZZZZZZ"

...在分配给 imgSrc 时死了,说:“System.FormatException 未处理 H结果=-2146233033 Message=输入字符串格式不正确".

...并且“疑难解答提示”说,“将字符串转换为 DateTime 时,在将每个变量放入 DateTime 对象之前解析字符串以获取日期。

但我没有将字符串转换为 DateTime...可能是什么问题?

【问题讨论】:

  • 你能告诉我们getImgSrc()吗?
  • getImgSrc 长什么样子?

标签: c# visual-studio


【解决方案1】:

不是替换标记的花括号搞砸了

new BookClass{Award=\"{0}\", Year={1}, YearDisplay=\"{1}\" 这里--^

您需要通过将您使用的任何{} 加倍来转义它们,它们将在Format 函数处理字符串后显示为单个花括号。

string.Format(
        "new BookClass{{Award=\"{0}\", Year={1}, YearDisplay=\"{1}\", Category=\"{2}\", Title=\"{3}\", Author=\"{4}\", KindleASIN=\"{5}\", HardboundASIN=\"{6}\", PaperbackASIN=\"{7}\", ImgSrc=\"{8}\"}},", award, year, cat, title, author, kindleASIN, hardboundASIN, paperbackASIN, imgSrc);

【讨论】:

  • 不错的皮卡!完全错过了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多