Hey, I'm working on this project and I need to know how to use global variables, basically, I have three functions, one of which sets the data for the other functions to use. In my main, I want to call all these functions to work.

using System.IO;
using System;
namespace Assignment
{
	class MainClass
	{

		public static void FileOpen()
		{
			// create an char array containing grades
			int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
			// open the new text file
			TextReader tr = new StreamReader("/Users//Desktop/Assignmenty/grades.dat");
			string line; 
			
			while ((line = tr.ReadLine()) != null)
			{
				if(line == "A")
				{
					a++;
				}else if(line == "B")
				{
					b++;
				}else if(line == "C")
				{
					c++;
				}else if(line == "D")
				{
					d++;
				}else if(line == "E")
				{
					e++;
				}else if(line == "F")
				{
					f++;
				}
			}	
			
			Graph(a, b, c, d, e, f);
			
		}
		
		public static void WebPage (int gradeA, int gradeB, int gradeC, int gradeD, int gradeE, int gradeF)
		{
			TextWriter tw = new StreamWriter ("grades,html");
			string line = "<html><head><title>Grades: Calculator</title></head><body><b>HELLO</b></body>";
			tw.WriteLine(line);
			tw.Close();
		}
		
		
		public static void Graph(int gradeA, int gradeB, int gradeC, int gradeD, int gradeE, int gradeF)
		{
			Console.WriteLine ("0\t10\t20\t30\t40\t50\t60\t70\t80\t90\t100");
			Console.WriteLine ("|\t|\t|\t|\t|\t|\t|\t|\t|\t|\t|");
			Console.WriteLine ("*********************************************************************************");
			
			int resultA = gradeA * 100 / 50;
			int resultB = gradeB * 100 / 50;
			int resultC = gradeC * 100 / 50;
			int resultD = gradeD * 100 / 50;
			int resultE = gradeE * 100 / 50;
			int resultF = gradeF * 100 / 50;
		     	
        	Console.WriteLine (resultA);
			Console.WriteLine (resultB);
			Console.WriteLine (resultC);
			Console.WriteLine (resultD);
			Console.WriteLine (resultE);
			Console.WriteLine (resultF);
	
		}
		
		
		public static void Main (string[] args)
		{
			Console.Clear();
			FileOpen();
		        WebPage(a, b, c, d, e, f); // This function will not call.
		}
	}
}

The other thing is that i want to create some HTML code and then write it into the file. The only problem is that I don't want to have it inside the code, I want to display it externally and then add it in. Is there a way of including files like in C++?


Best regards.

Recommended Answers

All 6 Replies

the problem is ...

in the main function, a,b,c,d,e,f arent defined, so theres an error.

if you want to use the a,b,c,d,e,f varibales that were defined in the first function (fileOpen()) then you need to define it outside the function but inside the class

i.e.

class MainClass
	{
                // create an char array containing grades
		int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;

		public static void FileOpen()
		{
			// open the new text file
			TextReader tr = new StreamReader("/Users//Desktop/Assignmenty/grades.dat");
			string line;

that should do it!...


as for you 2nd question, i didnt understand you exactly, can u make it more clear

Hey,

Thank you very much for your reply.

I want to create a function that writes some HTML to a text file, but I don't want to code it into the actual main because it will be too big, instead I want to code it in an external file and then call it from there. Like in C++ I would:

#include "function.h";

You can create another class to handle generating your HTML, or you can use a Partial Class if you want to keep it in the same class, but in a different file.

using 'include' for C++ and 'using' for C# is done my making classes as Momerath mentioned above...

as this is a C# forum, i'll explain it in the C# language...

right now, you're making a .cs file
now all you do is make another one (a new C# file) and write the code there.
then you include the file into your project; by writing this format.

using HTML2txt;

then using the functions within:::::

HTML2txt.nameoffunction();

mark as solved, if solved!

As far as the HTML code goes do you mean you want to have access to an HTML file created outside your program?
If so then create the HTML and add it as a Resource to you project.
Resources are merged in to the program and can be read by the program at any time.
See Resources in Applications

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.