Sunday 15 March 2015

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


The error is simple, but I am not able to solve it. I hope people will help me on this

This is my aspx page code

  & asp: treeview id = "picturetree" runat = "server" showLines = "true" & gt; & Lt; SelectedNodeStyle font-bold = "True" /> & Lt; Nodesety image URL = "~ / images / folder .jpg" /> & Lt; / ASP: TreeView & gt; & Lt; / TD & gt; & Lt; Td style = "width: auto; text-line: center;" Valign = "top" & gt; & Lt; ASP: Data List ID = "Pictures Infessor" Runat = "Server" width = "100%" cellpadding = "5" & gt; & Lt; ItemTemplate & gt; & Lt; H3 & gt; & Lt; ASP: label runat = "server" id = "fileNameLabel" text = '& lt;% # System.IO.Path.GetFilenameWithoutExtension (Eval ("name"))% & gt; & Gt; & Lt; / ASP: Labels & gt; & Lt; / H3 & gt; & Lt; ASP: Image Runat = "Server" id = "Picture" image URL = '& lt;% # PictureTree Solitated Value & amp; Evil ("name"). ToString ()% & gt; '/ & Gt; & Lt; Br / & gt; & Lt; Br / & gt; & Lt; / ItemTemplate & gt; & Lt; AlternatingItemStyle BackColor = "#E0E0E0" /> & Lt; / ASP: DataList & gt; & Lt; ASP: label runat = "server" id = "no picture infer message" font-italic = "true" visual = "fault" enabled widget = "falls" & gt; There are no pictures in the selected folder ... & lt; / Asp: Label & gt;   

This is my .cs code

  Private compressed object virtual image root = "~ / images / section /"; Protected Zero Page_Load (Object Sender, System.EventArgs E) {// On first page tour, select photo tree populate and select root node (Page.IsPostBack!) {PopulateTree (); PictureTree.Nodes [0] .select (); PictureTree_SelectedNodeChanged (PictureTree, EventAge.APT); }} Private zeros populateTree () {// specified virtualImageRoot DirectoryInfo rootFolder = the new DirectoryInfo (Server.MapPath (VirtualImageRoot)) populate the tree based on subfolders; Treeoid root = Adnode and Descendants (rootfolder, empty); // TreeView add root in PictureTree.Nodes.Add (root); } Private TreeNode AddNodeAndDescendents (DirectoryInfo Folder, TreeNode parentNode) {// Add TreeNode, display the name of the folder and storing the full path of the folder as the value string string as VirtualFolderPath; If ((parentNode == faucet)) {virtualFolderPath = VirtualImageRoot; } Else {virtualFolderPath = (parentNode.Value + (folder name + "/")); } TreeNode node = New Treeode (folder name, virtual folder path); // Reorder from subfolders directoryInfo [] of this folder [subFolders = folder.GetDirectories (); Foreign currency (directory subfolders in subfoldersInfo subfolders) {TreeNode children = add-nodes and desendents (subfolders, nodes); Node.ChildNodes.Add (child); } Return node; // New TreeNode} Zero PictureTree_SelectedNodeChanged (Object Sender, System.EventArgs e) Protected Return {// Refresh DataList whenever any new node DisplayPicturesInFolder (PictureTree.SelectedValue) is selected; } Personal Zero DisplayPicturesInFolder (string virtualFolderPath) {// Security Check: Sure that FolderPath starts with VirtualImageRoot and does not include any ".." if (virtualFolderPath.StartsWith (VirtualImageRoot) || (virtualFolderPath.IndexOf (" .. ") & Gt; = 0))) {New App Exception (" Attempt to see a folder outside the public image folder! "); } // Get information about the files in the specified folder DirectoryInfo folder = new DirectoryInfo (Server.MapPath (virtualFolderPath)); FileInfo [] fileList = folder.GetFiles (); Picture Infolder Datasource = filelist; PicturesInFolder.DataBind (); // If there is no picture in the folder, then no Display ImageFolder Message LabelImage Infolder. Visible = (Image Infolder.im.tex; 0); There are no pictures. }}   

I get an error in the private contact object virtual image root = "~ / Images / Departments /";

This virtual image root is an object that can be started with only one zero of a reference type other than a single string field

How can I resolve this thank you in advance

const supports very few types; If you do not have one of them, then in the normal case you should instead use to read instead: < Pre> private static readonly object virtual image root = "~ / images / section /";

However, this too is not clear why it is not in your case string :

 < Code> Private constring virtualImageRoot = "~ / images / section /";    

No comments:

Post a Comment