473,807 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring/populating variable arrays in ASP.NET???

Sorry if this is the wrong group. I tried to find the one I thought
would be most relevant.

I'm an old PHP guy, who knows little about asp and NOTHING about
asp.net, but need to learn at least enough to convert a favorite PHP
script to work on an ASP.NET site.

I'm experimenting with simple example scripts that will help me learn
how to implement each "piece" of the puzzle.

Doing well so far... one piece of the puzzle at a time.
I need to create a simple funtion that will check the IP Address of a
visitor against an "array variable" of banned IPs.

In asp, it saeems simple enough. Here's the include that contains the
function to be called (in .asp):

############### ###############
<%
Dim sIPAddress
sIPAddress = Request.ServerV ariables("HTTP_ X_FORWARDED_FOR ")
if sIPAddress = "" then
sIPAddress = Request.ServerV ariables("REMOT E_ADDR")
End If
%>

<script language="VB" runat=server>

function BannedIP(sIPAdd ress)
Dim sBanned
sBanned =
Array("69.202.1 23.157","216.23 9.39.5","216.23 9.37.5","216.23 9.37.104","2
16.155.200.231" ,"216.155.200.2 32","216.155.20 0.233","216.155 .200.234")

Dim i
For i = 0 to UBound( [sBanned] )
If selCriteria(i,1 ) = cstr(sBanned) Then
BannedIP = TRUE
Else
BannedIP = false
End Function

</script>
############### ############### ##

The function is called from an existing .aspx page thus:
############### ############### ##
if BannedIP(sIPAdd ress) then
' show it the "SORRY" page
Server.Execute( "sorry.aspx ")
' Then STOP! (same as PHP's "exit;" statement ???)
Response.End
' otherwise it's cool, go ahead and display this page below
End If
############### ############### ##

ASP.NET doesn't like this at all!
It tells me: "'Array' is a type and cannot be used as an expression."
:-(

What is the proper syntax in ASP.NET for:
variable = Array("value1", "value2","value 3")

I would simply use the function as is, written in asp, but the pages
are VERY .aspx and choke on it.
ALSO: My next piece of the puzzle will be to tackle this one, using the
same test script:
If I want e to ban a violator with a dynamic IP, how do I deal with
entire C-blocks (e.g.: BannedIP is TRUE if sIPAdress contains
123.45.6.*)
Many TIA
:-)
Friday

--
############### ############### #######
"The people cannot be all, & always well informed. The part which is wrong will
be discontented in proportion to the importance of the facts they misconceive.
If they remain quiet under such misconceptions it is a lethargy, the forerunner
of death to the public liberty. What country before ever existed a century & a
half without a rebellion? & what country can preserve it's liberties if their
rulers are not warned from time to time that their people preserve the spirit
of resistance? Let them take arms... The tree of liberty must be refreshed from
time to time with the blood of patriots & tyrants"
-- Thomas Jefferson
############### ############### #######
Jul 21 '05
29 4058
Friday,

I never use the notation from you. I use for Net1.1

