Solved

Is a field but is used like a type

Posted on 2014-07-29
2
1,207 Views
Last Modified: 2014-07-29
I have taken a code snippet and am trying to mold it to my needs but am getting errors early on.
It should be a simple get file from A and save file to B process.
But I am getting these errors
'HubbleDocs.GetFile.copyService' is a 'field' but is used like a 'type'      
and
A field initializer cannot reference the non-static field, method, or property 'HubbleDocs.GetFile.fieldInfo'      

Here is the code in full, much appreciated if someone could get me over the line on this please

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;

namespace HubbleDocs
{
    public class GetFile
    {

        string webUrl = "http://localhost:1000";
        FileCopy.Copy copyService = new FileCopy.Copy();
        copyService.Url = webUrl +"/_vti_bin/copy.asmx";
         NetworkCredential m_credentials = new NetworkCredential(UserId, Pwd, Domain)
             copyService.Credentials = m_credentials;

        //Source and Destination Document URLs
         string sourceUrl = "http://localhost:1000/Shared Documents/Sample.doc";
         string destinationUrl = "C:\\Documents\\Sample.doc";

        //Variables for Reading metadata’s of a document
         FileCopy.FieldInformation fieldInfo = new FileCopy.FieldInformation();
         FileCopy.FieldInformation[] fieldInfoArray = { fieldInfo };
         FileCopy.CopyResult cResult1 = new FileCopy.CopyResult();
         FileCopy.CopyResult cResult2 = new FileCopy.CopyResult();
         FileCopy.CopyResult[] cResultArray = { cResult1, cResult2 };

        

        //Receive a Document Contents  into Byte array (filecontents)
         byte[] fileContent; // no need to initialize the GetItem takes care of that.
         copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContents);

        //Create a new file and write contents to that document
         FileStream fStream = new FileStream(destinationUrl, FileMode.Create, FileAccess.ReadWrite);
        fStream.Write(fileContents, 0, fileContents.Length);
        fStream.Close();

    }
}

Open in new window

0
Comment
Question by:QPR
2 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40227965
The error happen because you can only use fields/properties or method in your class declaration. Just create a method CopyFile for your code, thus your class should look like:
public class GetFile
{

    string webUrl = "http://localhost:1000";
    public void CopyFile()
    { 
        FileCopy.Copy copyService = new FileCopy.Copy();
        copyService.Url = webUrl +"/_vti_bin/copy.asmx";
         NetworkCredential m_credentials = new NetworkCredential(UserId, Pwd, Domain)
             copyService.Credentials = m_credentials;

        //Source and Destination Document URLs
         string sourceUrl = "http://localhost:1000/Shared Documents/Sample.doc";
         string destinationUrl = "C:\\Documents\\Sample.doc";

        //Variables for Reading metadata’s of a document
         FileCopy.FieldInformation fieldInfo = new FileCopy.FieldInformation();
         FileCopy.FieldInformation[] fieldInfoArray = { fieldInfo };
         FileCopy.CopyResult cResult1 = new FileCopy.CopyResult();
         FileCopy.CopyResult cResult2 = new FileCopy.CopyResult();
         FileCopy.CopyResult[] cResultArray = { cResult1, cResult2 };

        

        //Receive a Document Contents  into Byte array (filecontents)
         byte[] fileContent; // no need to initialize the GetItem takes care of that.
         copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContents);

        //Create a new file and write contents to that document
         FileStream fStream = new FileStream(destinationUrl, FileMode.Create, FileAccess.ReadWrite);
        fStream.Write(fileContents, 0, fileContents.Length);
        fStream.Close();
    }
}

Open in new window

0
 
LVL 29

Author Closing Comment

by:QPR
ID: 40228007
Perfect thanks. Stand by for more beginner questions :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question