繁体   English   中英

为什么Visual Studio说我的输入字符串格式不正确?

[英]Why does Visual Studio say my input string is not in a correct format?

如下代码:

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 HResult = -2146233033消息=输入字符串的格式不正确 ”。

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

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

不是替换令牌的花括号将其弄乱了

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

您需要通过将它们加倍来转义使用的任何{} ,它们在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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM