Define >2 parameters & pass in values from one method to another
How do i actually pass in a value from a method to another method? I apologies for my lack in knowledge in c#. What i have done so far cannot work. I wish to pass the value 'MaxHeight' from Page() to 'MaxHeight' from fullNameControlLoaded().
Page.xaml.cs:
public Page(string _setArticles, strin开发者_开发问答g _setLength)
{
InitializeComponent();
//testing!
//send value to method 'fullNameControl_Loaded' (summary length of each ListBox item)
int MaxHeight = 0;
if (!string.IsNullOrEmpty(_setLength))
{
if (_setLength.Contains("_3"))
MaxHeight = 30;
fullNameControl_Loaded(null, null, MaxHeight);
}
}
private TextBlock m_textBlock;
void fullNameControl_Loaded(object sender, RoutedEventArgs e, int MaxHeight)
{
m_textBlock = sender as TextBlock;
m_textBlock.MaxHeight = MaxHeight;
}
You haven't made it clear what's not working, but this:
if (_setLength.Contains("_3"))
MaxHeight = 30;
fullNameControl_Loaded(null, null, MaxHeight);
looks like it should probably be this:
if (_setLength.Contains("_3"))
{
MaxHeight = 30;
fullNameControl_Loaded(null, null, MaxHeight);
}
However, at that point sender
will be null, so fullNameControl_Loaded()
will throw a NullReferenceException
.
It seems unlikely that you really want to change the value of m_textBlock
in the method... where were you expecting this to be initialized?
That's not a way how its done, declare your MaxHeight
field in class scope then you can access to it from anywhere inside the class. don't modify generated event.
You can set the "MaxHeight" as a property in your class then Page constructor can set it's value and then when the fullNameControl_Loaded function runs, the value of the property will be the updated value from the page constructor.
private int maxHeight = 0; public Page(string _setArticles, string _setLength) { InitializeComponent();
//testing!!!!
//send value to method 'fullNameControl_Loaded'
//(summary length of each ListBox item)
maxHeight = 0;
if (!string.IsNullOrEmpty(_setLength))
{
if (_setLength.Contains("_3"))
maxHeight = 30;
fullNameControl_Loaded(m_textBlock, null, MaxHeight);
}
}
private TextBlock m_textBlock;
void fullNameControl_Loaded(object sender, RoutedEventArgs e, int MaxHeight)
{
m_textBlock = sender as TextBlock;
m_textBlock.MaxHeight = maxHeight;
}
精彩评论