Solved

Is a field but is used like a type

Posted on 2014-07-29
2
1,222 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 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