Solved

Is a field but is used like a type

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

864 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now