What is the use of "Get" and "Set" and how it works?

I'm trying to figure out one part of the code which I found for paging. After looking at them and search other articles about this, I still couldn't understand how this "Get" and "Set" works. Can someone provide me a simple explaination how it works and why we need it? Thanks.

1        Public Property CurrentPage() As Integer
2            Get
3                Dim o As Object = ViewState("_CurrentPage")
4    
5                If o Is Nothing Then
6                    Return 0
7                Else
8                    Return o
9                End If
10           End Get
11   
12           Set(ByVal Value As Integer)
13               ViewState("_CurrentPage") = Value
14           End Set
15       End Property
 
My HomePage - www.findingsteve.net

"It is more blessed to give than to receive." Acts 20: 35 (NIV, Bible)

Progress - "Never discourage anyone...who continually makes progress, no matter how slow." Plato
0
ngaisteve1
5/29/2008 9:32:26 AM
asp.net.getting-started 91979 articles. 4 followers. Follow

15 Replies
1086 Views

Similar Articles

[PageSpeed] 49

Properties are useful when you want to provide several views of the same data. You probably only want to store it once but maybe you show it upteem ways.

0
Kiran
5/29/2008 9:51:49 AM

Using get/set for private variables is a good idea if you're creating a framework class that will be inherited from and used by other developers.  Interfaces are often needed up the inheritance tree as well as public interfaces to other classes.

0
Kiran
5/29/2008 9:53:20 AM

 Hi,

In properties we have two elements Get and Set

Get is used to retrieve the value of variable from the property

Set is used to insert the value of variable
 

 


Warm Regards:
Sitanshu

Please "Mark as Answer" if my post sort your problem
0
sbakshi
5/29/2008 10:05:59 AM

These are member function those are required to set the value at class level 

0
vijay
5/29/2008 10:12:35 AM

Hi,

Inorder to avoid the multiple declaration for the Same variable or Same object. Get and Set is used.

Because. There are some scenarios that u have to use the same variable for multiple pages. Example InvoiceCode in Point of Sales Based Application.

This can be aviod by setting get and set properties. Get is used for retreiving the value and set is assigning the value.

It is the class level access variables.

 

If this helps for u, Please click Mark as Answered

0
mohamed
5/29/2008 10:21:38 AM

 Check these url:

 

http://www.editorial.co.in/software/vbscript_property_let_get_set.php 

http://bytes.com/forum/thread551335.html 

http://msdn.microsoft.com/en-us/library/bzwdh01d(VS.71).aspx 

 


Mark as Answer if the post was useful to you

Rajesh Thangarasu
Microsoft Certified Professional
0
rajeshthangarasu
5/29/2008 10:58:43 AM

ngaisteve1:
If o Is Nothing Then 6 Return 0 7 Else 8 Return o 9 End If
 

In above code (part of the code you have posted)

there is no need of the If Else block as both returns the same thing.. 


Always remember to click “Mark as Answer” on the post that helps you. Thank you!


- Regards,
Paresh Jagatia
http://pareshjagatia.blogspot.com
0
pareshjagatiya
5/29/2008 1:11:39 PM

pareshjagatiya:

ngaisteve1:
If o Is Nothing Then 6 Return 0 7 Else 8 Return o 9 End If
 

In above code (part of the code you have posted)

there is no need of the If Else block as both returns the same thing.. 

Hard to spot, but one returns 0 (zero) if it's empty and the other returns the value of o.

To answer the question, these are used for getting the information from a private property of a class "getter" and for setting the private property of a class "setter"


Please remember to Mark As Answer if helpful

Regards,
Christiandev, MCPD Web & MCTS Windows

0
christiandev
5/29/2008 1:22:45 PM

 

christiandev:

pareshjagatiya:

ngaisteve1:
If o Is Nothing Then 6 Return 0 7 Else 8 Return o 9 End If
 

In above code (part of the code you have posted)

there is no need of the If Else block as both returns the same thing.. 

Hard to spot, but one returns 0 (zero) if it's empty and the other returns the value of o.

To answer the question, these are used for getting the information from a private property of a class "getter" and for setting the private property of a class "setter"

oops !! I really missed it. 


Always remember to click “Mark as Answer” on the post that helps you. Thank you!


- Regards,
Paresh Jagatia
http://pareshjagatia.blogspot.com
0
pareshjagatiya
5/29/2008 1:43:40 PM

pareshjagatiya:

 

christiandev:

pareshjagatiya:

ngaisteve1:
If o Is Nothing Then 6 Return 0 7 Else 8 Return o 9 End If
 

In above code (part of the code you have posted)

there is no need of the If Else block as both returns the same thing.. 

Hard to spot, but one returns 0 (zero) if it's empty and the other returns the value of o.

To answer the question, these are used for getting the information from a private property of a class "getter" and for setting the private property of a class "setter"

oops !! I really missed it. 

it really isn't the best variable naming :)


