pclarke7
asked on
c# Variable return types
Hello,
I am relatively new to c#. I have a c# application which uses a generic method to read dataset records. I would like to change the generic method so that when I pass specific table name (user_master, transaction_header etc..) the method will return the user_master object , transaction_header obj etc..). When an unspecified table name is encountered then it will just return the dataset which has just been been filled. The code below is just to illustrate what I am trying to do.
// read user master record
UserMaster myUser=new UserMaster();
tableName="user_master"
..
NavigateRecordsFlex(connSt ringKey, connString, sqlStmt, tableName);
// read transaction header record
TransactionHeader myTransHdr =new TransactionHeader();
tableName="transaction_hea der"
..
NavigateRecordsFlex(connSt ringKey, connString, sqlStmt, tableName);
public ????? NavigateRecordsFlex(string connStringKey, string connString, string sqlStmt, string tableName)
{
..
// If user master then return MyUser
if(tableName=="user_master ")
{
return MyUser;
}
// If transaction Header then return MyTransHdr
if(tableName=="transaction _header")
{
return MyTransHdr;
}
return MyDataSet
}
I realize that I will have to change the return type of the method but I'm not sure what I need to change it to. Appreciate some advise on how best to achieve this.
regards
Pat
I am relatively new to c#. I have a c# application which uses a generic method to read dataset records. I would like to change the generic method so that when I pass specific table name (user_master, transaction_header etc..) the method will return the user_master object , transaction_header obj etc..). When an unspecified table name is encountered then it will just return the dataset which has just been been filled. The code below is just to illustrate what I am trying to do.
// read user master record
UserMaster myUser=new UserMaster();
tableName="user_master"
..
NavigateRecordsFlex(connSt
// read transaction header record
TransactionHeader myTransHdr =new TransactionHeader();
tableName="transaction_hea
..
NavigateRecordsFlex(connSt
public ????? NavigateRecordsFlex(string
{
..
// If user master then return MyUser
if(tableName=="user_master
{
return MyUser;
}
// If transaction Header then return MyTransHdr
if(tableName=="transaction
{
return MyTransHdr;
}
return MyDataSet
}
I realize that I will have to change the return type of the method but I'm not sure what I need to change it to. Appreciate some advise on how best to achieve this.
regards
Pat
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Saige,
in my application my classes are defined in Transaction.cs. TransactionService.cs evokes a method in DataBaseIO.cs to read the User file. In this method I create the MyTrans object which now creates a new MyUser
Transaction MyTrans=new Transaction();
public Transaction()
{
thisTransId = "";
origTransId = "";
thisCompanyId = "";
MyUser = new MasterUser();
TranHdrList = new List<TranHdrList>();
DispSeqList = new List<DisplaySeq>();
}
I can see in debug that MyUser is being initialized but immediately after returning from creating MyTrans & MyUser logic returns to the DataBaseIO method and MyUser is null.
Any idea what I'm doing wrong ?
regards
Pat
in my application my classes are defined in Transaction.cs. TransactionService.cs evokes a method in DataBaseIO.cs to read the User file. In this method I create the MyTrans object which now creates a new MyUser
Transaction MyTrans=new Transaction();
public Transaction()
{
thisTransId = "";
origTransId = "";
thisCompanyId = "";
MyUser = new MasterUser();
TranHdrList = new List<TranHdrList>();
DispSeqList = new List<DisplaySeq>();
}
I can see in debug that MyUser is being initialized but immediately after returning from creating MyTrans & MyUser logic returns to the DataBaseIO method and MyUser is null.
Any idea what I'm doing wrong ?
regards
Pat
isn't it too complex for nothing? return a dataset. sometimes the dataset will contain a MyUser datatable, other times a MyTransHdr datatable, other times a full dataset. Ensure that your tablename is correctly set and test the table name when your dataset gets back from your NavigateRecordsFlex methods.
ASKER
Hi Eric,
I don't understand what you are try to say in your last comment. I have a generic database read method which I call to read any table. When it reads specific tables I would like to populate an instance of a class with the data. What you seem to be saying is that I should not have a generic read method and that I should have separate code for every file that I wish to read, which doesn't sound right to me.
I am still struggling with how to call a method that returns two or more data types. I understand that the called method (eg NavigateRecordsFlex) can have multiple return statements as follows
if(tableName=="user_master ")
{
return MyUser;
}
// If transaction Header then return MyTransHdr
if(tableName=="transaction _header")
{
return MyTransHdr;
}
return MyDataSet
But how do I code the Data type of NavigateRecordsFlex ?
NavigateRecordsFlex(connSt ringKey, connString, sqlStmt, tableName);
public ????? NavigateRecordsFlex(string connStringKey, string connString, string sqlStmt, string tableName)
If you could answer this question for me , it would help.
regards
Pat
I don't understand what you are try to say in your last comment. I have a generic database read method which I call to read any table. When it reads specific tables I would like to populate an instance of a class with the data. What you seem to be saying is that I should not have a generic read method and that I should have separate code for every file that I wish to read, which doesn't sound right to me.
I am still struggling with how to call a method that returns two or more data types. I understand that the called method (eg NavigateRecordsFlex) can have multiple return statements as follows
if(tableName=="user_master
{
return MyUser;
}
// If transaction Header then return MyTransHdr
if(tableName=="transaction
{
return MyTransHdr;
}
return MyDataSet
But how do I code the Data type of NavigateRecordsFlex ?
NavigateRecordsFlex(connSt
public ????? NavigateRecordsFlex(string
If you could answer this question for me , it would help.
regards
Pat
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Eric,
I have changed NavigateRecordsFlex to return the last read dataset rather than a variation of different types. However I am still having problems with the initialization of MyUser. In the constructor of MyTran I create a new instance of MyUser and it's constructor in turn initializes the MyUser instance values. But when I next call NavigateRecordsFlex() I find that MyTrans is initialized but MyUser is null. How can it change from initialized to null ?
regards
Pat
I have changed NavigateRecordsFlex to return the last read dataset rather than a variation of different types. However I am still having problems with the initialization of MyUser. In the constructor of MyTran I create a new instance of MyUser and it's constructor in turn initializes the MyUser instance values. But when I next call NavigateRecordsFlex() I find that MyTrans is initialized but MyUser is null. How can it change from initialized to null ?
regards
Pat
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Guys,
Final solution was to return a dataset from DataBaseIO and implement additional methods such as ReturnMyUser() , ReturnMyTran() to return the various objects. Thanks to all for your input.
regards
Pat
Final solution was to return a dataset from DataBaseIO and implement additional methods such as ReturnMyUser() , ReturnMyTran() to return the various objects. Thanks to all for your input.
regards
Pat
ASKER
I have been experimenting a little with the following:
public class TransactionHeader
{
public string transCompanyId { get; set; }
public string thisTransId { get; set; }
public string origTransId { get; set; }
public string transDescription { get; set; }
public string transType { get; set; }
public string screenSize { get; set; }
public string securityLvl { get; set; }
public string validated { get; set; }
public string bypassBarcodeChecks { get; set; }
public string nextNumberSystemCode { get; set; }
public string alertEmailAddress { get; set; }
public string emailSubject { get; set; }
public string emailBody { get; set; }
public string approvalRequired { get; set; }
public TransactionHeader()
{
transCompanyId = " ";
thisTransId = " ";
origTransId = " ";
transDescription = " ";
transType = " ";
transDescription = " ";
screenSize = " ";
securityLvl = " ";
validated = "N";
bypassBarcodeChecks = "N";
nextNumberSystemCode = " ";
alertEmailAddress = " ";
emailSubject = " ";
emailBody = " ";
approvalRequired = "N";
}
}
public class UserMaster
{
public string companyId { get; set; }
public string userId { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string userGroup { get; set; }
public string userBusUnit { get; set; }
public string initialMenu { get; set; }
public string userEmail { get; set; }
public string userPhone { get; set; }
public string lastTranName { get; set; }
public DateTime lastTranDate { get; set; }
public string securityLvl { get; set; }
public string promptTrans { get; set; }
public string scanPfxSfxRqd { get; set; }
public string password { get; set; }
public string environmentsAvail { get; set; } // Prod,Test,Dev,Uat
public string tranHdrDbOvrConStrCode { get; set; }
public string tranMstDbOvrConStrCode { get; set; }
public string userMstDbOvrConStrCode { get; set; }
public string errMsgDbOvrConStrCode { get; set; }
public string promptMsgDbOvrConStrCode { get; set; }
public string conStrDbOvrConStrCode { get; set; }
public UserMaster()
{
companyId = " ";
userId = " ";
firstName = " ";
lastName = " ";
userGroup = " ";
userBusUnit = " ";
initialMenu = " ";
userPhone = " ";
userEmail = " ";
lastTranName = " ";
lastTranDate = DateTime.MinValue;
securityLvl = " ";
promptTrans = " ";
scanPfxSfxRqd = "Y";
password = " ";
environmentsAvail = " ";
tranHdrDbOvrConStrCode = " ";
tranMstDbOvrConStrCode = " ";
userMstDbOvrConStrCode = " ";
errMsgDbOvrConStrCode = " ";
promptMsgDbOvrConStrCode = " ";
conStrDbOvrConStrCode = " ";
}
}
[DataContract]
public class Transaction
{
[DataMember]
public string environment { get; set; }
[DataMember]
public string companyId { get; set; }
[DataMember]
public string thisTransId { get; set; }
[DataMember]
public string origTransId { get; set; }
[DataMember]
public string thisCompanyId { get; set; }
public UserMaster MyUser { get; set; }
public List<TransactionHeader> TranHdrList { get; set; }
public List<DisplaySeq> DispSeqList { get; set; }
//Other props/methods
public Transaction()
{
thisTransId = " ";
origTransId = " ";
thisCompanyId = " ";
}
}
}
The Transaction class contains a copy of the User master & Transaction header classes. I can then use a return type of Transaction to return both User and Header details. One thing is confusing. When I create a new instance of Transaction called MyTrans I would have expected that the User details and transaction header details would be initialized - but the are set to null. Why would they not be initialized on creation of a Transaction Object ?
regards
Pat