Solved

Is a field but is used like a type

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

15 Experts available now in Live!

Get 1:1 Help Now