Showing posts with label variables. Show all posts
Showing posts with label variables. Show all posts

Wednesday, March 28, 2012

string.empty

Hi,

When initializing newly declared variables is it any different to say

string myString = ""

versus

string myString = string.Empty;

If there is an advantage/disadvantage to one way over the other, what might that be?

Thanks

They're programatically identical. I think string.empty may be a few nanoseconds faster. You could always write a benchmarker and check how long it takes to assign 100,000 strings to "" as opposed to string.emptyand tell us about it :)


FXCOP advises that String.Empty should be used in preference to assigning an empty string.


This would summarize that:

http://codebetter.com/blogs/brendan.tompkins/archive/2003/10/14/2585.aspx

http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx


We've already spent more time discussing this than the entire world would save if it every application was instantly converted from "" to string.Empty. Wink


trying to understand the rules I read so far, a string doesn't actually get assigned a new value, but instead is destroyed and then a new one created with the new value.

So, from the links, it would seem that the string = "" would be destroyed when it was set to a new value, and since the string.Empty is never created as an object, there would be no destruction of it, prior to (re)assignment.

Anyway, sounds like it is trivial, so I won't worry about it.

Thursday, March 22, 2012

Structures in Session variables

Is it possible to store structures in Session variables?

I want to store some basic user details in a Session variable (first name, last name, join date, etc) and thought it would be easier to put them into a structure rather than holding them in separate variables.

Is this possible?

How do I do it?Hi,
You can put in Session anything that inherits from System.Object. In .NET it is virtually everithing (except interfaces and delegates) including structures. Having structure str you can do it like this:


Session("MyStruct") = str

Thanks.

If I subsequently want to delete the structure from the Session, can I just set Session("MyStruct") = null ?

Will this release all the individual components of the structure in one go?
Yes

Structures vs Variables - Or can I even do this?

Hello all-
I need to pass around the details of a db record a few times and was wondering about using structures. If I create a structure something like the one listed below in one of my XXX.vb files, can I use it like a variable?

**USERDB.VB File**
Public Structure UserDetails
puplic FirstName as string
public LastName as string
End Structure

Public Function GetUserDetails(byval UID as Integer) as UserDetails
**Database crap here**
GetUserDetails.FirstName = objDR("FirstName").tostring
GetUserDetails.LastName = objDR("LastName").tostring
End Function

**WEBFORM1.ASPX.VB**
Private Sub ShowUser(byval UID as integer)
Dim objUserDetails as UserDetails
objUserDetails = GetUserDetails(UID)
lblFirstName.text = objUserDetails.FirstName
lblLastName.text = objUserDetails.LastName
End Sub

Sorry so long. Do I need to destroy the objects when done with them?

Thanks,
DougMaybe use a class instead? Any help would be great!