returning a struct

  • Thread starter Thread starter tawright915
  • Start date Start date
T

tawright915

I need to return a struct from another class. I set it up like this:
public struct myData
{
public int mPKey;
public String mOrgDate;
public String mDataText;
public myData(int mPKey, String mOrgDate, String mDataText)
{
this.mPKey = mPKey;
this.mOrgDate = mOrgDate;
this.mDataText = mDataText;
}
}

public myData GetNextRecord()
{
SqlConnection myConnection = new SqlConnection(strConnect);
SqlDataReader myReader = null;
ETC..........
myData mydata;
mydata.mPKey = myReader.GetInt32(myReader.GetOrdinal("DMV1_P_KEY"));
mydata.mOrgDate =
myReader.GetString(myReader.GetOrdinal("DMV1_ORG_DTTM"));
mydata.mDataText =
myReader.GetString(myReader.GetOrdinal("DMV1_DATA_TEXT"));

myConnection.Close();
return mydata;
}

However I do not seem to under stand how to reference this on the
calling method side. Thanks.
 
tawright915 said:
I need to return a struct from another class. I set it up like this:
public struct myData
{
public int mPKey;
public String mOrgDate;
public String mDataText;
public myData(int mPKey, String mOrgDate, String mDataText)
{
this.mPKey = mPKey;
this.mOrgDate = mOrgDate;
this.mDataText = mDataText;
}
}

public myData GetNextRecord()
{
SqlConnection myConnection = new SqlConnection(strConnect);
SqlDataReader myReader = null;
ETC..........
myData mydata;
mydata.mPKey = myReader.GetInt32(myReader.GetOrdinal("DMV1_P_KEY"));
mydata.mOrgDate =
myReader.GetString(myReader.GetOrdinal("DMV1_ORG_DTTM"));
mydata.mDataText =
myReader.GetString(myReader.GetOrdinal("DMV1_DATA_TEXT"));

It would be better to call the constructor of myData for this, IMO.
myConnection.Close();
return mydata;
}

However I do not seem to under stand how to reference this on the
calling method side. Thanks.

What happens when you try? You should just be able to do:

myData foo = someInstance.GetNextRecord();

Out of interest, why do you particularly want myData to be a struct
rather than a class?
 
Back
Top