Is it possible to define same variable twice in classic ASP? - variables

Is it somehow possible to declare same variable in .asp file twice? Example below does not look very clever, but this is just an example and I have to sort it out.
Dim number : number = 1
Select Case number
Case 1
Dim a
Case 2
Dim a
End Select

Theoretically you can of course declare a variable twice, the problem is, asp will throw an error, if the variable is declared in the same scope.
whatever you want to achieve, keep in mind, you can (almost) always access the variables in parent scope, thus rendering a double declaration useless.
Dim number : number = 1
Dim a
Select Case number
Case 1:
a = "whatever"
Case 2:
a = "something different"
End Select
response.write a

Related

.Select return wrong length

I'm trying to check if the field data of my DataTable records is set to '0' or '1'. All my local record is saved into local_ds DataTable. Now, the record that I want check is this: 21a956af-f304-4c72-97cf-1ef08e8719fc
for a better vision I paste here the content of my table (that is also the content of my DataTable local_ds):
How you can see the record that I want check have the field data set to 0. Now I perform the research through this code:
Dim local_data = local_ds.Tables(0).Select(String.Format("GUID = '{0}'", "21a956af-f304-4c72-97cf-1ef08e8719fc"), String.Format("data", 1))
The code above use LINQ to take the result, anyway, I pass the GUID field to search and the field data as 1. This code should be return local_data.length equal to 0 but, instead, return 1 and this is wrong, 'cause I want to check only if the field data is 0 or 1. In this example the result should be local_data.length = 0 'cause in the LINQ query I specified clearly that I want find the record with GUID = x and data = 1.
I already know that this record exists in the database, the local_data variable must help me to recognize which type of valorization the data field have.
So, what I did wrong?
No, in the code above, you are not using LINQ.
DataTable.Select is a method available starting from the 1.1 version of NET Framework and exists in four possible overloads.
The one you are using is the one that takes, as first parameter, the WHERE condition and, as second parameter, the SORT order. So you are not really passing a condition WHERE .... AND Data = 1 but the Data=1 it is interpreted as a sort order of some kind.
The correct string for the WHERE parameter should be
Dim where as String = String.Format("GUID = '{0}' AND Data = {1}", _
"21a956af-f304-4c72-97cf-1ef08e8719fc", 1)
Dim local_data = local_ds.Tables(0).Select(where)

Defining an expression or code with lookup RS

I need to migrate a CR report to RS.
I have two Methods, that are basically two queries, one is called "Products" the other one is called "Volume".
Both these datasets have a field called ID.
The idea is to count ProductsID = VolumeID. Can I use a lookup as an expression inside RS that counts when lookup is not equal to Nothing? Or I have to do a code to do that?
EDIT:
Let me add more information.
Crystal Report is managing these in the Data Layer of the web app like this:
DataRow drVol = vols.Rows.Find(new Object[] { id, idEzd });
if (drVol != null)
{
if (drVol["Volume"] != System.DBNull.Value)
cRow.Volume = (isNormalReport ? Convert.ToDecimal(drVol["Volume"]) : Convert.ToDecimal(drVol["Volume"]));
else
cRow.Volume = 0;
dset.Compradores.Rows.Add(cRow);
}
So in this case I verified that RS is giving me an X amount of rows while CR is giving me another amount of rows.
The Idea is to duplicate this on an expression or VB code inside RS.
As you can see, we have to filter the drVol != null, RS is bringing all the rows.
I know a Lookup function can be used in this case as:
Lookup(Fields!id.Value & Fields!idEzd.Value, Fields!id.Value & Fields!idEzd.Value,Fields!Givemethisfield.Value,"Dataset")
But can I use this Lookup expression inside a code to count?
The counter should be something like this (Then I should add the lookup or an if that equals that vlookup, can the code see the two datasets?):
Dim count=0 as integer
Public Function Counter() as Integer
count = count + 1
return count
End Function
Public Function GetCounter () as integer
return count
End Function
Then create a Calculated Field for example "Counter" with the expression
=Code.Counter()
And then make the sum of that calculated field:
=Sum(Fields!Counter.Value)
But is not working, not even the counter, is giving me 0.
Sorry if I made a mistake in the Visual Basic code, I don't program in Visual.
EDIT2:
I saw some workaround on MSDN, something like:
Lookup(Fields!id.Value & Fields!idEzd.Value, Fields!id.Value & Fields!idEzd.Value,Fields!Givemethisfield.Value,"Dataset").Lenght
It makes sense as a lookup is an Array.
The problem is, either if I choose one scope or the other, I still have the problem that some fields can't be seen for example if the fields id and idezd are on Dataset1 and Givemethisfield is on Dataset2, it will either not see the id,idEzd or Givemethisfield.

