Showing posts with label creating. Show all posts
Showing posts with label creating. Show all posts

Saturday, March 31, 2012

the asp.net equivalent of the "Request.Form" in asp

i am having problems with a simple web service i am creating in asp.net using C#. does anybody know what Request.Form is in asp.net.
cheers
much appreciatedI've never done classic ASP code, but I think what you're looking for is Request.Params[]

Should still be Request.Form. What is the problem that you are having?


Hi,
Request.Form method was used in classic ASP(3.0)
<form action="hello.aspx" method="post">
Your name: <input type="text" name="fname" size="20">
<input type="submit" value="Submit">
</form>
<%
dim fname
fname=Request.Form("fname")
Response.Write("Hello " & fname)
%>
As in Asp.net forms are posted to itself So Request.Form was not actively used in asp.net.
Iam still now able to understand why you require this in web services.


You can still use Request.Form, but you'll have to make sure the value exists before trying to use it. In Classic ASP, if the value didn't exist it would return an empty string, but in ASP.NET you get Nothing (in VB.NET, which I believe is null in C#).

The best way for OOP

Hi, I'm creating an application with a drop down menu that is populated by a database. The application has 4 other sections which also use the same drop down menu, however each of the 5 sections populate the drop down menu based on the section the user is in.

Currently I am using a single code behind file that queries the db and populates the drop down. I have all 4 sections using the same code behind file. Each section sends the code behind a parameter which allows the method to run the specific section query.

My questions are:

1. Is this the right way to do this?
2. Or should I create a class that all sections use? If I should create a class, what is the code behinds purpose, just to call the class?

I guess I'm kind of confused, it seems as though the code behind really is a class, I can place methods inside it and reuse them whereever I want. I would really like to be sure that I'm following OOP and doing this the correct way.

Thanks, DanBUMP, please help. Any takers? Is this a dumb question?
It's not a dumb question. It's a very good one.

You ask whether it is the "right" way to do it. In terms of OOP, then I'd suggest it's not the right way. The way you've described it, you have to pass in a parameter to your method in order for your code-behind to know how to populate the drop-down list. I'm guessing it's like this (using pseudo-code):

Namespace MyApp

Public Class Sections

Public Function GetList( ByVal strSection As String ) As ArrayList

Dim dstToReturn As New DataSet()

Select Case strSection

Case "homepage":
dstToReturn = SQLquery( "SELECT titles FROM Pages WHERE section='home'" )
' for the home page, we also add in a link to the sitemap and search pages
dstToReturn.Rows.Add( "sitemap" ... )
dstToReturn.Rows.Add( "seach" ... )

Case "help page":
dstToReturn = SQLquery( "SELECT titles FROM Pages WHERE section='help'" )
' for this page, we also add in a link to other resources
dstToReturn.Rows.Add( "forums" ... )
dstToReturn.Rows.Add( "other resources" ... )

Case "products page":
dstToReturn = SQLquery( "SELECT titles FROM Pages WHERE section='products'" )

Case "order page":
dstToReturn = SQLquery( "SELECT titles FROM Pages WHERE section='purchase'" )

End Select

Return dstToReturn

End Function
End Class
End Namespace

If I'm correct, then it is poor OOP design because it requires that you know,in advance, all of the ways in which your function might be used. If you add another section to your site, then you'd have to change your function.

This is bad because it violates thevery fundamental Open Closed Principle. To get an understanding of this and other principles of OOP, start atthis page. Follow its links andsearch for any terms you don't understand.

There are many principles and ideas about OOP, so I wouldn't recommend getting too concerned about finding the "right way". Get the gist of the Open Closed Principle, and then just start coding. You'll soon stub your toe in places where you find you should have done something differently, and you'll remember next time.

As a kind of litmus test though ... if you're using If/Then or Select Case statements, then you're probably not creating clean OOP code. Such loops are poor for performance and mean you are looking for thingsknown in advance.

Is that any help?
Dan, what you're doing is ok providing all sections are in the same page. if not then a seperate class may be useful. Also are you using a stored proc to return the data or dynamic sql in the code? if your rdbms supports sprocs then i would use one to improve performance (and to make maintenance easier).

To clear up your confusion, the code behind file IS a class. It inherits from the page class. Your aspx file in turn also compiles to a class that inherits from your code behind class. Cool hey!

Jon.

Saturday, March 24, 2012

The difference between creating an instance of an object

Could someone tell me in plain english was is the difference between


Dim MyObject As SomeType

and


Dim MyObject As New SomeType

and


Dim MyObject = New SomeType
Dim MyObject as SomeType = Creating a variable that holds an object of SomeType.

Dim MyObject as New SomeType = Creating and instantiating an object of SomeType.

Dim MyObject = New SomeType = Invalid syntax in vb.net. You need to specify the default type as variant is no longer supported in ASP.NET.
Ok, now I understand.

So if I have something like Fruit.Apple and Value is part of this as in Fruit.Apple.Value, I can do this...

Dim Food As Fruit.Apple

Food.Value

It looks like a way of shortening the name of an object.

Now, I have another question. How do you know when is the appropriate time to create and instantiate a new object rather than just use the above method?

Sorry if my question seems too easy, but I didn't do much of this stuff in regular ASP.
Well yes and no.

Lets think of this another way. Lets take your Fruit object. Your fruit has properties - color, weight, type, etc. You would create a class that describes this like so:


Public Class Fruit
Private _type As String
Private _color As String
Private _weight As Decimal

Public Property Type() As String
Get
Return _type
End Get
Set(ByVal Value As String)
_type = Value
End Set
End Property
' Add other properties for color and weight also

End Class

Then in your code you would instantiate a new Fruit object like:


Dim objFruit as New Fruit
'Then set properties of the fruit
With objFruit
.Type = "Apple"
.Color = "Red"
.Weight = 2.2
End With

It is appropriate to create a class whenever you are going to possibly reference the object from multiple places in your code.

Greg