Trying to construct a helper class that will return an arraylist, but I'm getting the following error, having to do with the xml document I need to create:
Util.oDocument': cannot declare instance members in a static class
I think I understand why you wouldn't want to create a new xmldoc object each time this method gets called, but I need that doc in there for the functionality. How should I be approaching this?
using System;
using System.Collections;
using System.Xml;
public static class Util
{
public static ArrayList multipleArtistList(string artistName)
{
XmlDocument oDocument = new XmlDocument();
string uri = "http://api.leoslyrics.com/api_search.php?auth=duane&artist=" + artistName;
oDocument.Load(uri);
XmlNodeList results = oDocument.GetElementsByTagName("name");
ArrayList artistList = new ArrayList();
for (int i = 0; i < results.Count; i++)
{
if (!artistList.Contains(results[i].InnerText))
{
artistList.Add(results[i].InnerText);
}
}
return artistList;
}
}
This error here:
Util.oDocument: cannot declare instance members in a static class
means that you've declared oDocument outside of the method.
There's nothing wrong with the code you posted, in fact the error and the code contradict each other.
Make sure that oDocument is declared inside the method. If you want to declare it as a field, make sure to give it the static modifier, like so:
public static class Util
{
static XmlDocument oDocument;
/* code */
}
Related
In my project I have a class that represents a certain document type. Each class has its own properties and methods although there are some similarities between them.
I am trying to implement an extension method for each class that has the same name (method overloading) but I am getting an ambiguous call error
Code to explain further
Document Type A class representation:
static class DocType_A
{
private static XElement baseDocType_A = XmlFiles[0].Root;
// Extension Method
internal static IEnumerable<ViewData> AsViewData(this IEnumerable<XElement> result)
{
return
from doc in result select new ViewData { };
}
// Contains methods for querying through the XML file of this [DocType_A] type
internal static class Query
{
internal static IEnumerable<XElement> ByStatusOrAny(byte status = 0)
{
return baseDocType_A.Descendants("DocType_A").Select(doc => doc);
}
internal static IEnumerable<XElement> Expired()
{
return baseDocType_A.Descendants("DocType_A").Where("some conditions").Select(doc => doc);
}
}
// Represents the data needed to be displayed to the user via the DGV
internal class ViewData
{
// [DocType_A] related Property
}
internal class Counter
{
// some property
}
}
Document Type B class representation:
static class DocType_B
{
private static XElement baseDocType_B = XmlFiles[1].Root;
// Extension Method
internal static IEnumerable<ViewData> AsViewData(this IEnumerable<XElement> result)
{
return
from doc in result select new ViewData { };
}
// Contains methods for querying through the XML file of this [DocType_A] type
internal static class Query
{
internal static IEnumerable<XElement> ByStatusOrAny(byte status = 0)
{
return baseDocType_B.Descendants("DocType_B").Select(doc => doc);
}
internal static IEnumerable<XElement> Expired()
{
return baseDocType_B.Descendants("DocType_B").Where("some conditions").Select(doc => doc);
}
}
// Represents the data needed to be displayed to the user via the DGV
internal class ViewData
{
// [DocType_B] related Property
}
internal class Counter
{
// some property
}
}
Usage:
class SomeApplicationClass
{
void Method()
{
IEnumerable<DocType_A.ViewData> query = DocType_A.Query.ByStatusOrAny().AsViewData();
}
void SomeOtherMethod()
{
IEnumerable<DocType_B.ViewData> query = DocType_B.Query.ByStatusOrAny().AsViewData();
}
}
But I am getting ambiguous call error.
Is is possible to make extension method for each class with the same name?
The extension methods that you are creating aren't for Doctype_A or Doctype_B, you are creating them for the IEnumerable < XElement >. So you do actually have two extension methods with the same signature.
If you want one specific to A or B, you'd do it like this
internal static IEnumerable<XElement> ByStatusOrAny(this DocType_A doc, byte status = 0)
{
return doc.Query.Descendants("DocType_A").Select(doc => doc);
}
and then call it like
DocType_A.ByStatusOrAny().AsViewData();
I know you want it to be scoped, but that's not how it works. Whatever has the [this] qualifier is what the extension method is going to be applied to, regardless of the class it's in. You might be able to do what you want if you keep each extension method in a different namespace and only reference the namespace you want in the specific files you want, but I've never tried that, so your mileage may very.
Also, as others have pointed out, your example doesn't seem to fit the normal use case for extension methods. So if that's really what you are doing you might want to rework something.
You are thinking about extension Methods as if they were class Methods. They are not.
What they actually are is syntax sugar for a bunch of static Methods, so the compiler & Intellisense can help you calling them.
You have to functions that require a parameter of type IEnumerable<XElement>. Wich one is the compiler suppsoed to call on a IEnumerable<XElement>? For all intents of overload checking, the "this" parameters is ignored. After after that you have two functions with the same name and with any parameter types to differentiate on.
I believe that your problem is here:
internal static IEnumerable<ViewData> AsViewData(this IEnumerable<XElement> result)
You have one metod like this in DocType_B and DocType_A ... if you remove static, it should work, but I don't think this is a way to go.
I'm learning C# but I'm having trouble with passing the data NameValueCollection from one class to another in my console application, I'm experienced in using java but from reading about passing by value and passing by reference has me stumped. My demo code below of the two classes is as follows, my helper class.
namespace ConsoleApplication3
{
class Helper
{
public void Dostuff()
{
string name = "valves";
int num = 5;
NameValueCollection collection = new NameValueCollection();
collection.Add("Sam", "Dot Net Perls");
collection.Add("Bill", "Microsoft");
}
public NameValueCollection GetCollection()
{
NameValueCollection coll2 = Dostuff.Collection();
return coll2;
}
}
}
And here is my program class
namespace ConsoleApplication3
{
class Program
{
public void Main(string[] args)
{
Helper h1 = new ConsoleApplication3.Helper();
NameValueCollection newcollection = h1.GetCollection();
RandomInbuiltMethod(newcollection);
}
}
}
I'm not handling the passing of the NameValueCollection data properly and I don't want to make the helper methods static, Is there a way to do this, I'm sure I'm over creating the complexity and using to much resources for the Helper class, can anyone show me how to solve this.
Probably you want to return the collection created in the DoStuff method of the Helper class
class Helper
{
private NameValueCollection Dostuff()
{
string name = "valves";
int num = 5;
NameValueCollection collection = new NameValueCollection();
collection.Add("Sam", "Dot Net Perls");
collection.Add("Bill", "Microsoft");
return collection;
}
public NameValueCollection GetCollection()
{
return Dostuff();
}
}
Now, your main method will retrieve the collection with the values added inside the DoStuff method
I'm new to programming and I have this script that I'm making, it makes a function that reads XML files based on your input, I've just ran into this issue though where I cannot access the variable named "XMLtext", it's public, can someone tell me what I'm doing wrong and explain it, I've tried various things.
using UnityEngine;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.Collections;
public class Data : MonoBehaviour {
public TextAsset XMLtext;
void Main () {
}
public static string XMLread (params string[] no) {
var XMLfile = XDocument.Parse ();
var a = Data.XMLfile.Element ("data");
for (int i = 0; no[i] == null; i++) {
}
}
}
The problem is that your class Data, which owns the XMLtext field is an instance class. Your method XMLread is static, meaning it is shared across all instances of Data. To solve your problem either make XMLText static or make XMlread an instance method by removing the static keyword.
The problem is that your XMLtext field belongs to an instance of Data, while your XMLread method is static, so it belongs to the class Data. You probably just want to remove the static from XMLread, so that you're always in an instance of Data.
I have created a wrapper class to access PDF FORMS using PDFBox , by using the wrapper I m trying to execute it with VBScript..
Here is my wrapper class (Class Library) with COM enabled
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.util;
using org.apache.pdfbox.pdmodel.interactive.form;
namespace PDF.API
{
public class PDFDocument
{
private PDDocument PD;
public void load(string PDFPath)
{
PD = PDDocument.load(PDFPath);
}
public PDDocumentCatalog getDocumentCatalog()
{
return PD.getDocumentCatalog();
}
public void save(string PDF_Path)
{
PD.save(PDF_Path);
}
public void close()
{
PD.close();
}
}
here is my vbscript
Set TestPDF = CreateObject("PDF.API.PDFDocument")
Set test = PDFDocument.load("D:\\PDF_FORMS\\sample_form.pdf")
Set PDDocumentCatalog = test.getDocumentCatalog()
Set PDAcroForm = PDDocumentCatalog.getAcroForm()
Set PDFField = PDAcroForm.getField("Forenames")
PDField.setValue("VBSCRIPT")
test.save("D:\\PDF_FORMS\\a.pdf")
test.close()
Now it throws me Object required for PDDocument
could not able to resolve this issue
can any one help me out please
Thanks
As Ansgar Wiechers and Aphoria already mention, your Load is a method of your PDFDocument class, and to simplify yourself, you may use the same name as variable name in your .vbs, i.e.:
Set PDFDocument = CreateObject("PDF.API.PDFDocument")
Next issue I see, is that your Load method is a void (not return value), so the syntax should been like this:
PDFDocument.load "D:\path\to\file_a.pdf"
Set PDDocumentCatalog = PDFDocument.getDocumentCatalog()
' ... '
PDFDocument.save "D:\path\to\file_b.pdf"
PDFDocument.close
And I not touched C# recently, but as far as I remember you need a Constructor.
namespace PDF.API
{
public class PDFDocument
{
private PDDocument PD;
public PDFDocument()
{ //class constructor
}
public void load(string PDFPath)
{
PD = PDDocument.load(PDFPath);
}
// ...
}
}
I think you need to change PDFDocument.load... to TestPDF.load....
Set TestPDF = CreateObject("PDF.API.PDFDocument")
Set test = TestPDF.load("D:\\PDF_FORMS\\sample_form.pdf")
Set TestPDF = CreateObject("PDF.API.PDFDocument")
Set test = PDDocument.load("D:\\PDF_FORMS\\sample_form.pdf")
You're using PDDocument without instantiating it first. You probably meant to do this:
Set test = TestPDF.load("D:\\PDF_FORMS\\sample_form.pdf")
As a side note: I'd recommend to escape backslashes inside your class. In VBScript it's usually not required to escape backslashes in paths (WMI notwithstanding), so it may confuse your users if you handle this differently.
I want to use a function from another class within a new function which I will call from main. I am trying to do this as below, but get an error:
Error The name 'Class1' does not exist in the current context.
Actually, in my code I use different names, but its just to illustrate the structure and to make it easier to read for you.
public class Class1
{
public static int[] Function1()
{
// code to return value
}
}
public class Class2
{
public static int Function2()
{
int[] Variable = Class1.Function1();
//other code using function1 value
}
}
Actually, in my code I use different names, but its just to illustrate the structure and to make it easier to read for you.
Unfortunately you've made it so easy to read that you have eliminated the problem entirely! The code you posted does not contain an error and is perfectly valid.
The error message is very clear; from wherever you are actually calling the code, "Class1" (or whatever it may be) is not in scope. This may be because it is in a different namespace. It may also be a simple typo in your class name. Does your code actually look something like this?
namespace Different
{
public class Class1
{
public static int[] Function1()
{
// code to return value
}
}
}
namespace MyNamespace
{
class Program
{
static void Main(string[] args)
{
// Error
var arr = Class1.Function();
// you need to use...
var arr = Different.Class1.Function();
}
}
}
That's the best I got until you post the actual code.