Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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(connStringKey, connString, sqlStmt, tableName);

// read transaction header record
TransactionHeader myTransHdr =new TransactionHeader();
tableName="transaction_header"
..
NavigateRecordsFlex(connStringKey, 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
0
pclarke7
Asked:
pclarke7
5 Solutions
 
Éric MoreauSenior .Net ConsultantCommented:
you could return an object and you will have to cast it.

or always return a dataset which will contain only MyUser or MyTransHdr if that's the case. I really think it is the easiest solution.
0
 
Jacques Bourgeois (James Burger)Commented:
Éric solution would work, but unfortunately, you would have to repeat the logic anyway (or something similar) to know to what type to cast.

His second solution is OK, but once again, you will need to repeat the logic in order to know how to react to the value returned by the method. And by the way, do you really need a DataSet if you are fetching only one table. See http://www.experts-exchange.com/Programming/Languages/.NET/A_10298-When-to-use-a-DataSet-DataTable-DataReader-Command.html

It is not very good design to have a unique method for different purposes. Since no matter what you will need some kind of If to determine what was returned, why not create different methods and call the one that is appropriate.

If you really need want to have only one method, then what I would suggest is to create a class with 3 properties. One for each of the 3 different informations that you may return. Return an instance of that class. This way, you will not have to deal with any type of casting on the receiving end. If you need to return a DataSet (or DataTable) it will be in its own property, but that property will be set to null otherwise, with less overhead that returning a DataTable just to get a String.
0
 
pclarke7Author Commented:
Hi James & Eric,
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
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
it_saigeDeveloperCommented:
It's because you don't initialize them anywhere, e.g. - In the constructor or by way of a private variable; and because they are reference types, the default value for a reference type is null.

If you don't want them unitialized, you will need to either create new instances in the constructor:
public Transaction()
{
	thisTransId = "";
	origTransId = "";
	thisCompanyId = "";
	MyUser = new MasterUser();
	TranHdrList = new List<TranHdrList>();
	DispSeqList = new List<DisplaySeq>();
}

Open in new window

Or use backing fields (private variables) for your properties:
private UserMaster _myUser = new UserMaster();
public UserMaster MyUser
{
	get { return _myUser; }
	set
	{
		if (!value.Equals(_myUser))
			_myUser = value;
	}
}

Open in new window


-saige-
0
 
pclarke7Author Commented:
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
0
 
Éric MoreauSenior .Net ConsultantCommented:
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.
0
 
pclarke7Author Commented:
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(connStringKey, 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
0
 
Éric MoreauSenior .Net ConsultantCommented:
Your NavigateRecordsFlex needs to return a dataset:

public Dataset NavigateRecordsFlex(string connStringKey, string connString, string sqlStmt, string tableName)

Open in new window


Before returning your values, you need to ensure they are in a dataset:

if(tableName=="user_master")
{
var ds = new Dataset();
ds.Tables.Add(MyUser);
return ds;
}

Open in new window

0
 
pclarke7Author Commented:
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
0
 
Éric MoreauSenior .Net ConsultantCommented:
It is hard to say when we don't see code! Have you compared MyUser and MyTran to see if there are differences?
0
 
pclarke7Author Commented:
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
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now