Please remember to Mark As Answer if helpful

Regards,
Christiandev, MCPD Web & MCTS Windows

0
christiandev
5/29/2008 1:55:30 PM

Hi all, thanks for all the prompt replies. Smile

I think now I got a bit of idea of this "Get" and "Set" thing.

But, isn't it same with using session?


My HomePage - www.findingsteve.net

"It is more blessed to give than to receive." Acts 20: 35 (NIV, Bible)

Progress - "Never discourage anyone...who continually makes progress, no matter how slow." Plato
0
ngaisteve1
5/30/2008 1:28:50 AM

Hi,

It isn't the same, lets say you want to store a list of properties for a user, for example...

- Name, surname, age, sex, email, address, etc etc

If you then want to store these in a database, by passing the values to a method. You would have to create a session object for all of these and there would be no typesafety on them. If you were to create a user class with all the above as the fields, then you would simply pass an instance of the object to the method to store the user in the DB and you wouldnt need to unbox any values to suit the databse, you wouldn't need to check the names of each session variable, as all you would need to do is use the 'getter' myUser.Name, myUser.Surname etc (provided you have passed myUser as a User object to the method)


Please remember to Mark As Answer if helpful

Regards,
Christiandev, MCPD Web & MCTS Windows

0
christiandev
6/2/2008 8:08:08 AM
CurrentPage is a inteeger property which get or set value from ViewState.Please check
Following code and read comments.
public int CurrentPage

{

get

{

//look for current page in ViewState

object o = this.ViewState["_CurrentPage"];

if (o == null)

return 0; // default to showing the first page

else

return (int)o;

}

set

{

this.ViewState["_CurrentPage"] = value;

}

}

protected void cmdPrev_Click(object sender, EventArgs e)

{

CurrentPage -= 1;

ItemsGet();

}

protected void cmdNext_Click(object sender, EventArgs e)

{

CurrentPage += 1;

ItemsGet();

}


Mark as Answer if this reply is useful to you


Pradeep Kamlapuri
B.Tech (Computer Science & Engineering)
India
0
kamlapuri
6/2/2008 8:36:45 AM

kamlapuri and christiandev, thanks for the reply.

Sorry, I don't know which is the answer to mark. Maybe the moderator can help.

Thanks everyone again.


My HomePage - www.findingsteve.net

"It is more blessed to give than to receive." Acts 20: 35 (NIV, Bible)

Progress - "Never discourage anyone...who continually makes progress, no matter how slow." Plato
0
ngaisteve1
6/4/2008 2:20:42 AM

Mark the one that helped answer your question, if both did, then you can mark both.


Please remember to Mark As Answer if helpful

Regards,
Christiandev, MCPD Web & MCTS Windows

0
christiandev
6/4/2008 6:31:54 AM
Reply:

Similar Artilces:

Get "Mail"/"Get "News" / "Write"/"Post" buttons?
If the "Get Mail" button text changed to "Get News", and "Write" changed to "Post" when TB's in the news-reading mode, it would save me and probably others from constantly forgetting to make the change before proceeding. :-) Or is there an extension to do this which I don't know about? Thanks! Bob Bob P wrote: > If the "Get Mail" button text changed to "Get News", and "Write" > changed to "Post" when TB's in the news-reading mode, it would save me > and probably others...

"Using" or "With"
Hi all Please can someone enlighten to me as regards the difference with the "Using" and "With" statement when accessing data - which is better, what are the limitations and/or any pointers. Many thanks. Regards DaveDavid WinchesterPlease mark as answer if this is the solution.  using gives you the ability to use the connection and it closes the connection directlly after you finish using it. and there is no need to try- cach - finaly. there is no limitation on using USING keywordMuhanad YOUNISMCSD.NETMy Blog || My Photos || LinkedIn I have a dataobject the re...

Using "+" or "||"
Using SQLAnywhere 5.5.04, I've gotten into the habit of using "||" in ISQL to indicate a string concatenation. I needed to paste my SQL statement into the PowerBuilder script painter for some embedded SQL, and PB didn't like the "||" very much at all. I changed it to "+" and it seems to be ok. Do these two operators indicate ~exactly~ the same thing? moin, afaik these two's are not the same! if you're using "||" and any term is NULL then in the resultstring the term will be ignored if you use "+" then the resu...

Precedence of "where" ("of", "is", "will")?
Nobody on #perl6 today could answer this one. Is: Str | Int where { $_ } the same as: (Str | Int) where { $_ } or: Str | (Int where { $_ }) ? Followup questions, Mr. President: What kind of operators are "where", "of", "is", and "will"? Is there a reason that S03 doesn't list them? What are their precedence(s)? -- Chip Salzenberg - a.k.a. - <chip@pobox.com> Open Source is not an excuse to write fun code then leave the actual work to others. Chip Salzenberg writes: &...

