473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cannot add to an array [c#]

Markus
6,050 Expert 4TB
I'm trying to take values from an .xml file and add them into an array, but I keep hitting this error: Error 1 An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Program.Names'

Here is the c# (line #23)
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         string[] Names = new String[500];
  12.         static void Main(string[] args)
  13.         {
  14.             //Create instance and then call read method
  15.             XmlTextReader TextReader = new XmlTextReader("C:\\names.xml");
  16.             TextReader.Read();
  17.             //While there is a value
  18.             int i = 0;
  19.             while (TextReader.Read())
  20.             {
  21.  
  22.                 TextReader.MoveToElement();
  23.                 Names[i] = TextReader.Value;
  24.                 i++;
  25.             }
  26.             Console.ReadLine();
  27.         }
  28.     }
  29. }
  30.  
Oct 16 '08 #1
7 1565
Plater
7,872 Expert 4TB
Main() is always a static function. You need to create an instance of the Program class if you want to use its member variables.
Oct 16 '08 #2
Curtis Rutland
3,256 Expert 2GB
The best way to take care of this is to declare Names as static, since that is how you are going to use it here.
Oct 16 '08 #3
Markus
6,050 Expert 4TB
Main() is always a static function. You need to create an instance of the Program class if you want to use its member variables.
How would you do that?

And thanks Kurt/Curt.
Oct 17 '08 #4
r035198x
13,262 8TB
How would you do that?

And thanks Kurt/Curt.
To declare something as static just prepend it with the static modifier or declare it in a static context. You could declare that array inside the Main method itself.
Oct 17 '08 #5
Curtis Rutland
3,256 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. //change
  2. string[] Names = new String[500];
  3. //to
  4. static string[] Names = new String[500];
Oct 17 '08 #6
Markus
6,050 Expert 4TB
To declare something as static just prepend it with the static modifier or declare it in a static context. You could declare that array inside the Main method itself.
No, I got that. I was asking about the 'create an instance of the program class'.
Oct 17 '08 #7
Curtis Rutland
3,256 Expert 2GB
No, I got that. I was asking about the 'create an instance of the program class'.
Expand|Select|Wrap|Line Numbers
  1. Program program = new Program();
But you really don't have any need to do that. The Program class is there to contain your static void Main() method, the entry point of the application. Since Main is static, you can't access non-static members of Program without making an instance of program. But there's really no point in doing that, seeing as you want to use that array statically too.
Oct 17 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: overbored | last post by:
I can do this: int asdf; int* zxcv = asdf; but not this: int asdf; int** zxcv = asdf;
7
by: tuchka | last post by:
Hi, guys! I am very new here and just started to learn C. I have previous java exp. however. I'm abs. stuck on pointers and i'm unable comprehend algorithm of simple program that reverses chars...
3
by: klynn | last post by:
I defined a session variable as an array using Session = new string; Later, in my code, I need to set it and sort it. I tried Session = "some string"; My error is "Cannot apply indexing with to...
10
by: asnowfall | last post by:
I get following compilation error C3149: 'System::String' : cannot use this type here without a top-level '^'. Could someone explain why I get this error? here is the code... ImageFileData.h...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
2
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
2
by: cendter | last post by:
Hi there, I am utterly confused - I have a form that starts an instance from excel and let's the user select a range. I then want to take this range as an array of doubles. I ran into trouble...
9
by: anon.asdf | last post by:
Hi! The following code is a snippet where the goto's cannot be removed "without increasing code-size, or decreasing performance". /*********A**************/ int var_other_thread; // value...
4
by: sirjohnofthewest | last post by:
If I possessed the power to sway the mind of every user in the world to delete all forms of Internet Explorer I would die a happy man. Hi guys, I frequently visit this site to get answers to my...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.