Returning multiple values

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

If I have the following code, what do I have to change to
make it return more than one value?

public string Records
{
get { return textBoxName.Text; }
//return textBoxAddress.Text;
//return textBoxNotes.Text;
}

I can only return one value from my dialog box, but I would like
to return a few more values.


Thanks


Steve
 
If I have the following code, what do I have to change to
make it return more than one value?

        public string Records
        {
            get { return textBoxName.Text; }
             //return textBoxAddress.Text;
             //return textBoxNotes.Text;
        }

I can only return one value from my dialog box, but I would like
to return a few more values.

Thanks

Steve

One option is to create a class with all these values and return the
class instead of the string variable.
Second option is to concatinate the values and split it in place where
you get it.
Thrid option is to put it in a string array and return string array
instead of string.
Fourth option is to have seperate getters for each text box.
Fifth option is to make a method and use 'out'

The bottom line is, try to have only one return value from a method if
possible. If you are trying to return more than one, then you may need
to revisit how it is used.
 
public List<string> MultipleValues
{
get {

List<string> values = new List<string>();
values.Add ( textBox1Name.Text );
values.Add ( textBox2Name.Text );
return values;
}
}


You might want to explain what you're trying to do. That looks like its
heading down a hacky path.
(Maybe I'm wrong..but it be good to ask either way).
 
If I have the following code, what do I have to change to
make it return more than one value?

public string Records
{
get { return textBoxName.Text; }
//return textBoxAddress.Text;
//return textBoxNotes.Text;
}

I can only return one value from my dialog box, but I would like
to return a few more values.

Thanks

Steve

I ran into a similar issue in my application. I created a RecordInfo
class that contains the information (or you can change the return type
to string[]).

Is there a better way to handle this scenario?

-tom
 
If I have the following code, what do I have to change to
make it return more than one value?

public string Records
{
get { return textBoxName.Text; }
//return textBoxAddress.Text;
//return textBoxNotes.Text;
}

I can only return one value from my dialog box, but I would like
to return a few more values.

You can:
- return an array, if the values are all of the same type
- create a class or struct to hold your multiple values and return
that

There is unfortunately no way to return multiple values directly, like
tuples in Python or similar languages.
Lasse Vågsæther Karlsen
mailto:[email protected]
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
 
Appreciate the help everyone,

I guess it will have to use a class then. I did not know how to
accomplish the task, but I guess a class will take care of it.

Sloan, I am trying to create a record. On new entry a dialog
will popup and you fill in the entries, then all the values will
be passed back to the main form for processing.


Thanks again


Steve
 
My approach to this is to create a "stream lined" class....
When I need to pull the info for an Employee....I do something like this

public class EmployeeStreamLined
{

//guts for
EmpUUID
LastName
FirstName
SSN

}


Now, I do alot of remoting(wcf)....so I like my objects as small as can
be.....

However, you can create this class...populate it with textbox.Text values
(or whatever)
and ship it back.

EmployeeStreamLined e = new EmployeeStreamLined();
e.LastName = textbox1.Text;
e.FirstName = textbox2.Text;
e.SSN = textbox3.Text;
return e;





You can use a "full blown" version of
Employee (as in public class Employee)
for a
EmployeeStreamLined

but the reason I do it...is so that when I am populating a form (with
Employee data), the Employe data may contain child collections of
Depts
JobTitles

so the "Employee" object is bloated...when I need to pull the update.

This is my style. I know not every likes it, or agrees with it.

My approach is about the objects I send "along the wire" in WCF. I want
them to be as small as possible.....no bloat.


You might just want to create an Employee object.....since remoting isn't an
issue for you.

...
 
This is what i did since it was a bit easier without having to
do too many changes.

/* from the data form*/
public string uName { get { return textBoxName.Text; } }
public string uAddress { get { return textBoxAddress.Text; } }
public string uNotes { get { return textBoxNotes.Text; } }


/* from my main form */
using (frmData GetData = new frmData())
{
GetData.ShowDialog();
if (GetData.uName != "")
{
UserName = GetData.uName;
UserAddress = GetData.uAddress);
UserNotes = GetData.uNotes);
}
}