.ALLCOL("%COLUMN%", " ", ", ", ", ")
Do you know anyway for me to exclude a subset of columns returned by this function. We have two columns (rec_user and rec_datetime) which are in all of our tables, but when generating triggers I want automatically generate a script which does not include those two columns but does include all other columns in that table. Bruce I should add that I am using PD 9.0.0.580. Bruce "Bruce Lamb" <lamb.bruce@mayo.edu> wrote in message news:6HgI315nCHA.155@forums.sybase.com... > Do you know anyway for me to exclude a subset of columns returned by this > function. ...

"Get Mail" vs "Get All New Messages"
I have a few different email accounts set up in Thunderbird, and I'm using the unified view to view them. Recently, I noticed that the "Get Mail" button seems to only retrieve email for the first account (sometimes?). I'd like to click on that button and retrieve email for all of the accounts. I can get the behavior that I want consistently if I pull down the menu for "Get Mail" and select "Get All New Messages", but I'd prefer to get that in a single click if possible. I tried starting from scratch and recreating my profile, but I st...

Feature Request: "Get Extensions" and "Get Themes"
I think that when you click on the "Get Extensions" or "Get Themes" links, it should open depending on your default window-opening behavior. For example, all new windows for me open up in new tabs. However, when I click the "Get" links, they always open in new windows. I can't even find an extension to do this. How hard would this be to incorporate natively? ...

Where can I get the images used by VS2005 for "Edit, Update, Cancel"; "Select"; and "Delete"
I'm looking for small, elegant images to use in the CommandField of a GridView for Edit, Delete, and Select. Visual Studio 2005 uses images that would be perfect but I don't know where to get the files for these images so I can use them in my own project. From a GridView's Smart Tag, go to GridView Tasks | Edit Columns | Available Fields. Under CommandField there is a sub-tree with three images that are just what I need: "Edit, Update, Cancel"; "Select"; and "Delete". Does anyone know where I can find the files for these three images (.bmp, ,gif, etc.)?     I d...

quotes, quotes, quotes...
I am getting this error and I know what is causing it, but I have no idea how to fix it, any help would be great. The script steps through the /var/log/messages file on a linux server and puts The entries into a mysql database. However when it gets to the 'hlt' line in the messages file it just barfs. The single quotes are freaking it out. I know about quotes but not how to use in this situation. Thanks, Paul Error: May 27 17:53:00 localhost kernel: Checking 'hlt' instruction... OK. <----- doesn't like this in the messages file DBD::mysql::st exec...

Script to perform a "Get latest", then a "Build", then a "Publish"
I frequently have to copy out of source safe, the latest version of some source code. Then perform a build and then a publish. The publish is published to a local directory where I then copy it out to a server. This is a bit of a hassle to have to do all of this mannualy. What I would prefer to do is to use some type of script that would do this. Does anyone know if this is possible? I can manage copying to the server. I just need a script that I could kick off to do the following. 1. Copy the latest source code for the project out of source safe. 2. Perform a build of the projec...

Whan can i do? make "string" data type convert to "window" data type, and use a "window" name to get it ancestor "window name", thanks! i use pb5
Rich, With a string window name, you can open a window of that type if you need to. You could, of course, query that window object for any attributes. Another option is to use the LibraryExport function to export the definition of the window which is specified by the string. You can then parse the resulting file to get information on the ancestor object. HTH Sanjiv. "Rich" <windows_ce@netease.com> wrote in message news:2Xsr1WW0AHA.178@forums.sybase.com... > > > ...

"Net use" not working in application that runs as "system"
Hello, I am trying to create an application object to distribute a program which during the setup procedure creates two local users on the workstation. I tried to set up the application to run as an unsecure system user and start the setup.exe. This method worked fine for quite a number of apps in the past. However this app needs a drive mapping to a windows 2003 server to start the setup.exe. Apparently under windows 2003 it is no longer possible to use the "net use" command when running a script as system user. I get system error 1312 "A specified logon ses...

I'm getting an error using NavigateURL ='<%# Eval("Url") %>', it works when using NavigateURL="intro.aspx"
I'm getting an error using NavigateURL ='<%# Eval("Url") %>', it works when I use NavigateURL="intro.aspx" Server Error in '/orthoassist' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. Requested URL: /orthoassist/intro.aspx <asp:HyperLink runat="server" ID="hl1" Text=...

replace the "." with a ","
Oi.... I need to build a small programm in ASP.NET and chose to use C# for it.Now i got everything working but there's one little problem.the first textbox is a double. I need to make it so that when someone enters a "." then it gets replaced by a ","any ideas?Ghan  string blah = "4.2.2.2";blah = blah.Replace(".", ",");Ryan Ryan OlshanASPInsider | Microsoft MVP, ASP.NEThttp://ryanolshan.comHow to ask a question...

Web resources about - What is the use of "Get" and "Set" and how it works? - asp.net.getting-started

Resources last updated: 12/11/2015 10:24:34 AM