1
Answer

Proper use of constructor ?

Rob Sobol

Rob Sobol

10y
1.1k
1

I am reading in and processing a html file and adding records to my SQL database each time the GUID element is read in.

I create the data structure at the start of the loop with the NEW action.  Every time I add a record  I manually clear out each field of the data record structure I am building in preparation of reading in the next set of html records.

Seems like this is the purpose of the data constructor related to the structure.  Is there a way to call the constructor to clear out the existing record? Can I dispose of the current variable and use the NEW to create a new record each time.  Seems like a lot of code to keep clearing out each variable.  It also requires me to modify my code in two places when I add a field.  I am importing 20 tables, each having up to 30 fields.  I do not want to abuse memory resources and would like to optimize my code.
 
Thanks in advance for any suggestions.  My pseudo code is below, but it lost the indents. I left some out for brevity, but you can get the general idea.

private void importServiceCallTypeOfWorkMaster(string filename)

{
string lastID = null;
string nodename = null;
XmlTextReader reader = new XmlTextReader(filename);
ServiceCallTypeOfWorkMaster sctwm = new ServiceCallTypeOfWorkMaster();

while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
nodename = reader.Name;
break;
case XmlNodeType.Text:
switch (nodename)
{
case "TypeOfWorkID":
writeRecordtoSQL(sctwm);

'Once the record is written, I need to clear out the variables.
' Can these lines be replaced ?
sctwm.TypeOfWorkID = "";
sctwm.TypeOfWorkCode = "";
sctwm.TypeOfWorkDescription = "";
sctwm.TypeOfWorkID = reader.Value;
break;

case "TypeOfWorkCode":
sctwm.TypeOfWorkCode = reader.Value;
break;
case "TypeOfWorkDescription":
sctwm.TypeOfWorkDescription = reader.Value;
break;

}

break;

case XmlNodeType.EndElement:
nodename =
null;
break;

}

}

}

public class ServiceCallTypeOfWorkMaster
{
private string typeOfWorkID;
private string typeOfWorkCode;
private string typeOfWorkDescription;

public ServiceCallTypeOfWorkMaster()

{
typeOfWorkID =
"";
typeOfWorkCode =
"";
typeOfWorkDescription =
"";
}

public string TypeOfWorkID
{
get

{
return typeOfWorkID;
}

set
{
typeOfWorkID =
value;
}
}

public string TypeOfWorkCode
{
get
{
return typeOfWorkCode;
}
set
{
typeOfWorkCode =
value;
}

}

public string TypeOfWorkDescription
{
get
{
return typeOfWorkDescription;
}
set
{
typeOfWorkDescription =
value;
}
}
}

 
 
 
Answers (1)