Dim sBanned() As String =
("69.202.123.15 7","216.239.39. 5","216.239.37. 5","216.239.37. 104","2
16.155.200.231" ,"216.155.200.2 32","216.155.20 0.233","216.155 .200.234")
For i as integer = 0 to sBanned.length - 1
If sBanned(i) = selCriteria Then
BannedIP = True
Else
BannedIP = False
End Function

I hope this helps,

Cor
Jul 21 '05 #11
doh,

my previous code would only test the last one. This is better in my opinion.

\\\\
Dim sBanned() As String =
("69.202.123.15 7","216.239.39. 5","216.239.37. 5","216.239.37. 104","2
16.155.200.231" ,"216.155.200.2 32","216.155.20 0.233","216.155 .200.234")
For i as integer = 0 to sBanned.length - 1
If sBanned(i) = selCriteria Then
BannedIP = True
'take action
exit for
end if
End for
////

I hope this helps,

Cor

Jul 21 '05 #12
Cor Ligthert <no************ @planet.nl> wrote:
I never use the notation from you. I use for Net1.1

Dim sBanned() As String =
("69.202.123.15 7","216.239.39. 5","216.239.37. 5","216.239.37. 104","2
16.155.200.231" ,"216.155.200.2 32","216.155.20 0.233","216.155 .200.234")
For i as integer = 0 to sBanned.length - 1
If sBanned(i) = selCriteria Then
BannedIP = True
Else
BannedIP = False
End Function


Just to find an element within an array, I'd suggest using
Array.IndexOf. (In this case, Array.IndexOf(s Banned, selCriteria).) If
the return value is -1, the element isn't found. Otherwise, it is.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #13
Jon,
Just to find an element within an array, I'd suggest using
Array.IndexOf. (In this case, Array.IndexOf(s Banned, selCriteria).) If
the return value is -1, the element isn't found. Otherwise, it is.

I have never thought of using it in that way, however you are right, I will
remember it.

:-)

Cor
Jul 21 '05 #14
In article <e1************ **@TK2MSFTNGP14 .phx.gbl>, Cor Ligthert
<no************ @planet.nl> wrote:
doh,

my previous code would only test the last one. This is better in my opinion.

\\\\
Dim sBanned() As String =
("69.202.123.15 7","216.239.39. 5","216.239.37. 5","216.239.37. 104","2
16.155.200.231" ,"216.155.200.2 32","216.155.20 0.233","216.155 .200.234")
For i as integer = 0 to sBanned.length - 1
If sBanned(i) = selCriteria Then
BannedIP = True
'take action
exit for
end if
End for
////

I hope this helps,

Cor

GREAT advice from everyone!
Thank You.
:-)

But....

NOW I'm getting: "BC30198: ')' expected"
Where in the _WORLD_ does it expect a ')'???
(This little experiment doesn't seem that complicated 8-P)

If this were PHP, I would suspect a problem on the preceeding line:
"function BannedIP(sIPAdd ress as string) as Boolean"
hmmm...

Anyway, here's what I have on the included page with the function:
############### ########
<%
Dim sIPAddress
' check for proxies
sIPAddress = Request.ServerV ariables("HTTP_ X_FORWARDED_FOR ")
if sIPAddress = "" then
sIPAddress = Request.ServerV ariables("REMOT E_ADDR")
End If
%>

<script language="VB" runat=server>

function BannedIP(sIPAdd ress as string) as Boolean

' The following line is where the error occurs [expecting ')']
Dim sBanned() As String =
("69.202.123.15 7","216.239.39. 5","216.239.37. 5","216.239.37. 104","2
16.155.200.231" ,"216.155.200.2 32","216.155.20 0.233","216.155 .200.234")
' No line breaks, missing commas, or quotation marks in above line
For i as integer = 0 to sBanned.length - 1
If sBanned(i) = selCriteria Then
BannedIP = True
exit for
end if
End for

End Function
</script>
############### #########

And here is how the function is called from other .aspx pages:

############### #########
<!--#include file = "inc/IsBanned.aspx"-->
<%
if BannedIP(sIPAdd ress) THEN
' show it the GET LOST page
Server.Execute( "get-lost.aspx")
' Then STOP! (same as PHP's "exit;" statement ???)
Response.End
' otherwise it's cool, serve up the page below:
End If
%>

<%@ import Namespace="Stor eFront.StoreFro nt" %>
<%@ assembly name="StoreFron t" %>
blah... blah... blah....
############### #########

Thanks to everyone for their patience.
Hard to teach an old L.A.M.P. dog new trix.
(Who thought Windows would ever catch on????)
;-)

