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

F

Friday

John H said:
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 = FillBannedAddresses(sIPAddress())

Then declare the other function as
public function FillBannedAddresses(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
#####################################
 
F

Friday

Friday said:
Cor Ligthert said:
doh,

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

\\\\
Dim sBanned() As String =
("69.202.123.157","216.239.39.5","216.239.37.5","216.239.37.104","2
16.155.200.231","216.155.200.232","216.155.200.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(sIPAddress 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.ServerVariables("HTTP_X_FORWARDED_FOR")
if sIPAddress = "" then
sIPAddress = Request.ServerVariables("REMOTE_ADDR")
End If
%>

<script language="VB" runat=server>

function BannedIP(sIPAddress as string) as Boolean

' The following line is where the error occurs [expecting ')']
Dim sBanned() As String =
("69.202.123.157","216.239.39.5","216.239.37.5","216.239.37.104","2
16.155.200.231","216.155.200.232","216.155.200.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(sIPAddress) 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="StoreFront.StoreFront" %>
<%@ assembly name="StoreFront" %>
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.157","216.239.39.5","216.239.37.5","216.239.37.104","2
16.155.200.231","216.155.200.232","216.155.200.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
#####################################
 
B

Brock Allen

This works for me:

Module Module1
Sub Main()
Dim sBan As String() = {"one", "two", "three"}
For Each s As String In sBan
Console.WriteLine(s)
Next
End Sub
End Module
 
F

Friday

Brock Allen said:
This works for me:

Module Module1
Sub Main()
Dim sBan As String() = {"one", "two", "three"}
For Each s As String In sBan
Console.WriteLine(s)
Next
End Sub
End Module
Yep! Dead on.
It wants "{" and NOT "("
Fussy little thing.
Thanks 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
#####################################
 
F

Friday

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.104","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
#####################################
 
J

Jon Skeet [C# MVP]

Friday said:
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(sBanned, 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
 
J

Jon Skeet [C# MVP]

Friday said:
OK! Now I'm getting somewhere.
When I replace the () in the array with containers {}, the compiler
gets happy.
:)
(WTF?! Oh well.)

<snip>

At this stage, I would *seriously* give up on just experimenting within
ASPX. It's really not a good way of learning any language unless you
already have a reasonably solid foundation and are just checking one
particular aspect - and even then I'd check the spec afterwards.

I suggest you start learning VB.NET (or C#) with small console apps,
and when you're happy with the language itself without the ASP.NET
adornments, *then* move on to web apps.
 
C

Cor Ligthert

Friday,

It seems that we all had sht in our eyes.
Dim sBanned() As String = {"69.202.123.157", "216.239.39.5",
"216.239.37.5", "216.239.37.104", "216.155.200.231", "216.155.200.232",
"216.155.200.233", "216.155.200.234"}

( = {
) = }

:)

I hope this helps,

Cor
 
F

Friday

Jon Skeet said:
<snip>

At this stage, I would *seriously* give up on just experimenting within
ASPX. It's really not a good way of learning any language unless you
already have a reasonably solid foundation and are just checking one
particular aspect - and even then I'd check the spec afterwards.

I suggest you start learning VB.NET (or C#) with small console apps,
and when you're happy with the language itself without the ASP.NET
adornments, *then* move on to web apps.

Thanks Jon,

Iunderstand, and agree with your reasoning 100%. One day, time
permitting, I would like to learn this intriguing and powerful language
more thoroughly.

What I'm striving to do at this point, is to convert a series of PHP
scripts I make my living with to work for a client who recently
migrated to IIS. The rest Ive researched (along with the basics), but
this one small part has become a sticking point.
Thanks to the help of all the knowledgeable and helpful folks here,
however, I'm 99% there.

Thanks Again and Have a GREAT Day (Week, Year, and Beyond),
:)
Sincerely,
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
#####################################
 
F

Friday

Cor Ligthert said:
Friday,

It seems that we all had sht in our eyes.
Dim sBanned() As String = {"69.202.123.157", "216.239.39.5",
"216.239.37.5", "216.239.37.104", "216.155.200.231", "216.155.200.232",
"216.155.200.233", "216.155.200.234"}

( = {
) = }

:)

I hope this helps,

Cor
THat was indeed the problem, Cor.
The only way I caught it was that I noticed than an earlier poster had
used the '{' indstead of the '(' So I tried it, not really expecting it
to make a diofference, but whether working on a car that stalls, or
programming, I always look to the seemingly insiognificant ("Stupid" if
you will) little things first. Or at least I try to make a practice of
that.
:)
Odd thing that it would make the difference.
But MS never was much for adhering to time-honored standards, were they?
;-)
Thanks Again for Taking the Time to Help,
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
#####################################
 
F

Friday

Jon Skeet said:
<snip>

At this stage, I would *seriously* give up on just experimenting within
ASPX. It's really not a good way of learning any language unless you
already have a reasonably solid foundation and are just checking one
particular aspect - and even then I'd check the spec afterwards.

I suggest you start learning VB.NET (or C#) with small console apps,
and when you're happy with the language itself without the ASP.NET
adornments, *then* move on to web apps.

Which would you recomnmend?
VB or C#
Or does it depend upon which language the site is built around?

Thx Agn
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
#####################################
 
F

Friday

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

Excelent Jon!
Fewer lines of code is GOOD.

This works well for my intended application:
##########################
function BannedIP(sIPAddress As String) As Boolean

Dim selCriteria as String
selCriteria = sIPAddress

Dim sBanned() As String =
{"68.142.230.181","66.228.164.108","202.165.98.144"}

If Array.IndexOf(sBanned, selCriteria) > -1 Then
IsMember = True
End If

End Function
##################################
Very tight.

Seems I don't even need the "Else = False"
Or would it be considered bad protocall to leave that out?

Many Thx
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
#####################################
 
J

Jon Skeet [C# MVP]

Friday said:
Which would you recomnmend?
VB or C#
Or does it depend upon which language the site is built around?

Personally I'd recommend C# as having a cleaner syntax and less baggage
from the past. Certainly if you're not familiar with VB to start with,
C# is the more natural choice for most people.
 
J

Jon Skeet [C# MVP]

Friday said:
This works well for my intended application:
##########################
function BannedIP(sIPAddress As String) As Boolean

Dim selCriteria as String
selCriteria = sIPAddress

Dim sBanned() As String =
{"68.142.230.181","66.228.164.108","202.165.98.144"}

If Array.IndexOf(sBanned, selCriteria) > -1 Then
IsMember = True
End If

End Function
##################################
Very tight.

Seems I don't even need the "Else = False"
Or would it be considered bad protocall to leave that out?

I'm afraid I don't know what VB does in terms of unspecified function
return values.

However, you could just change it to:

IsMember = (Array.IndexOf(sBanned, selCriteria) > -1)

Or at least, you could in C# and I assume you can in VB.NET...
 
C

Cor Ligthert

"Jon Skeet [C# MVP]"
However, you could just change it to:

IsMember = (Array.IndexOf(sBanned, selCriteria) > -1)

Or at least, you could in C# and I assume you can in VB.NET...

Function BannedIP(ByVal sIPAddress As String) As Boolean
Dim sBanned() As String = {"68.142.230.181", _
"66.228.164.108", "202.165.98.144"}
Return (Array.IndexOf(sBanned, sIPAddress) > -1)
End Function

I don't know if it goes still shorter.

Cor
 
F

Friday

Jon Skeet said:
I'm afraid I don't know what VB does in terms of unspecified function
return values.

However, you could just change it to:

IsMember = (Array.IndexOf(sBanned, selCriteria) > -1)

Or at least, you could in C# and I assume you can in VB.NET...

Hmmm.... nice.

--
#####################################
"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
#####################################
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top