簡體   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