PDA

View Full Version : need more C# help




twoodcc
Feb 28, 2007, 03:54 PM
well i have a harder assignment now, that i'm having trouble getting started. we are into arrays now.

basically what i have is a class file with methods given to me. i just have to call the methods and stuff.

but the methods are private. the name of the class is SurveyAnalysis. and one method is a bool method called OpenDataFile. and it accepts a string.

if i have a string called "fileName", that i create in Main, and i try this code:

SurveyAnalysis.OpenDataFile(fileName);

i get this error:

"An object reference is required for the nonstatic field, method or property"

what am i doing wrong?

thanks in advance



AussieSusan
Feb 28, 2007, 04:15 PM
....
but the methods are private. the name of the class is SurveyAnalysis. and one method is a bool method called OpenDataFile. and it accepts a string.

if i have a string called "fileName", that i create in Main, and i try this code:

SurveyAnalysis.OpenDataFile(fileName);

i get this error:

"An object reference is required for the nonstatic field, method or property"
....

Try something like

SurveyAnalysis MyAnalysis = new SurveyAnalysis();
bool OpenResult = MyAnalysis.OpenDataFile(fileName);
if( OpenResult)
{
// Use the "opened" file
}

Basically, the error message is telling you that the method you are calling is must be called in the context of an instance of the class, not the class itself.

However, if, as you say, all the methods are private, then what I have given above will generate its own error saying something about the method not being visible. If all methods are private, then the class is practically useless as nothing can call the methods except within the class itself. Protected methods could be used to inherit from the class, but public methods would be needed to 'use' the class in the way you seem to be wanting to.

Cheers,

Susan

twoodcc
Mar 1, 2007, 10:51 AM
Try something like

SurveyAnalysis MyAnalysis = new SurveyAnalysis();
bool OpenResult = MyAnalysis.OpenDataFile(fileName);
if( OpenResult)
{
// Use the "opened" file
}

Basically, the error message is telling you that the method you are calling is must be called in the context of an instance of the class, not the class itself.

However, if, as you say, all the methods are private, then what I have given above will generate its own error saying something about the method not being visible. If all methods are private, then the class is practically useless as nothing can call the methods except within the class itself. Protected methods could be used to inherit from the class, but public methods would be needed to 'use' the class in the way you seem to be wanting to.

Cheers,

Susan

thank you for helping me out. i tried something similar to your code, but i'm still getting an error. this is my code:

bool OpenResult;
SurveyAnalysis[] survey = new SurveyAnalysis[10000];
OpenResult = survey.OpenDataFile(fileName);

and i get this error:

(my program name is WoodT_program3)

"WoodT_program3.SurveyAnalysis[] does not contain a definition for OpenDataFile"

what did i do wrong this time?

foo.c
Mar 1, 2007, 04:05 PM
thank you for helping me out. i tried something similar to your code, but i'm still getting an error. this is my code:

bool OpenResult;
SurveyAnalysis[] survey = new SurveyAnalysis[10000];
OpenResult = survey.OpenDataFile(fileName);

and i get this error:

(my program name is WoodT_program3)

"WoodT_program3.SurveyAnalysis[] does not contain a definition for OpenDataFile"

what did i do wrong this time?

Pretty much the same thing you did previously, with a new twist. ;)

You need to grok the difference between a class and an object, and static vs non-static member functions. (What textbook are you using? Give it a read. (I don't mean this in a bad way either, this is something you're going to have to figure out.))

Cliff Notes:

A class is, well, the simplest way to think of it is as a mix of variables and functions.

An object is an instance of a class. Usually you get an instance via the new keyword.

Static functions can be called with just the class name.

For non-static functions we get an object, and use that to call the function(s) we want.

In your first error, you are calling a non-static function like it was static. The error message should make more sense to you now that you know the difference between a class and an object. It's saying, hey you need an object to use this function.

Your next error is because you are trying to call a function which is not in the SurveyAnalysis[] type (or class). (SurveyAnalysis and SurveyAnalysis[] are different types.)

What these two errors have in common is that you are not using an instance when you should.

Does xcode have code completion like Visual Studio? If the function you want to call doesn't appear in the list, that should be a clue to you. (I've never used xcode, used to use CodeWarrior for mac stuff back in the day. I'm stuck on Windows and .NET for work.)

Anyway ... this would work with what you have:

bool OpenResult;
SurveyAnalysis[] survey = new SurveyAnalysis[10000];
survey[0] = new SurveyAnalysis(); //I'm getting an instance to use
survey[0].OpenDataFile(fileName);

But ... I don't think that creating an array of 1000 SurveyAnalysis objects is what you want to do. Hard to say for sure without knowing the context of what you are trying to do though.

