473,326 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Pass a list in C#

158 100+
Right now i have a C# program that has two forms frmHoliday, and frmNewHoliday.

I need to pass a list of holidays from frmNewHoliday to frmHoliday. Right now it works using the Tag property.


(From frmHoliday)
Expand|Select|Wrap|Line Numbers
  1.  private void btnAdd_Click(object sender, EventArgs e)
  2.         {
  3.             frmNewDate newHolidayForm = new frmNewDate();
  4.             newHolidayForm.CreateHoliday();
  5.             Holiday newHoliday = (Holiday)newHolidayForm.Tag;
  6.             if (newHoliday != null)
  7.                 lstDate.Items.Add(newHoliday.Display());
  8.         }
  9.  
-----------------------------------------------------------------------------------------
(From frmNewHoliday)
Expand|Select|Wrap|Line Numbers
  1. public void CreateHoliday()
  2.         {
  3.             this.ShowDialog();
  4.             this.Tag = aHoliday;
  5.         }
  6.  
  7.  
Thanks
Apr 14 '08 #1
5 3137
r035198x
13,262 8TB
You can pass variables between objects using the contructor or through method parameters.
Apr 14 '08 #2
Curtis Rutland
3,256 Expert 2GB
Right now i have a C# program that has two forms frmHoliday, and frmNewHoliday.

I need to pass a list of holidays from frmNewHoliday to frmHoliday. Right now it works using the Tag property.


(From frmHoliday)
private void btnAdd_Click(object sender, EventArgs e)
{
frmNewDate newHolidayForm = new frmNewDate();
newHolidayForm.CreateHoliday();
Holiday newHoliday = (Holiday)newHolidayForm.Tag;
if (newHoliday != null)
lstDate.Items.Add(newHoliday.Display());
}

-----------------------------------------------------------------------------------------
(From frmNewHoliday)
public void CreateHoliday()
{
this.ShowDialog();
this.Tag = aHoliday;
}



Thanks
There are several ways to go about this. You could create a public member of frmNewHoliday as a list or whatever array you would use. Then you could reference it the same way you reference the tag. Also, you could create a public method called GetHolidayList or whatever, and have it return your list. Then you could call that from your other form. Either way will work fine.
Apr 14 '08 #3
Jollywg
158 100+
There are several ways to go about this. You could create a public member of frmNewHoliday as a list or whatever array you would use. Then you could reference it the same way you reference the tag. Also, you could create a public method called GetHolidayList or whatever, and have it return your list. Then you could call that from your other form. Either way will work fine.

Could you give me an example of how to do this?
Thanks
Apr 14 '08 #4
Curtis Rutland
3,256 Expert 2GB
Could you give me an example of how to do this?
Thanks
Sure.
From frmNewHoliday.cs
Expand|Select|Wrap|Line Numbers
  1. public partial class frmNewHoliday : Form
  2. {
  3.     //this would be the public member
  4.     public List<string> HolidayList;
  5.     //this would be the public method.
  6.     public List<string> GetHolidays()
  7.     {
  8.         return HolidayList;
  9.     }
  10.     //either way is ok, but you only need to do one of them.
  11.  
  12.     public frmNewHoliday()
  13.     {
  14.         InitializeComponent();
  15.     }
  16.  
  17.     private void frmNewHoliday_FormClosed(object sender, FormClosedEventArgs e)
  18.     {
  19.         this.DialogResult = DialogResult.OK;
  20.     }
  21. }
  22.  
From frmHoliday.cs, wherever you want to open the new holiday dialog
Expand|Select|Wrap|Line Numbers
  1. List<string> newHolidays;
  2. frmNewHoliday fnh = new frmNewHoliday();
  3. DialogResult res = fnh.ShowDialog();
  4. if (res = DialogResult.OK)
  5. {
  6.     //heres two ways of doing it.
  7.     newHolidays = fnh.HolidayList;
  8.     //or, if you do it with a method...
  9.     newHolidays = fnh.GetHolidays();
  10. }
  11.  
Ok, what I did here is show you the bare bones of two methods. I chose a List<string> because I have no idea of what kind of object you need to pass. But if you need a list of strings, this is a good object. But you can do this with any kind of object. You can either use the public method that returns this type, or you can expose a public member of this type. Also, I used a DialogResult because you might want to cancel. Instead of on FormClosed, you could set the DialogResult when you push an ok button. Then in FormClosed, you can set the dialogResult to something else.

But if you said OK, then I showed you both ways to get to that list. I like exposing the public member. But if you are going to use a method to generate your list anyway, you might as well just make that public and get it's value once the form is closed. Either way works pretty much the same.
Apr 14 '08 #5
Jollywg
158 100+
Thanks a bunch that really helps!
Apr 14 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: user | last post by:
When trying to pass the contents from one list to another this happens: list = list1 = list print list1 list.append(7) print list1
10
by: Doug Jordan | last post by:
I am fairly new to Python. This should be an easy answer but I cannot get this to work. The code is listed below. I know how to do this in C, Fortran, and VB but it doesn't seem to work the same...
2
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
0
by: Zlatko Matiæ | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
6
by: Vern | last post by:
I'd like to make the following a generic method that all my forms can call to validate all the fields on the form. So how do I pass the form object that is represented as "this" in the following...
13
by: andrea | last post by:
Sorry for the stupid question, I know, but sometimes is necessary starts from the basic. I don't know how to pass the result of a method generated from a DAL class to a BL class returning the...
3
by: erikcw | last post by:
Hi, I'm getting the following error when I try to pass a list into a function. My List: crea = Traceback (most recent call last): File "wa.py", line 118, in ? curHandler.walkData()
12
by: ArunDhaJ | last post by:
Hi Friends, Is it possible to pass a table as a parameter to a funtion. whos function declaration would look some thing like this.... ALTER FUNCTION TempFunction (@TempTable TABLE, @nPId INT) ...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.