VBA Variable Scope in a Case Statement

Wondering how variables work when used in a Case statement. It seems like they are declared in the first Case, regardless of whether that Case is relevant.
'The following code will throw an error
Select Case team
Case "Philadelphia Eagles"
dim record as String
Case "Dallas Cowboys"
dim record as String
End Select
Even if 'team' isn't Philadelphia Eagles, it claims I've already declared the variable 'record'
I was under the impression that anything in a case statement was completely skipped over if that case wasn't relevant.
'The following code works
Select Case team
Case "Philadelphia Eagles"
dim record as String
Case "Dallas Cowboys"
record = "8-8"
End Select
Just want to confirm that I am understanding the Case statement correctly here.
Thank you!
Josh
Variable declarations (Dim statements) are resolved at the start of the execution of the program, which is why your Dim record statements weren't "skipped" over in your example.
You should put your variable declarations once at the top of your code, just after you start the subroutine or function. You cannot use Dim on the same variable twice. If the variable is an array which you need to resize, you can use ReDim [Preserve] instead.
Sub Subname()
Dim record as String
Select Case team
Case "Philadelphia Eagles"
record = "16-0"
Case "Dallas Cowboys"
record = "8-8"
End Select
End Sub

vb.net Select Case error

When i want define values in Select Case i got error:
'Value' is not declared. It may be inaccessible due to its protection level.
When Dim Value As Object is outside Select Case - No error. My target is get Value different on special numbers. For example:
Select Case Integer
Case 1
Dim Value As New UserControl1
Case 2
Dim Value As New UserControl2
Case Else
Dim Value As New UserControl3
End Select
Try this, assuming all 3 user control types derive from the base UserControl object:
Dim Value as UserControl
Select Case Integer
Case 1
Value = New UserControl1
Case 2
Value = New UserControl2
Case Else
Value = New UserControl3
End Select
It is not passable to declare a variable inside a scope and access it outside the scope such as as select case statement. However you problem is easily solved by separating the declaration and initialization. This enables you to use the variable outside the select case as the variable is in the higher scope. The variable is declared as System.Windows.Controls.UserControl as this is the most specific common type.
Dim Value As UserControl
Select Case Integer
Case 1
Value = New UserControl1
Case 2
Value = New UserControl2
Case Else
Value = New UserControl3
End Select

Pass in Array to Select Case

this may be a dense question and I'm not finding it to be a dup of this one, but I need some help with understanding if an array can be used in a Select Case statement.
I have a sub routine that I will create a string array dynamically. The XML is also listed, but it could be any of the values listed below. It will be something like this:
Dim offensiveLine() As String = New String() {"center", "right wing", "left wing"}
Dim defensiveLine As String = "defense"
Dim playerInfo = <Player><Name>John</Name><Position val="right wing"/></Player>
What I want to do is see if this player is in one of the offensiveLine. So I say:
Dim playerPosition = playerInfo.Position.#val
Select Case playerPosition
Case offensiveLine
'do something
Case defensiveLine
'do something
Case Else
'do nothing
End Select
Here is lies the issue: Case offensiveLine is invalid. I know I could write out Case "center", "right wing", "left wing", but that would defeat the purpose of what I'm trying to do, which is to make a generalized variable that is an array that can be read from in a Case statement. Secondly, I know I can't create a variable like Dim offensiveLine = ""center", "right wing", "left wing"" and pass that in.
Any insight on how I may be able to pass in an array to a Case statement and have each one evaluated?
You may want to consider using an if clause here rather than a switch. Try this logic: if offensiveLine contains playerPosition, then offensive line, etc.
You actually can use a Select statement:
Dim playerPosition = playerInfo.Position.#val
Select Case True
Case offensiveLine.Contains(playerPosition)
'do something
Case defensiveLine.Contains(playerPosition)
'do something
Case Else
'do something - otherwise you don't need the 'Case Else'
End Select
The trick is the True in the first line of the Select.
The Select..Case construct does not work like that. However, it is easy to test if an element exists in an array:
If offensiveLine.Contains(playerPosition) Then
'Do something
ElseIf defensiveLine.Contains(playerPosition) Then
'Do something else
End If