are you using WebForms, ASP.NET MVC 3, or something else?
are you talking about property get; and set;?
please show us the relevant code that you have already tried ... at least for me, you really have not provided enough detail.
what is it that you do not know but need to know in order to achieve your goal(s)?
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
See previous post for code. I am using Web Developer 2010 with C#. I have not tried anything because I don't know how to tell Get and Set properties to retrieve and set a single row.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
It is a good idea to only expose from your class what needs to be exposed. Instead of exposing the List you can just expose get/set for individual elements You may want look in to the concept of an indexer. Roughly, the code for an indexer would be something
like
public Email_Record this[int recordNum]
{
get { return EmailRecs[recordNum]; }
set { EmailRecs[recordNum] = value; }
}
Note that the property is called 'this', that is what makes it an indexer. The sample assumes you want an int as your index, you can use any type that makes sense. You may want to choose a different type for the private backing store depending on the type
of the parameter to the indexer. For example, if you want to index with a string then a Dictionary may make more sense than a List, if you want to allow entries to be added a random locations then, again, a Dictionary may be a better choice. Give some thought
to what should happen if someone attempts to retrieve an element which has not been set or attempts to store an element beyond the limits of your backing store.
With the indexer in place the elements of your list can be accessed as though they were elements of the enclosing class.
harrytu@clea...
Member
106 Points
124 Posts
Get and set forlists
Feb 19, 2012 01:55 PM|LINK
I have several lists that I want to use with Get and Set.
How do I do this so I can return a single row of the list or set values in a single row?
MetalAsp.Net
All-Star
112162 Points
18252 Posts
Moderator
Re: Get and set forlists
Feb 19, 2012 01:58 PM|LINK
Can you show a sample of what you mean by "lists"?
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Get and set forlists
Feb 19, 2012 02:02 PM|LINK
@ harrytu@clea...
is this a homework assignment?
are you using WebForms, ASP.NET MVC 3, or something else?
are you talking about property get; and set;?
please show us the relevant code that you have already tried ... at least for me, you really have not provided enough detail.
what is it that you do not know but need to know in order to achieve your goal(s)?
g.
harrytu@clea...
Member
106 Points
124 Posts
Re: Get and set forlists
Feb 19, 2012 08:07 PM|LINK
public class Email_Record
{public int EmailId;
public string EmailAddress;
}
public static List<Email_Record> EmailRecs = new List<Email_Record();
I want to make the list private and use the Get and Set properties to access and set the
rows in the list.
harrytu@clea...
Member
106 Points
124 Posts
Re: Get and set forlists
Feb 19, 2012 08:10 PM|LINK
See previous post for code. I am using Web Developer 2010 with C#. I have not tried anything because I don't know how to tell Get and Set properties to retrieve and set a single row.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Get and set forlists
Feb 19, 2012 08:45 PM|LINK
@ harrytu@clearwire.net
it appears that you need to learn the basics of c# ... i suggest that you begin here:
http://msdn.microsoft.com/en-us/vstudio/hh388566
g.
P.S.: this website has many excellent, easy to understand examples:
http://www.dotnetperls.com/property
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Get and set forlists
Feb 19, 2012 08:52 PM|LINK
@ harrytu@clearwire.net
Paul Linton recommends this free resource:
http://www.charlespetzold.com/dotnet/index.html
Charles Petzold
This free on-line 267-page book is an introduction to C# and the Microsoft .NET Framework for programmers who have experience with C or C++.
Check it out: Charles Petzold is one of the all time great programmers.
Do not worry if you do not know c or c++ ... you'll still find the book useful.
For accessing data ... go here: http://msdn.microsoft.com/en-us/data
g.
Paul Linton
Star
13421 Points
2535 Posts
Re: Get and set forlists
Feb 19, 2012 09:12 PM|LINK
It is a good idea to only expose from your class what needs to be exposed. Instead of exposing the List you can just expose get/set for individual elements You may want look in to the concept of an indexer. Roughly, the code for an indexer would be something like
public Email_Record this[int recordNum] { get { return EmailRecs[recordNum]; } set { EmailRecs[recordNum] = value; } }Note that the property is called 'this', that is what makes it an indexer. The sample assumes you want an int as your index, you can use any type that makes sense. You may want to choose a different type for the private backing store depending on the type of the parameter to the indexer. For example, if you want to index with a string then a Dictionary may make more sense than a List, if you want to allow entries to be added a random locations then, again, a Dictionary may be a better choice. Give some thought to what should happen if someone attempts to retrieve an element which has not been set or attempts to store an element beyond the limits of your backing store.
With the indexer in place the elements of your list can be accessed as though they were elements of the enclosing class.
harrytu@clea...
Member
106 Points
124 Posts
Re: Get and set forlists
Feb 23, 2012 01:39 PM|LINK
how do Im get a record into the list?
I tried this:
public class Email_Records{
private static List<Email_Record> EmailRecs = new List<Email_Record>();
public Email_Record this[int recordNum]
{
get { return EmailRecs[recordNum]; }
set { EmailRecs[recordNum] = value; }
}}
in my program:
Email_Record eRec = new Email_Record();
Email_Records eRecs = new Email_Records();
eRec.EmailId=1;
eRec.EmailAddress = hulmer@cfl.rr.cm;
eRecs[0] = eRec;
I get an indexing error. Do I have to size the list?
Paul Linton
Star
13421 Points
2535 Posts
Re: Get and set forlists
Feb 23, 2012 07:54 PM|LINK
Don't use a List for your backing store. From what you have told us so far it seems that a Dictionary would work better.