--
############### ############### #######
"The people cannot be all, & always well informed. The part which is wrong will
be discontented in proportion to the importance of the facts they misconceive.
If they remain quiet under such misconceptions it is a lethargy, the forerunner
of death to the public liberty. What country before ever existed a century & a
half without a rebellion? & what country can preserve it's liberties if their
rulers are not warned from time to time that their people preserve the spirit
of resistance? Let them take arms... The tree of liberty must be refreshed from
time to time with the blood of patriots & tyrants"
-- Thomas Jefferson
############### ############### #######
Jul 21 '05 #15
In article <MP************ ***********@msn ews.microsoft.c om>, Jon Skeet
[C# MVP] <sk***@pobox.co m> wrote:
Cor Ligthert <no************ @planet.nl> wrote:
I never use the notation from you. I use for Net1.1

Dim sBanned() As String =
("69.202.123.15 7","216.239.39. 5","216.239.37. 5","216.239.37. 104","2
16.155.200.231" ,"216.155.200.2 32","216.155.20 0.233","216.155 .200.234")
For i as integer = 0 to sBanned.length - 1
If sBanned(i) = selCriteria Then
BannedIP = True
Else
BannedIP = False
End Function


Just to find an element within an array, I'd suggest using
Array.IndexOf. (In this case, Array.IndexOf(s Banned, selCriteria).) If
the return value is -1, the element isn't found. Otherwise, it is.


That's very interestinf Jon.
Thx.

How exactly would I use time-saver? i.e.: where and how would I define
the strings contained within the array (array variable?)?

Just as shown above?
Then simply replace the "IF" statement with your examle?

Thnx Again
Friday

--
############### ############### #######
"The people cannot be all, & always well informed. The part which is wrong will
be discontented in proportion to the importance of the facts they misconceive.
If they remain quiet under such misconceptions it is a lethargy, the forerunner
of death to the public liberty. What country before ever existed a century & a
half without a rebellion? & what country can preserve it's liberties if their
rulers are not warned from time to time that their people preserve the spirit
of resistance? Let them take arms... The tree of liberty must be refreshed from
time to time with the blood of patriots & tyrants"
-- Thomas Jefferson
############### ############### #######
Jul 21 '05 #16
In article <5D************ *************** *******@microso ft.com>, John H
W <Jo****@discuss ions.microsoft. com> wrote:
I have not used Array as such. How I do it is:

Dim sIPAddress(8) as String
I would use 8 or some other amount that I would not exceed. I then use
another function to fill it from an XML or other "formated" file, passing the
array, i.e.,

brtn = FillBannedAddre sses(sIPAddress ())

Then declare the other function as
public function FillBannedAddre sses(ByRef sIPAddress() as string) as Boolean
I then return a true if filled (false if a problem)

In this other function, I would open the file, parse it and load the
addresses into the passed array (byref).

I am leaving work now or I would give you some more "pointers."

John H W


Thanks John.
Interesting, but seems pretty heavy on the CPU.
Still...
I'm curious. Where and how would I format the "list" to parse the known
IPs?
I don't need to learn XML too do I?

Gosh I remember when a little Perl (YUCK!) and a good knowledge of PHP
and MySQL would carry the day.
8-P

Enquiring minds want to know.

Thanks Againm
Friday

--
############### ############### #######
"The people cannot be all, & always well informed. The part which is wrong will
be discontented in proportion to the importance of the facts they misconceive.
If they remain quiet under such misconceptions it is a lethargy, the forerunner
of death to the public liberty. What country before ever existed a century & a
half without a rebellion? & what country can preserve it's liberties if their
rulers are not warned from time to time that their people preserve the spirit
of resistance? Let them take arms... The tree of liberty must be refreshed from
time to time with the blood of patriots & tyrants"
-- Thomas Jefferson
############### ############### #######
Jul 21 '05 #17
In article <09************ ***********@now here.org>, Friday
<fr****@nowhere .org> wrote:
In article <e1************ **@TK2MSFTNGP14 .phx.gbl>, Cor Ligthert
<no************ @planet.nl> wrote:
doh,

my previous code would only test the last one. This is better in my opinion.

\\\\
Dim sBanned() As String =
("69.202.123.15 7","216.239.39. 5","216.239.37. 5","216.239.37. 104","2
16.155.200.231" ,"216.155.200.2 32","216.155.20 0.233","216.155 .200.234")
For i as integer = 0 to sBanned.length - 1
If sBanned(i) = selCriteria Then
BannedIP = True
'take action
exit for
end if
End for
////

I hope this helps,

Cor

GREAT advice from everyone!
Thank You.
:-)

But....

NOW I'm getting: "BC30198: ')' expected"
Where in the _WORLD_ does it expect a ')'???
(This little experiment doesn't seem that complicated 8-P)

If this were PHP, I would suspect a problem on the preceeding line:
"function BannedIP(sIPAdd ress as string) as Boolean"
hmmm...

Anyway, here's what I have on the included page with the function:
############### ########
<%
Dim sIPAddress
' check for proxies
sIPAddress = Request.ServerV ariables("HTTP_ X_FORWARDED_FOR ")
if sIPAddress = "" then
sIPAddress = Request.ServerV ariables("REMOT E_ADDR")
End If
%>

<script language="VB" runat=server>

function BannedIP(sIPAdd ress as string) as Boolean

' The following line is where the error occurs [expecting ')']
Dim sBanned() As String =
("69.202.123.15 7","216.239.39. 5","216.239.37. 5","216.239.37. 104","2
16.155.200.231" ,"216.155.200.2 32","216.155.20 0.233","216.155 .200.234")
' No line breaks, missing commas, or quotation marks in above line
For i as integer = 0 to sBanned.length - 1
If sBanned(i) = selCriteria Then
BannedIP = True
exit for
end if
End for

End Function
</script>
############### #########

And here is how the function is called from other .aspx pages:

############### #########
<!--#include file = "inc/IsBanned.aspx"-->
<%
if BannedIP(sIPAdd ress) THEN
' show it the GET LOST page
Server.Execute( "get-lost.aspx")
' Then STOP! (same as PHP's "exit;" statement ???)
Response.End
' otherwise it's cool, serve up the page below:
End If
%>

<%@ import Namespace="Stor eFront.StoreFro nt" %>
<%@ assembly name="StoreFron t" %>
blah... blah... blah....
############### #########

OK! Now I'm getting somewhere.
When I replace the () in the array with containers {}, the compiler
gets happy.
:-)
(WTF?! Oh well.)
Thenit tells me I need a NXT statement to balance the FOR

OK, I'm down with that.

So, now I have:
############### ############### ###
<script language="VB" runat=server>