Is this the best way to do it? I think Pete suggested doing something like
this
too.

I didn't use a class because I couldn't figure out how to place the values
in the
class, and then return the class back to the main form. In my data form
there is
no code with the exception of the three lines I pasted above.
 
This is what i did since it was a bit easier without having to
do too many changes.

/* from the data form*/
        public string uName { get { return textBoxName.Text; } }
        public string uAddress { get { return textBoxAddress.Text;} }
        public string uNotes { get { return textBoxNotes.Text; } }

/* from my main form */
            using (frmData GetData = new frmData())
            {
                GetData.ShowDialog();
                if (GetData.uName != "")
                {
                    UserName = GetData.uName;
                    UserAddress = GetData.uAddress);
                    UserNotes = GetData.uNotes);
                }
            }

Is this the best way to do it?  I think Pete suggested doing something like
this
too.

I didn't use a class because I couldn't figure out how to place the values
in the
class, and then return the class back to the main form.  In my data form
there is
no code with the exception of the three lines I pasted above.











- Show quoted text -

Hi,

I suggest you get a book of C# programming, you are asking basic
questions that are better explained in a sequencial order, like in a
book
 
This is what i did since it was a bit easier without having to
do too many changes.

/* from the data form*/
public string uName { get { return textBoxName.Text; } }
public string uAddress { get { return textBoxAddress.Text; } }
public string uNotes { get { return textBoxNotes.Text; } }

/* from my main form */
using (frmData GetData = new frmData())
{
GetData.ShowDialog();
if (GetData.uName != "")
{
UserName = GetData.uName;
UserAddress = GetData.uAddress);
UserNotes = GetData.uNotes);
}
}

Is this the best way to do it? I think Pete suggested doing something like
this
too.

I didn't use a class because I couldn't figure out how to place the values
in the
class, and then return the class back to the main form. In my data form
there is
no code with the exception of the three lines I pasted above.

Hi Steve
Thats the best way to do it.
You'll find the same thing in C# books.
Its the most efficient and least complicated.
As an example look at the System.Windows.Forms.FontDialog class.
It has a public property Font and when the ShowColor property is true
you can get the selected color in the Color property.

So go ahead with the code.
 
Thanks a lot Pete,

And also for showing me how to actually return the values using
a class. Now I know how to do them both.


Thanks again

Steve




class PromptRecord
{
public readonly string uName;
public readonly string uAddress;
public readonly string uNotes;

public PromptRecord(string uName, string uAddress, string uNotes)
{
this.uName = uName;
this.uAddress = uAddress;
this.uNotes = uNotes;
}
}

Then in your dialog class, instead of the properties you showed, have
something like this:

public PromptRecord Data
{
get { return new PromptRecord(textBoxName.Text,
textBoxAddress.Text, textBoxNotes.Text); }
}

Finally, in the code that shows the dialog:

using (frmData GetData = new frmData())
{
if (GetData.ShowDialog() == DialogResult.OK)
{
PromptRecord data = GetData.Data;

UserName = data.uName;
UserAddress = data.uAddress);
UserNotes = data.uNotes);
}
}

(I made the fields "readonly" as an alternative to writing read-only
properties for the class...it seems to me that the data for sure should be
read-only, and putting properties in there seems like overkill for
something that's basically just a bucket to stick some data into for later
retrieval).

Note that if the client of the dialog class isn't careful to retrieve the
Data property just once, multiple instances of the PromptRecord class wind
up getting created. This is a theoretical performance issue, but in
reality it's negligible. That said: you could add some complexity to deal
with that possibility, but also it's a good example of how the "one
property per field" paradigm is simpler and less prone to accidental
misuse.

Pete
 

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

Back
Top