AussieSusan
Mar 1, 2007, 04:27 PM
thank you for helping me out. i tried something similar to your code, but i'm still getting an error. this is my code:

bool OpenResult;
SurveyAnalysis[] survey = new SurveyAnalysis[10000];
OpenResult = survey.OpenDataFile(fileName);

and i get this error:

(my program name is WoodT_program3)

"WoodT_program3.SurveyAnalysis[] does not contain a definition for OpenDataFile"

what did i do wrong this time?

Firstly, the error is telling you that the array you have created (but not filled with any actual objects - see below) does not have an OpenDataFile method. This makes sense because, as you have defined it, 'survey' is an array of references to SurveyAnalysis objects (which you have not yet created), not a SurveyAnalysis object itself..

If you really want to create 10,000 SurveyAnalysis objects (and surely you don't!), then you would need to do something like:

SurveyAnalysis[] survey = new SurveyAnalysis[10000];
for( int i = 0; i < 10000; i++)
{ survey[i] = new SurveyAnalysis(); }

Then you could choose one of these 10,000 objects to do the actual work for you:

OpenResult = survey[3154].OpenDataFile(fileName);


I think you are confusing the data you want analysed with the object that does the analysis:

SurveyAnalysis survey = new SurveyAnalysis();
OpenResult = survey.OpenDataFile(fileName);

// Making up the next bit....
int[] Surveydata = new int[10000];
// fill in the SurveyData array here
survey.AnalyseData(SurveyData);
survey.PrintResultsToConsole();


Susan

PS: See the above reply - I was interrupted as I was writing!

twoodcc
Mar 1, 2007, 04:37 PM
thank you both very much for helping me.

i think i'm getting a little confused here, mainly b/c what ya'll are saying and what the teacher has said, isn't quite the same.

basically the assignment is to read in a text file. the text file has a list of letters, with an integer beside them. the letters are supposed to be 'A' or 'B'. and you're supposed to add up all the A's and all the B's. then do some other stuff with them.

so i need to put them in an array. 1 array for all the A's and one for all the B's (i think).

so now i'm getting confused. how do i read in the file? i have the method to read in the file, but i can't figure out how to use it. and in the assignment, it says that there won't be more than 10000 items in the text file. so i guess i need two 5000 arrays?

kainjow
Mar 1, 2007, 05:50 PM
I'm curious, doesn't your professor go over how to read in files, and how to work with classes? Do you have a book to go along with the course?

It's good to ask questions on forums and get an instant answer, but if you don't understand the concepts of what you're doing than it's useless (if you care to actually learn). If you have a book it should tell you how to do all these basic things, in a clear way.

Are you doing this C# on your Mac with Mono?

twoodcc
Mar 1, 2007, 05:57 PM
I'm curious, doesn't your professor go over how to read in files, and how to work with classes? Do you have a book to go along with the course?

It's good to ask questions on forums and get an instant answer, but if you don't understand the concepts of what you're doing than it's useless (if you care to actually learn). If you have a book it should tell you how to do all these basic things, in a clear way.

Are you doing this C# on your Mac with Mono?

you are right. and yes i have a professor and a book, but it is not clear. especially on assignments like this, where we've never gone over something like this before. i mean, i have a 95 in the class right now, so it's not like i'm not learning anything.

yes, i'm doing this on my Mac with Mono and a CSharp plugin in xcode

twoodcc
Mar 1, 2007, 06:34 PM
ok, well i got it to at least compile. but i'm still kinda lost. this is what i did:

string fileName = "survey.txt";
bool OpenResult;
SurveyAnalysis fileData = new SurveyAnalysis();
OpenResult = fileData.OpenDataFile(fileName);

but now i really don't now what to do

twoodcc
Mar 1, 2007, 06:47 PM
well really the main thing i'm having trouble with is figuring out if it's reading the variable or not. here is the method that was given to us:

public bool OpenDataFile(string f)
{
bool success = true;

// try to open the file whose path is provided
try // use the exception handling syntax to show how this is done
{
sr = new System.IO.StreamReader(f);
}
catch // exception handling is not discussed
{ // in the book until Chapter 9
success = false; // this signals that the file did not open
}

return success;
}

but all it does is return success
so does it actually read in the file?

twoodcc
Mar 1, 2007, 09:18 PM
well i thought i was getting somewhere. but for some reason it only stores the first value in the array, and that's it. i didn't want to post all my code in here, but i need to, then i can. but for now, i'll just post this part:

aSurvey = new int[DATAELEMENTS];
bSurvey = new int[DATAELEMENTS];
SurveyAnalysis fileData = new SurveyAnalysis();
OpenResult = fileData.OpenDataFile(fileName);
StreamReader inputFile = new StreamReader(fileName);

if( OpenResult)
{

line = inputFile.ReadLine();
while (line != null)
{
ParseResult = fileData.Parse(line, out city, out hours, out msg);
if (ParseResult)
{
IsIntegerResult = fileData.IsInteger(line);
//if(IsIntegerResult)
//{
ValidCityResult = fileData.ValidCity(city);
//if (ValidCityResult)
//{
ValidHoursResult = fileData.ValidHours(hours);
//if (ValidHoursResult)
//{
if (city == 'A' && ValidCityResult && ValidHoursResult)
{
for (int i = 0; i < aSurvey.Length; i++)
aSurvey[i] = hours;
}
if (city == 'B' && ValidCityResult && ValidHoursResult)
{
for (int i = 0; i < bSurvey.Length; i++)
bSurvey[i] = hours;
}
//}
//}
}
line = inputFile.ReadLine();
}

}
inputFile.Close();
minimumA = fileData.GetMinimum(aSurvey, aSurvey.Length);
minimumB = fileData.GetMinimum(bSurvey, bSurvey.Length);
maximumA = fileData.GetMaximum(aSurvey, aSurvey.Length);
maximumB = fileData.GetMaximum(bSurvey, bSurvey.Length);
averageA = fileData.GetAverage(aSurvey, aSurvey.Length);
averageB = fileData.GetAverage(bSurvey, bSurvey.Length);
deviationA = fileData.GetStandardDeviation(aSurvey, aSurvey.Length);
deviationB = fileData.GetStandardDeviation(bSurvey, bSurvey.Length);

foo.c
Mar 1, 2007, 09:22 PM
well really the main thing i'm having trouble with is figuring out if it's reading the variable or not. here is the method that was given to us:

[SNIP by foo.c]

but all it does is return success
so does it actually read in the file?

Nope, it doesn't read the file, but it does initialize the sr member variable (an instance of StreamReader).

Is there another function, something like ReadDataFile in the class?

Search for 'sr.' I'm sure somewhere in there he's doing something like:

sr.ReadLine();

twoodcc
Mar 1, 2007, 09:39 PM
Nope, it doesn't read the file, but it does initialize the sr member variable (an instance of StreamReader).

Is there another function, something like ReadDataFile in the class?

Search for 'sr.' I'm sure somewhere in there he's doing something like:

sr.ReadLine();

thanks. i'm past that part now. i think i found my problem.

i have a bool variable called ParseResult.

my code is depending on a while loop, where it calls the Parse method over and over again, sending data into the method.

here is the line of code:

ParseResult = fileData.Parse(line, out city, out hours, out msg);

where 'line' is the line in the text file

this line is in my while loop. will this be able to loop and change the value in line, city, hours, msg? b/c i think it does it once, and then it's storing that one value for every part of the array.

so basically i come out with 1 array with all 10's, and another with all 14's

foo.c
Mar 2, 2007, 01:02 PM
so basically i come out with 1 array with all 10's, and another with all 14's

Hint: for loops

AussieSusan
Mar 4, 2007, 04:47 PM
......
my code is depending on a while loop, where it calls the Parse method over and over again, sending data into the method.

here is the line of code:

ParseResult = fileData.Parse(line, out city, out hours, out msg);

where 'line' is the line in the text file

this line is in my while loop. will this be able to loop and change the value in line, city, hours, msg? b/c i think it does it once, and then it's storing that one value for every part of the array.

so basically i come out with 1 array with all 10's, and another with all 14's

Without seeing the complete module that you are attempting to use (i.e. the one your professor would have given you) its a bit hard to guess at what is going on here.

However, I suspect that there are separate routines to:
- open the data file (OpenDataFile)
- read a line (this is the one I don't think you have mentioned)
- parse a line (Parse)

I would think that you need a program structured something like


SurveyAnalysis myAnalysis = new SurveyAnalysis();
String DataLine;
bool ParseResult;

myAnalysis.OpenDataFile( fileName);
while( myAnalysis.ReadLine(DataLine)) // Assumed to put next line in DataLine and return false on end-of-file
{
if( myAnlysis.Parse( DataLine, out City, out Hours, out Msg))
{
// if City = "A" then store the 'Hours' value in the next CityA array element
// if City = "B" then same for CityB
}
}


I'm also suspicious of your assumption that, if there are no more than 10,000 then 5,000 element arrays will be required. Unless you are told otherwise, don't assume that there is a 50/50 split - you will have problems when you get the 5,001'th element for any city!

What are you expected to do with the values? Looking at the code you posted before, I think you may be looking for minimum, maximum and average values for each city. If that is the case, then you may not need arrays at all. As you get the value for each city, you can determine if this is the largest or smallest value seen so far, as well as summing the total value and the number of entries so that you can work out the average later on. (If you need standard deviations and other statistics as well, then you may need to calculate other running totals).

Susan