gpt4 book ai didi

c# - 错误 : A const field of a reference type other than string can only be initialized with null

转载 作者:太空宇宙 更新时间:2023-11-03 17:17:27 25 4
gpt4 key购买 nike

错误很简单,但我无法解决这个问题,希望你们能帮助我

这是我的aspx页面代码

 <asp:TreeView ID="PictureTree" runat="server" ShowLines="True">
<SelectedNodeStyle Font-Bold="True" />
<NodeStyle ImageUrl="~/Images/Folder.jpg" />
</asp:TreeView>

</td>
<td style="width:auto;text-align:center;" valign="top">
<asp:DataList ID="PicturesInFolder" runat="server" Width="100%" CellPadding="5">
<ItemTemplate>
<h3><asp:Label runat="server" ID="FileNameLabel" Text='<%# System.IO.Path.GetFilenameWithoutExtension(Eval("Name")) %>'></asp:Label></h3>

<asp:Image runat="server" ID="Picture" ImageUrl='<%# PictureTree.SelectedValue & Eval("Name").ToString() %>' />
<br /><br />
</ItemTemplate>
<AlternatingItemStyle BackColor="#E0E0E0" />
</asp:DataList>
<asp:Label runat="server" ID="NoPicturesInFolderMessage" Font-Italic="True" Visible="False" EnableViewState="False">
There are no pictures in the selected folder...
</asp:Label>

这是我的 .cs 代码

private const object VirtualImageRoot = "~/Images/Departments/";


protected void Page_Load(object sender, System.EventArgs e)
{
// On the first page visit, populate the photo tree and select the root node
if (!Page.IsPostBack)
{
PopulateTree();
PictureTree.Nodes[0].Select();
PictureTree_SelectedNodeChanged(PictureTree, EventArgs.Empty);
}
}

private void PopulateTree()
{
// Populate the tree based on the subfolders of the specified VirtualImageRoot
DirectoryInfo rootFolder = new DirectoryInfo(Server.MapPath(VirtualImageRoot));
TreeNode root = AddNodeAndDescendents(rootFolder, null);
// Add the root to the TreeView
PictureTree.Nodes.Add(root);
}

private TreeNode AddNodeAndDescendents(DirectoryInfo folder, TreeNode parentNode)
{
// Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value...
string virtualFolderPath;
if ((parentNode == null))
{
virtualFolderPath = VirtualImageRoot;
}
else
{
virtualFolderPath = (parentNode.Value
+ (folder.Name + "/"));
}
TreeNode node = new TreeNode(folder.Name, virtualFolderPath);
// Recurse through this folder's subfolders
DirectoryInfo[] subFolders = folder.GetDirectories();
foreach (DirectoryInfo subFolder in subFolders)
{
TreeNode child = AddNodeAndDescendents(subFolder, node);
node.ChildNodes.Add(child);
}
return node;
// Return the new TreeNode
}

protected void PictureTree_SelectedNodeChanged(object sender, System.EventArgs e)
{
// Refresh the DataList whenever a new node is selected
DisplayPicturesInFolder(PictureTree.SelectedValue);
}

private void DisplayPicturesInFolder(string virtualFolderPath)
{
// Security check: make sure folderPath starts with VirtualImageRoot and doesn't include any ".."
if ((!virtualFolderPath.StartsWith(VirtualImageRoot)
|| (virtualFolderPath.IndexOf("..") >= 0)))
{
throw new ApplicationException("Attempting to view a folder outside of the public image folder!");
}
// Get information about the files in the specified folder
DirectoryInfo folder = new DirectoryInfo(Server.MapPath(virtualFolderPath));
FileInfo[] fileList = folder.GetFiles();
PicturesInFolder.DataSource = fileList;
PicturesInFolder.DataBind();
// If there are no pictures in the folder, display the NoPicturesInFolderMessage Label
PicturesInFolder.Visible = (PicturesInFolder.Items.Count > 0);
NoPicturesInFolderMessage.Visible = !PicturesInFolder.Visible;
}
}

我在私有(private)常量对象 VirtualImageRoot = "~/Images/Departments/"中出错;

即VirtualImageRoot是一个对象,除string以外的引用类型的const字段,只能用null初始化

请问如何解决这个问题

最佳答案

const 支持的类型非常少;如果您的不是其中之一,那么在一般情况下您应该改用static readonly:

private static readonly object VirtualImageRoot = "~/Images/Departments/";

但是,非常不清楚为什么在您的情况下这不是字符串:

private const string VirtualImageRoot = "~/Images/Departments/";

关于c# - 错误 : A const field of a reference type other than string can only be initialized with null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17234402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com