I want to make this string public. i.e., available from other functions. How can I do that?
protected override void OnNavigatedTo(NavigationEventArgs e)
{
String parameter = NavigationContext.QueryString["parameter"];
}
Declare it outside of the function (global scope).
Something like:
String parameter="";
protected override void OnNavigatedTo(NavigationEventArgs e)
{
parameter = NavigationContext.QueryString["parameter"];
}
Now you can use the string parameter any where in that file.
public String parameter = new String();
protected override void OnNavigatedTo(NavigationEventArgs e){
String parameter = NavigationContext.QueryString["parameter"];
}
You just need to change the scope of your variable to global :)
My suggestion would be that you move it outside the function, but make it private, then expose it via a public property; for example:
private string _parameter;
public string Parameter
{
get { return _parameter;}
set
{
_parameter = value;
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
_parameter = NavigationContext.QueryString["parameter"];
}
If you only need to access it from within the current class, but outside of the function then you can leave out the property, or even make it read-only. Alternatively, you could use the implicit property definition:
public string Parameter { get; set; }
And with C# 6 you will be able to use initializers with this.
Related
Am passing a parameter as a way to allow a user to go back and make changes
private void go_back_btn_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(TruckRegistrationPage), this.truckdetails);
}
Now on the trruck registration page
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
this.changeddetails= (TruckRegistrationDetails)e.Parameter;
//set the form fields based on the details
if (e.Parameter) //this throws an error of boolean not casted
{
truck_reg_no.Text = changeddetails.reg_no;
transporter_name.Text = truckdetails.owner_id;
.......assign other xaml controls
}
}
The parameters am passing are of type TruckRegistrationDetails whic is a class containing properties as below
class TruckRegistrationDetails
{
public int id { get; set; }
public string reg_no { get; set; }
public int truck_category { get; set; }
.......others
}
How do i check to see if any parameters have been passed and hence assign the xaml controls value
Change your code to this
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
changeddetails = (TruckRegistrationDetails)e.Parameter;
if (changeddetails != null)
{
truck_reg_no.Text = changeddetails.reg_no;
//do what ever you want ^^
}
}
Your check was for a boolean, but e.Parameter is an object. Here is the link to MSDN https://msdn.microsoft.com/de-de/library/windows/apps/windows.ui.xaml.navigation.navigationeventargs.parameter.aspx
I was getting an error using this approach as at some point OnNavigatedTo is called with a NavigationEventArgs.Parameter of an empty string. This was causing a Cast exception when the object is cast to the object type I passed.
I used:
if (args.Parameter is DeviceInformation)
{
DeviceInformation deviceInfo = (DeviceInformation)args.Parameter;
//Do something with object
}
This checks the type of object first to see if it matches the expected first, then the cast will not throw an exception.
I have a class that has multiple properties. What I want to have is a value set to a particular property if there is no value passed to it. For instance, if txtFather.Text is blank, then the _theFather property of a class should have a default value of "N/A". Here is the code I have so far:
class StudentModel
private string _theFather = "N/A";
public string SetFather
{
get { return _theFather; }
set { _theFather = value; }
}
public void InsertStudent(StudentModel student)
{
MessageBox.Show(_theFather);
}
class AddStudent
private void button1_Click(object sender, EventArgs e)
{
var sm = new StudentModel()
{
SetFather = txtFathersName.Text
};
sm.InsertStudent(sm);
}
If I place a value in the txtFather.Text, I get the its value in the StudentModel Class but if I leave the txtFather.Text blank, I don't get the "N/A" value. I just get a blank or no value at all.
Thanks for your help.
I would encapsulate the logic within the StudentModel class by checking the value being passed to the setter, and only updating your backing field if the string is not null or whitespace.
public string SetFather
{
get { return _theFather; }
set {
if(!string.IsNullOrWhiteSpace(value))
{
_theFather = value;
}
}
}
This way you only have the logic in a single place. If you modify your class' consuming code, then you have to remember to change it everywhere.
You should set the property only if a string is typed, like this:
private void button1_Click(object sender, EventArgs e)
{
var sm = new StudentModel();
if (!string.IsNullOrEmpty(txtFathersName.Text))
sm.SetFather = txtFathersName.Text;
sm.InsertStudent(sm);
}
I have a class called nyoba, i tried to enter value of textBox1.Text to eek.konsentrasi.
And I don't have any idea to call value of eek.konsentrasi from another class. Anybody knows? please help me.
public class nyoba
{
private string Konsentrasi;
public string konsentrasi
{
get
{
return Konsentrasi;
}
set
{
Konsentrasi = value;
}
}
public void njajal(string hehe)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
nyoba eek = new nyoba();
eek.konsentrasi = textBox1.Text;
}
public class caller
{
//how to get eek.konsentrasi variable?
}
As first, your class names should always be pascal case (first letter uppercase). Also your public property should be pascal case.
Then your Nyoba class and its property Konsentrasi are not static, means you have to initiate the class as object before you can access it's non static property.
Nyoba n = new Nyoba();
string s = n.Konsentrasi;
To access the same instance you should not create the instance inside of the button click event. Place your Nyoba instance somewhere you can access to in the form and in the Caller class.
I've a file SiteMinder.CS in App_code where I set the UserID who has accessed the webpage
public class SiteMinder : IHttpModule
{
public string UserID { get; set; }
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(Application_PreRequestHandler);
}
private void Application_PreRequestHandler(Object source, EventArgs e)
{
if (HttpContext.Current.Request.Headers != null)
{
NameValueCollection coll = HttpContext.Current.Request.Headers;
UserID = coll["uid"]; // Doesn't have NULL value
}
}
}
In another webpage UserDetails.aspx.cs file I'm trying to access this UserID but it is having NULL value.
public partial class UserDetails : System.Web.UI.Page
{
protected string SessionUser { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
SiteMinder objSite = new SiteMinder();
SessionUser = objSite.UserID;//Returns NULL
}
}
All these are under same namespace. Please let me know where I'm wrong here.
You're creating a new SiteMinder object. That's not the same object that had the property set on it, so the property will have the default value (null).
You need to obtain a reference to the original SiteMinder object which set the property - or store the value somewhere else (such as the HttpContext).
I have a class called Global.cs:
public class Global
{
private string id= string.Empty;
public string Id
{
get { return id;}
set { id= value; }
}
}
Now in the Main class,
public class Main
{
public Global objGlobal;
protected void Page_Load(object sender, EventArgs e)
{
objGlobal= new Global();
objGlobal.id="XX001";
}
public void Setdata()
{
// Trying to access objGlobal.id value here but it's null
}
}
What am I missing?
Shouldn't you always be getting/setting "Id" rather than "id". As "id" is private.
Well, your XX class instance is more than one time.
If you need to persist some user-retalive info, try storing it into the SessionState.
If you need to just have the static class with some static data, add the static keyword to both class and its members.