Hello everyone,
I am new to this forum, and relatively new to C#...
So I have this project (midterm) that i need to work on:
Looks like i need to create records to a .txt file via StreamWriter with whatever filename the user wishes to use. Then for every record recorded i need to create an object with those fields. Field names: number, data, time, (enum)priority, and description. All of these fields are required as a guideline to be "readonly."
So this leads me with a problem: once i set values to all the readonly fields, then that's that. i can't use them for anything. I will try (write is the name of the StreamWriter i made) write(object.field); and it will always tell me i can't use them due to their protection level. I even tried Parsing it and it still wouldn't allow me to use it! So pretty much, this readonly modifier wont even let me READ IT?!
I tried using microsoft's help website and anywhere else and i think i might have to use "get" and "set"? I don't know how to use these since i've never had to use them...
any help would be really good right now. I have literally hit a brick wall on this one :cursing:
7 replies to this topic
#1
Posted 30 April 2011 - 11:06 AM
|
|
|
#2
Posted 30 April 2011 - 12:37 PM
Most likely you didn't declare them as 'public readonly' which is why you can't access them. But I'd go with get/set:
public int Number {get; private set;}
As an example.
#3
Posted 30 April 2011 - 01:12 PM
Get and Set are exactly what you need to use. What you're talking about here (all the field names you posted) are properties. Properties are just variables inside a class. The get and set methods make them visible (get) and updateable (set).
To make a readonly property, just don't declare the set.
So for example, to create a property for your number field:
Now when you do:
It should write the value 5 to your file (in my example).
I just air-coded all that so hopefully it's syntactically correct.
Hope it answered your question!
To make a readonly property, just don't declare the set.
So for example, to create a property for your number field:
public class myObject {
// this private variable is used internally (the underscore is just a naming convention to show that it's internal
// it will actually hold the value of the property
private int _number = 0;
// the constructor
public myObject() {
//set all your fields to their values
_number = 5;
}
public int Number {
get {
return _number;
}
}
}
Now when you do:
myObject obj = new myObject(); streamwriter.write(obj.Number.toString());
It should write the value 5 to your file (in my example).
I just air-coded all that so hopefully it's syntactically correct.
Hope it answered your question!
#4
Posted 01 May 2011 - 10:42 AM
Awesome! Thank you guys for all your help!
I should have mentioned that the object fields also had to be private. so...
as another guideline. But i am going to type up this code once again. I had it all ready but i lost that code while stupid windows decided it would update! :cursing:
So i guess all i need is "get" and it involves making another method within the object?
could I do this?:
I will probably find this out myself before a reply :P but if i have any troubles i will post my actual code on here.
This project is a little tough since the teacher never once clarifies what it's supposed to to! :cursing:
I should have mentioned that the object fields also had to be private. so...
private readonly int number;
as another guideline. But i am going to type up this code once again. I had it all ready but i lost that code while stupid windows decided it would update! :cursing:
So i guess all i need is "get" and it involves making another method within the object?
could I do this?:
public string Fields()
{
// these are all of type string so can i clump them together?
return time;
return data;
return description;
}
I will probably find this out myself before a reply :P but if i have any troubles i will post my actual code on here.
This project is a little tough since the teacher never once clarifies what it's supposed to to! :cursing:
#5
Posted 01 May 2011 - 03:36 PM
Ok so I have another small problem. This one should be really easy for anyone who has dealt with List<object>'s often...
to give a little detail: it looks like right after i write all the object fields to a .txt file, i must give the function for users to search through the file for certain things. In this case i have to search for the number. It's more like a serial number the more i think about it now. Before i go further, it's important to note that each object represents a .txt record and the List<object> is the physical .txt file.
Record Format:
number: 1
data: blah blah
time: 12:00
priority: urgent
description: This is one urgent error!
and a .txt file can be full of these. But anyways, one "Record" is one object, and one whole .txt file is a List<object> (a list of records :thumbup1:).
So the short version:
I know how to search the List using "foreach" and how to see if one of the list's objects have a field with whatever number the user wants. But i can't seem to use that particular object.
*remember every object is a numbered Record, like a list of fields i listed above*
So naturally once i find this object i have to read it's contents out through the Console.... In my method that searches this List<object> it returns the individual object. But when i try to have the program write it's fields, the object i refer to is out of context.
Here's some code that may help:
Hopefully i have provided enough information so you guys know how to help me! And also know that i am assuming that there is nothing wrong with the way I have this coded.
if my coding is drastically wrong and/or you need more information please let me know! :thumbup:
to give a little detail: it looks like right after i write all the object fields to a .txt file, i must give the function for users to search through the file for certain things. In this case i have to search for the number. It's more like a serial number the more i think about it now. Before i go further, it's important to note that each object represents a .txt record and the List<object> is the physical .txt file.
Record Format:
number: 1
data: blah blah
time: 12:00
priority: urgent
description: This is one urgent error!
and a .txt file can be full of these. But anyways, one "Record" is one object, and one whole .txt file is a List<object> (a list of records :thumbup1:).
So the short version:
I know how to search the List using "foreach" and how to see if one of the list's objects have a field with whatever number the user wants. But i can't seem to use that particular object.
*remember every object is a numbered Record, like a list of fields i listed above*
So naturally once i find this object i have to read it's contents out through the Console.... In my method that searches this List<object> it returns the individual object. But when i try to have the program write it's fields, the object i refer to is out of context.
Here's some code that may help:
// this is from the main program class. Its function is pretty clear.
Console.WriteLine("Do you want to search {0}.txt by RECORD number or Error PRIORITY? (RECORD, PRIORITY)", filename);
string searchResponce = Keyboard.InputString();
if (searchResponce == "RECORD")
{
Console.WriteLine("Insert the record number you wish to find: ");
int recordNumber = Keyboard.InputInteger();
// here is where i send the number to the method that searches through my
// List<object>
// NOTE: "program" is just the name of an object that pretty much is in charge of any
// given .txt file. It contains the Method that searches the List<object>
program.ReadRecord(recordNumber);
// this is where i get my "out of context" issues.
// errorRecord is the name of the object in the list<object> i wish to get information
// from.
Console.WriteLine("The record reads as follows: ");
Console.WriteLine(errorRecord.number);
Console.WriteLine(errorRecord.data);
Console.WriteLine(errorRecord.time);
Console.WriteLine(errorRecord.description);
}
// here is my method that searches through the List<object>
// I am pretty sure it works, but haven't been able to test it yet...
public Error ReadRecord(int recordNumber)
{
// "errorList" is the name of the List<object>
foreach (Error errorRecord in errorList)
{
if (number.Equals(number))
{
// this is where I return the object from within the List<object> that supposedly has
// the number the user is looking for
return errorRecord;
}
else
{
Console.WriteLine("there is either an error in input, or there is no record of this number at this time.");
Console.Read();
}
}
}
Hopefully i have provided enough information so you guys know how to help me! And also know that i am assuming that there is nothing wrong with the way I have this coded.
if my coding is drastically wrong and/or you need more information please let me know! :thumbup:
#6
Posted 05 May 2011 - 05:14 PM
do you have to use a list? Have you considered a hash table?
eg
you can use foreach on the dict.Values collection, but you can also access it by it's key
for example:
eg
Dictionary<int, object> dict = new Dictionary<int, object>(); dict[0] = an_object; dict[1] = another_object;
you can use foreach on the dict.Values collection, but you can also access it by it's key
for example:
object some_object = dict[0];
#7
Posted 05 May 2011 - 11:19 PM
what are readonly fields and sam_coder why did you write <int, object> what is that part???
#8
Posted 06 May 2011 - 04:55 AM
Tonchi, I was just responding to the post previously, where he was having issues with a particular collection. The int, object part, these are type specifications for a generic collection. in a more simpler form, something like List<T> would be a generic collection, that collects type T.
For example
or,
a readonly field, is a field that can be read, but cannot be written to. This is useful in some circumstances, say. for example you have a class that you want to restrict external access to..
externally, they user can see the name field, but you've only provided logic for them to read. They cannot assign anything to it.
How does this work? Well lets look at the above example.
In that example, the Read Only Name field, returns the value of the m_name variable. There is another method supplied to set it's value. This implementation is impractical, but I think it explains what you want to know.
note, there is also some pretty nice syntax for automatic properties, that make this make more sense.
in this case, you can set the name field internally from the Person class, but you can only read it externally.
For example
List<int> lst = new List(); lst.Add(0); lst.Add(1);
or,
List<String> lst = new List();
lst.Add("A");
lst.Add("b");
lst.Add("C");
a readonly field, is a field that can be read, but cannot be written to. This is useful in some circumstances, say. for example you have a class that you want to restrict external access to..
class Person {
public String Name {
get {
//You can set what ever you want here
}
}
}
externally, they user can see the name field, but you've only provided logic for them to read. They cannot assign anything to it.
How does this work? Well lets look at the above example.
class Person {
private String m_name;
public String Name {
get {
return m_name;
}
}
public void set_name(string name) {
m_name = name;
return;
}
}
In that example, the Read Only Name field, returns the value of the m_name variable. There is another method supplied to set it's value. This implementation is impractical, but I think it explains what you want to know.
note, there is also some pretty nice syntax for automatic properties, that make this make more sense.
class Person {
public String Name { get; private set; }
}
in this case, you can set the name field internally from the Person class, but you can only read it externally.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