Dim sBanned() As String =
("69.202.123.15 7","216.239.39. 5","216.239.37. 5","216.239.37. 104","2
16.155.200.231" ,"216.155.200.2 32","216.155.20 0.233","216.155 .200.234")

selCriteria = sIPAddress
For i as integer = 0 to sBanned.length - 1
If sBanned(i) = selCriteria Then
BannedIP = True
exit for
end if
Next
End for

End Function
</script>
############### ############### ###########

And the sever tells me:
Name 'selCriteria' is not declared.

I was under the impression selCriteria was a built-in function...
No?

So, I add:
Dim selCriteria As String

Good! Good!
SH*T

Error: 'End' statement not valid.

Something out of order. Let's get rid of the "end for" since we
al;ready have "exit for"

and...
and...
something is happening!
Lookslike it might be working.

I'll get back to you after I plkug my oen IP in there and see what
happens.

Keep your fingers crossed!
:-)
TIA
AGAIN!
:-)
Friday

--
############### ############### #######
"The people cannot be all, & always well informed. The part which is wrong will
be discontented in proportion to the importance of the facts they misconceive.
If they remain quiet under such misconceptions it is a lethargy, the forerunner
of death to the public liberty. What country before ever existed a century & a
half without a rebellion? & what country can preserve it's liberties if their
rulers are not warned from time to time that their people preserve the spirit
of resistance? Let them take arms... The tree of liberty must be refreshed from
time to time with the blood of patriots & tyrants"
-- Thomas Jefferson
############### ############### #######
Jul 21 '05 #18
In article <09************ ***********@now here.org>, Friday
<fr****@nowhere .org> wrote:

<GREAT BIF SNIP>
And the sever tells me:
Name 'selCriteria' is not declared. So, I add:
Dim selCriteria As String
selCriteria = sIPAddress Good! Good!
SH*T

Error: 'End' statement not valid.

Something out of order. Let's get rid of the "end for" since we
already have "exit for"

and...
and...
something is happening!
Lookslike it might be working.

I'll get back to you after I plkug my oen IP in there and see what
happens.

Keep your fingers crossed!
:-)


OK, it works after a little fine-tuning.
Survey says:
############### ############### #
Dim sBanned() As String =
{"216.239.39.5" ,"216.239.37.5" ,"216.239.37.10 4","69.202.123. 157"}

' My IP for testing is: "69.202.123.157 ",

Dim selCriteria as String
selCriteria = sIPAddress
For i as integer = 0 to sBanned.length - 1
If sBanned(i) = selCriteria Then
BannedIP = True
exit for
Else
BannedIP = FALSE
end if
Next
End Function
############### ############### #

NOW THEN....

One final lesson I need to learn in my little experiment.

Suppose I want to include someone with a dynamic IP from
"69.202.123 .*",
So I want to match IPs against the entire C-block: "69.202.123.".. .
Without too much convolution, how do I tell the above function to check
for that?
Or do I simply make the last entry in my array "69.202.123 "?
With or without the final "."?
With or without a wildcard?

In PHP, I do it like this (note the last two enties in array):

$Banned = "^216.239.39.5| ^216.239.37.8|^ 216.239.37|^69. 202.123";

Thanks again for everyone's help and TIA once again.
:-)
Friday

--
############### ############### #######
"The people cannot be all, & always well informed. The part which is wrong will
be discontented in proportion to the importance of the facts they misconceive.
If they remain quiet under such misconceptions it is a lethargy, the forerunner
of death to the public liberty. What country before ever existed a century & a
half without a rebellion? & what country can preserve it's liberties if their
rulers are not warned from time to time that their people preserve the spirit
of resistance? Let them take arms... The tree of liberty must be refreshed from
time to time with the blood of patriots & tyrants"
-- Thomas Jefferson
############### ############### #######
Jul 21 '05 #19
Friday <fr****@nowhere .org> wrote:
How exactly would I use time-saver? i.e.: where and how would I define
the strings contained within the array (array variable?)?

Just as shown above?
Then simply replace the "IF" statement with your examle?


Yup:

If Array.IndexOf(s Banned, selCriteria) = -1 Then
.... do stuff you want to do when it's not found ...
Else
.... do stuff you want to do when it's found ...
End If

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2073
by: JNY | last post by:
Hello, Is it possible to declare an array with variable indeces? i.e. int x = 4; int myArray; for (j = 0;j < 5;j++) {
34
2137
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least enough to convert a favorite PHP script to work on an ASP.NET site. I'm experimenting with simple example scripts that will help me learn how to implement each "piece" of the puzzle.
4
1658
by: Peter Duniho | last post by:
On Thu, 14 Aug 2008 18:56:00 -0700, Phill <Phill@discussions.microsoft.comwrote: For future reference, if you are asking for help with an error (compile or execution), you really should post the complete text of the error and be specific about when and where it happens. That said, in your code it's clear what's wrong:
0
9720
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10112
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.