returning a struct

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.
 
J

Jon Skeet [C# MVP]

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top