Foros del Web » Programación para mayores de 30 ;) » .NET »

Asignacion de Sessions

Estas en el tema de Asignacion de Sessions en el foro de .NET en Foros del Web. Hola gente, estoy haciendo un programilla en que quiero que me haga un random de dos Sessions. @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código C#: Ver original protected void ...
  #1 (permalink)  
Antiguo 07/02/2012, 15:21
 
Fecha de Ingreso: septiembre-2011
Mensajes: 87
Antigüedad: 12 años, 6 meses
Puntos: 6
Asignacion de Sessions

Hola gente, estoy haciendo un programilla en que quiero que me haga un random de dos Sessions.

Código C#:
Ver original
  1. protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             Session["min"] = 5;
  4.             Session["max"] = 15;
  5.  
  6.             txt_min.Text = Session["min"].ToString();
  7.             txt_max.Text = Session["max"].ToString();
  8.  
  9.             Random rand = new Random();
  10.             int num1 = rand.Next(int.Parse(Session["max"]), int.Parse(Session["max"]));
  11.  
  12.         }

Nose si se pueden asignar directamente en el metodo Next la Session.

gracias
  #2 (permalink)  
Antiguo 19/10/2012, 12:02
 
Fecha de Ingreso: julio-2010
Ubicación: santiago
Mensajes: 21
Antigüedad: 13 años, 9 meses
Puntos: 1
Respuesta: Asignacion de Sessions

primero: no manejes los valores de sesiones directamente, encapsulala en una propiedad y luego usas la propiedad. Así (fijate que los nombres de la sessiones empiezan con minuscula y las propiedades con mayuscula):
private int MinValue
{
get
{
if (Session["minValue"] != null)
return (int)Session["minValue"];
else
throw new AggregateException("this value is not loaded.");
}
set
{
Session["minValue"] = value;
}
}
private int MaxValue
{
get
{
if (Session["maxValue"] != null)
return (int)Session["maxValue"];
else
throw new AggregateException("this value is not loaded.");
}
set
{
Session["maxValue"] = value;
}
}

y el contenido del handler Page_Load te queda así:

MinValue = 5;
MaxValue = 15;

txt_min.Text = MinValue.ToString();
txt_max.Text = MaxValue.ToString();

Random rand = new Random();
int num1 = rand.Next(MinValue, MaxValue);

Saludos.

Etiquetas: c#
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 23:50.