Understand C# code

Hello Experts,
I am trying to understand the following line of code.  Any idea what does it do?  Thank you very much in advance.

Thank you

Request.QueryString.GetKey(0)
RadhaKrishnaKiJayaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
This basically means give me the first value of the first querystring item.

So for example, I made a new asp.net website and changed the link of the about page to:
<li><a runat="server" href="~/About?ImAQueryString=SomeValue">About</a></li>

Open in new window


If I browse to the About page from this link I get the following -Capture.JPG
Which is produced by the following code:
Public Class About
    Inherits Page

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
		Literal1.Text = String.Format("The first query key is: {0}", Request.QueryString.GetKey(0))
		Literal2.Text = String.Format("The first query value is: {0}", Request.QueryString.Get(Request.QueryString.GetKey(0)))
	End Sub
End Class

Open in new window


*WARNING* - If the page is loaded without passing a querystring, you will get an error (but this can be avoided with the use of a try...catch)

-saige-
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadhaKrishnaKiJayaAuthor Commented:
Thank you it_saige for your time and help.  Yes, you are right.  But still I am not able to understand the logic.  Below is the code.  It returns 3 laters.  I don't understand where from the values are coming.  Please let me know how it happens.  Thank you again!

switch(Request.QueryString.GetKey(0))
{
      case "c":
      case "f":
      case "z":
      default:
}
0
it_saigeDeveloperCommented:
In your case, the GetKey returns the first key (which by the statement could be c, f, or z).  From the posted code you only care about the key; e.g. -

http://www.somesite.com/somepage.aspx?c=4

Would cause the following in your logic:
switch(/* Request.QueryString.GetKey(0) gets replaced with*/ "c")
{
      case "c":
            // We end up here and perform any statements herein.
            // In order to get the value associated with "c".
            var value = Request.QueryString.Get("c")
            // value = 4.
            break;
      case "f":
            break;
      case "z":
            break;
      default:
            break;
}

Open in new window

-saige-
0
RadhaKrishnaKiJayaAuthor Commented:
I am trying to understand where those values "c", "f", "z" are coming from.  Because I don't see anywhere any query is passed during the call.  Also please tell me under what scenario it should be used.  Sorry for the miscommunication.  Thank you.
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you  it_saige.   It worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.