Riddhi Valecha

Riddhi Valecha

  • 441
  • 3.3k
  • 415k

Passing multiple class objects in a method as parameter.

Feb 21 2015 3:11 AM
Hi all....

Please help me out -

My Scenario -
I have total 3 class files -
1. AnnTblProperties
public class Announcements
{
 public String Announcement
  {
  get;
  set;
  }
  public DateTime CreatedDate
  {
  get;
  set; 
 }
}
2. NewsTblProperties
public class News
{
 public String News
  {
  get;
  set;
  }
  public DateTime CreatedDate
  {
  get;
  set; 
 }
}
3. InsertMethods
public class INsert{
public Datatable InsertValues (int Table, Announcement ann, News news)
{
 if(table ==1 )
  {
  Insert in Announcement(Announcement, CreatedDate) values (ann.Announcement, ann.CreatedDate);
 }
 if(table ==2 )
  {
  Insert in News(News, CreatedDate) values (news.News, news.CreatedDate);
 }
 
}
}
---
Table structure of Announcements and News are same - (In Annpuncement table, Column "News" is "Announcement").
ID(PK, not Null)
News
CreatedDate
1
Item-1
3-Jan-2015
2
Item-2
16-Jan-2015
3
Item-3
4-Feb-2015
Now, from front-end, the user selects the table name from dropdownlist.

Say, if user selects "Announcements" from dropdownlist, then following steps takes place -
1. The below method is called (Validations done)
public Announcement SetValuesAnnouncement()
{
Announcements ann = new Announcements(); // object of class Announcements
ann.Announcement = txt_ann.text.trim();
ann.CreatedDate = System.datetime.now;
return ann;
}
Insert insrt = new Insert();
insrt(1,ann,null);
------------
Now,
The tables will be increased in future (structure will be same).
My query -

How do I pass just 2 values to the method InsertValues() ??

How do I pass many objects of classes in a single variable ??

My intend is to reduce the number of parameters.

Can we use collections like arrays or is there some other ways??

PLease Help !!
------




Answers (7)