Hi i am having difficulty passing the contence of a struct to a class. I have constructed the class without any errors. However when i try to pass the struct with just its name i get a not declaired compiling error. I have no idea how i can do this can someone please help?
FallenPaladin 0 Junior Poster in Training
Dani AI
Generated
Short answer for : the compile-time problem you hit is the C# definite-assignment rule for local value-type variables. As pointed out, the compiler will not let you use a local struct variable until it is definitely assigned. See the language note on definite assignment for details: .
A few practical tips that build on the thread and avoid common pitfalls:
- Understand the difference between value types and reference types. Structs are value types and have default values, but local variables still require explicit assignment before use; class fields are auto-initialized. See the struct reference for behavior and guidance: C# struct types.
- Passing a struct to a method copies it. For large records consider passing by reference (
ref/out) or using a class to avoid copying and to make intent clearer. Therefkeyword docs explain the semantics and trade-offs: ref keyword.
I/O and serialization notes that help make the reader/writer pair robust:
- Always dispose streams (use
usingor try/finally). Ensure write and read use the same field order and compatible encodings. - Prefer writing primitive binary representations for stable round-trips (for example ticks for DateTime) rather than depending on formatted strings, which are culture-sensitive.
Quick checklist if the error persists: confirm the struct variable is assigned before the call, verify scope (local vs field), check for naming conflicts or namespace visibility, and ensure reader/writer ordering matches exactly. For BinaryWriter/Reader reference and details, consult the API docs.
Recommended Answers
Jump to Post— scru 909Could you attach some code please?
All 4 Replies
scru 909 Posting Virtuoso Featured Poster
Could you attach some code please?
FallenPaladin 0 Junior Poster in Training
//struct class
/// <summary>
/// Summary description for Class employeeStructur.
/// This class holds the data structur for the records held within
/// the employee file, the backup of this file and the archive of this file.
/// </summary>
//[StructLayout(LayoutKind.Sequential)]
public struct employeeRecord
{
public char status;
public int employeeNo;
public string lastName;
public string firstName;
public DateTime dateBirth;
public string homePoneNo;
public string mobPhoneNumber;
public string houseNo;
public string addressRoad;
public string addressTown;
public string addressCounty;
public string addressPostCode;
public DateTime startDate;
public DateTime endDate;
public string userName;
public string password;
public int accessLevel;
public string department;
public decimal salary;
public DateTime lastUpdate;
public int updatedBy;
}
//Main class
employeeRecord newRecord;
//Large amount of code to accept input and validate
//for each struct component
//subroutine call passing the file stream, binnary writer and structure
changeFile.recordWriter(empFileStream,empFileWriter,newRecord);
//the class being called is layed out as
public employeeRecord returnRecord(FileStream theFile, BinaryReader theReader,employeeRecord theRecord)
{
//method to read one entire record
employeeRecord returnRecord = theRecord;
returnRecord.status=theReader.ReadChar();
returnRecord.employeeNo=theReader.ReadInt32();
returnRecord.userName=theReader.ReadString();
returnRecord.password=theReader.ReadString();
returnRecord.accessLevel=theReader.ReadInt32();
returnRecord.lastName=theReader.ReadString();
returnRecord.firstName=theReader.ReadString();
returnRecord.dateBirth= DateTime.Parse(theReader.ReadString());
returnRecord.houseNo=theReader.ReadString();
returnRecord.addressRoad=theReader.ReadString();
returnRecord.addressTown=theReader.ReadString();
returnRecord.addressCounty=theReader.ReadString();
returnRecord.addressPostCode=theReader.ReadString();
returnRecord.homePoneNo=theReader.ReadString();
returnRecord.mobPhoneNumber=theReader.ReadString();
returnRecord.startDate=DateTime.Parse(theReader.ReadString());
returnRecord.endDate=DateTime.Parse(theReader.ReadString());
returnRecord.department=theReader.ReadString();
returnRecord.salary=theReader.ReadDecimal();
returnRecord.lastUpdate=DateTime.Parse(theReader.ReadString());
returnRecord.updatedBy=theReader.ReadInt32();
return returnRecord;
}
public void recordWriter( FileStream theFile, BinaryWriter theWriter,employeeRecord theRecord)
{
//method to write one entire record
theWriter.Write(theRecord.status);
theWriter.Write(theRecord.employeeNo);
theWriter.Write(theRecord.userName);
theWriter.Write(theRecord.password);
theWriter.Write(theRecord.accessLevel);
theWriter.Write(theRecord.lastName);
theWriter.Write(theRecord.firstName);
theWriter.Write((theRecord.dateBirth).ToShortDateString());
theWriter.Write(theRecord.houseNo);
theWriter.Write(theRecord.addressRoad);
theWriter.Write(theRecord.addressTown);
theWriter.Write(theRecord.addressCounty);
theWriter.Write(theRecord.addressPostCode);
theWriter.Write(theRecord.homePoneNo);
theWriter.Write(theRecord.mobPhoneNumber);
theWriter.Write((theRecord.startDate).ToShortDateString());
theWriter.Write((theRecord.endDate).ToShortDateString());
theWriter.Write(theRecord.department);
theWriter.Write(theRecord.salary);
theWriter.Write((theRecord.lastUpdate).ToShortDateString());
theWriter.Write(theRecord.updatedBy);
}
} unfortunatly without giving all the code it is still a bit difficult to see what is happening.
any help you could give would be great
thanks
scru 909 Posting Virtuoso Featured Poster
I get a build. I had to change this to
employeeRecord newRecord = new employeeRecord(); though.
FallenPaladin 0 Junior Poster in Training
Thank you. I have been trying to work it out and had stumbled accros it by accident, but thanks for taking the time to look.
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.