Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cache varybycustom

Status
Not open for further replies.

Badgers

Programmer
Joined
Nov 20, 2001
Messages
187
Location
US
Hi,

i have the following code

Overrides Function GetVaryByCustomString(ByVal context As HttpContext, ByVal arg As String) As String

Dim objUtility As New OMG.Common.Utility

Try

Select Case arg.ToLower
Case "userid"
Dim iUserID As Nullable(Of Integer) = objUtility.GetUserIDFromPageContext

If iUserID.HasValue = True Then
Return iUserID.Value
End If

Case "test"
Return "jj"

End Select


Return MyBase.GetVaryByCustomString(context, arg)

Finally
objUtility = Nothing


End Try


The problem is my args are userid;test, so how do you set mutiple custom settings within this function?
 
parse the string and use if statements instead of case statements. C# would look like this. it's similar for VB
Code:
string toReturn = string.empty;

List<string> args = new List<string>();
args.AddRange(arg.ToLower().split(new char[] { ';' }));

if(args.Contains("userid"))
{
  OMG.Common.Utility objUtility = new OMG.Common.Utility();
  if (objUtility.GetUserIDFromPageContext.HasValue)
  {
      toReturn += objUtility.GetUserIDFromPageContext.Value;
  }
}
if (args.Contains("test"))
{
   toReturn += "jj";
}

if (string.IsNullOrEmpty(toReturn))
{
   toReturn = MyBase.GetVaryByCustomString(context, arg);
}

return toReturn;

you shouldn't need a try/catch block in this function since you're only working with simple data types.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for that it makes good sense.

When you add the toReturn together, should you add a delimetre ';' between each value, or dosn't it matter.

For eg; values 1 and 2 = 12 or 1;2 ?

Also, you mentioned about the try block, I thought it was practise to use this when creating objects thus to use the finally to destoy them. All opionions are good just interested.

Thanks

Tim.
 
if you want to add the semicolon to the return value you can. I don't see how it would help though, as I don't think you have aceess to the return value once is returned. it just used by the system internally to determine what/when to cache.

try/catch is considered an expensive action, so I try to use it only when necessary. DB connections, opening files, large blocks of conditional objects.

if you want to make sure your objects are destroyed use the using keyword. it works with any object that can be disposed.
Code:
c#
using (SqlConnection cnn = new SqlConnection())
{
...
}
//is the same as
SqlConnection cnn = new SqlConnection();
...
cnn.Dispose();

VB
using SqlConnection cnn = new SqlConnection()
...
end using
//is the same as
Dim cnn as SqlConnection = new SqlConnection()
...
cnn.Dispose()
I let grabage collection take care of setting objects to null/nothing.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top