It might be easy but im stuck!
i do have a class that Create one Command for me like:
As you can see this Encode will Return me bCommand!
Now im having a Button Function that i should call this bCommand and Assign it as a Value like:
So i got the Error that Bcommand Does not Exist in Current Context
I'm looking for any advice how i can Solve the Problem.
You need to assign the results of the Encode method to a variable.
private void btnModel_Click(object sender, EventArgs e)
{
byte[] bCommand = Encode("C11", "");
WriteData(bCommand); // bCommand will now exist in this context
}
Related
I have two stupid questions :
The first one :
I have already asked this and I'm truly sorry to ask it again (even more so because there are a lot of posts about it), but now I really don't understand why that happens even if I read every other post, here is my code :
public class PageTitre {
...
public void situation(string s) {
onglet.get_Range("C11").Value = "(" + s + ")";
}
}
public class PPE_Process {
public static PageTitre pageTitre;
public static void MainProcess() {
...
pageTitre = new PageTitre();
...
}
}
public partial class PPE_Ribbon {
private void SituationEditBox_TextChanged(object sender, RibbonControlEventArgs e)
{
PPE_Process.pageTitre.situation(SituationEditBox.Text);
}
}
I have tried some other things, like putting situation as a variable of PageTitre and having a get and a set, and
private void SituationEditBox_TextChanged(object sender, RibbonControlEventArgs e)
{
PPE_Process.pageTitre.set_situation(SituationEditBox.Text);
}
Or
private void SituationEditBox_TextChanged(object sender, RibbonControlEventArgs e)
{
PPE_Process.pageTitre.situation = SituationEditBox.Text;
}
But nothing worked, with the same error: cannot explicitly call operator or accessor.
I guess there is a problem with the static? If that is the case, here is my second question:
Second one :
I read a lot of documentation about it but I really can't understand what is the use of "static"... Is it just so that we can't change the value outside of the class, or something like this? Then, would it really change something if I take off every static there is in my code?
Again, I am sorry that you have to answer this question again and again, but I understand a lot better if that is directly related to my code, and not someone answering someone else about some other random code, which has a different problem than mine. :/
Edit to add more information:
There shouldn't be any problem with .Value or .get_Range, as it works on other parts of the code, but
For .Value, here is the information given by Visual Studio:
void Range.set_Value([object RangeValueDataType = System.Type.Missing], [object value = System.Type.Missing])
For .get_Range: Excel.Range_Worksheet.get_Range(object Cell1, [object Cell2 = System.Type.Missing])
For static, I still don't really understand all these "instances" things, but I will try to look more, and add another question in this forum if I still don't understand after this.
To your first question: I don't see any problem with your call of the method situation, maybe the problem is the statement inside the method (get_Range("C11"))?
To your second question: When you make a variable/property/method static it means that it is independent from any instance (object) of the class. Otherwise you couldn't access the property PPE_Process.pageTitre without an instance of PPE_Process.
Alrighty, I changed nothing in my code but now it works with
private void SituationEditBox_TextChanged(object sender, RibbonControlEventArgs e)
{
PPE_Process.pageTitre.situation = SituationEditBox.Text;
}
So... I don't know, it was maybe a problem with the build of my solution, but that's pretty fortunate :)
I do have a CodeFunction object from EnvDTE namespace. I do want to get the definition of it; e.g.:
private void MenuItemCallback(object sender, EventArgs e)
{
MessageBox.Show("Try");
}
I would like to get first line as string.
What I have tried until now,
1 ) Try to make a string by getting CodeFunction's Type (return type) and Parameters then in a loop add them to a string. However, parameter types' names become like "System.UInt32" etc. which I don't want to. Also a problem with this, it may not take ref Guid pguidCmdGroup as fully. I am afraid of skipping ref at this.
2 ) I tried to use functions of CodeFunction but all I could get was simple name of it.
3 ) I tried to write from starting point and ending point of the CodeFunction but couldn't find a way to turn two TextPoint to string and as I realized ending point is not the ending of the definition but the function it self which I don't want to.
How can I get just simply private void MenuItemCallback(object sender, EventArgs e) or MenuItemCallback(object sender, EventArgs e)?
Thanks for all your help.
You must use GetStartPoint() and GetEndPoint() :
Read the full source of the function and then cut off the
code before the first open curly brace.
// Retrieve the source code for the function.
TextPoint start = codeFunction.GetStartPoint(vsCMPart.vsCMPartHeader);
TextPoint finish = codeFunction.GetEndPoint();
string fullSource = start.CreateEditPoint().GetText(finish);
// Find the first open curly brace
int openCurlyBracePos = fullSource.IndexOf('{');
string declaration = String.Empty;
if (openCurlyBracePos > -1) {
declaration = fullSource.Substring(0, openCurlyBracePos).Trim();
}
I have public variables:
public static List<CompanyModel1> companies1 = new List<CompanyModel1>();
public List<URL> urls = new List<URL>();
I add more values to this variable during a function call
public void DataBind_Results()
{
..companies1.AddRange(listToAdd.OrderByDescending(c => c.compnMastery1));
}
urls.Add(new URL
{
Link = listToAdd.First().compnId1.ToString(),
Title = arrProcess[ar]
});
But my code always run twice (I see that when I use debug), I think the reason is it always have page reload.
Then my variable will be duplicate its values, and when I the data display, I see it is duplicated. How to avoid it ?
I'm going to assume you're using asp.net or something. Wrap the "add" function in an
if(!Page.IsPostBack) {
//Your add code
}
Ideally, find out why your page is loading twice then fix that.
Then add code to check whether the lists are populated at the start of each data bind before they are re-populated.
You want the variable to populate OnLoad so do something like this:
Declare your variables:
public static List<CompanyModel1> companies1;
public List<URL> urls;
The populate them when the page loads. But not if you're doing a postback.
protected override void OnLoad(EventArgs e)
{
if(!IsPostBack)
{
companies1 = new List<CompanyModel1>();
urls = new List<URL>();
}
}
Your code is running twice that means, once when you call that function and again when the page is posted back.
Put your code in
if(!IsPostBack)
{
here.....
}
I have something to do under a button click (add values to listbox) only if a particular string changes from its previous value. How do I manage this? Below is a sample of my code:
private void button6_Click(object sender, EventArgs e)
{
string x = //some varying value I get from other parts of my program
listBox1.Items.Clear();
listBox1.Items.Add(x + /*other things*/);
}
I can at times have same value for string x from previous value when clicking button6. In such cases I don't want listBox1 to add the item (string x). How to add to listbox only when value of string changes? There's no way to predetermine string x. It gets value when program is running.
Note: adding values to listBox1 every single time and later deleting the duplicates wont work in my program.
Have you considered keeping a copy of the old string value around in a private field, and simply comparing the new value to the old value to see if they match?
For example:
// holds a copy of the previous value for comparison purposes
private string oldString = string.Empty;
private void button6_Click(object sender, EventArgs e)
{
// Get the new string value
string newString = //some varying value I get from other parts of my program
// Compare the old string to the new one
if (oldString != newString)
{
// The string values are different, so update the ListBox
listBox1.Items.Clear();
listBox1.Items.Add(x + /*other things*/);
}
// Save the new value back into the temporary variable
oldString = newString;
}
Edit: As the other answers suggest, there are certainly other, more complicated solutions, like encapsulating all access to the string value in a property, or wrapping the string in a custom class. Some of these alternatives have the potential to be "cleaner", more object-oriented approaches. But they're all more complicated than simply saving the previous value in a field. It's up to you to decide whether your specific use case merits the complicated solution, or a simpler one. Think about long-term maintainability, not what's easier for you to implement right now.
string last = string.Empty;
private void button6_Click(object sender, EventArgs e)
{
string x = //some varying value I get from other parts of my program
if(x!=last)
{
listBox1.Items.Clear();
listBox1.Items.Add(x + /*other things*/);
last = x;
}
}
If this string is super important and gets passed around alot, maybe you should wrap it in a class. The class can hold the string value as a property, but also keep track of when it has changed.
public class StringValue
{
private bool _changed;
public string StrValue{get; set{ _changed = true;}
public bool Changed{get;set;}
}
this is rudimentery of course
I'm not sure I understand completely, but it sounds like you should be using a property to set String x;
string _x = string.Empty;
public string X
{
set
{
if(value != this._x)
{
DoFancyListBoxWork();
this._x = value;
}
}
get
{
return this._x;
}
}
If this is web application, store your last value into session variable. If this is windows application, store it at a class level variable or in singleton class and use this last value for comparison with new value.
On the page load add the current value to viewstate and at the button click check the current value is equal to the value in the view state. If both are equal we can say that the value is not changed.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["CurrentValue"] = Your Value;
}
}
protected void btnSubmit_click(object sender, EventArgs e)
{
if (NewValue== ViewState["CurrentValue"].ToString())
{
lblmsg.Text = "value is not changed..";
return;
}
else
lblmsg.Text = "value is changed..";
}
You can check the detailed article in this link.
Check Control Value is changed or not
First, I'd like to ask you to check most of the other answers. They are more complete, in that they treat more global issues of tracking the changes of a variable.
Now, I'm assuming, from reading the snippet of code you provided, that you need to track if a string was changed by the user. So, in other words, you probably have a TextBox or other kind of control through which the user can change that value. This is where you should focus your attention: just consume the TextChanged event.
If, however, I'm mistaken and your string comes from any other kind of external source, either use the wrapper class suggested by #Ryan Bennett or, if you are using .Net 4, use a dynamic container, which raises a PropertyChanged event whenever any property is changed.
Wierd behaviour when passing values to and from second form.
ParameterForm pf = new ParameterForm(testString);
works
ParameterForm pf = new ParameterForm();
pf.testString="test";
doesn't (testString defined as public string)
maybe i'm missing something? Anyway I'd like to make 2nd variant work properly, as for now - it returns null object reference error.
Thanks for help.
Posting more code here:
calling
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(doc.GetElementById(ParametersButton.Tag.ToString()));
pf.ShowDialog(this);
pf.test = "test";
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
};
definition and use
public partial class ParameterForm : Form
{
public string test;
public XmlElement node;
public delegate void ParameterSubmitResult(object sender, XmlElement e);
public event ParameterSubmitResult Submit;
public void SubmitButton_Click(object sender, EventArgs e)
{
Submit(this,this.node);
Debug.WriteLine(test);
}
}
result:
Submit - null object reference
test - null object reference
pf.ShowDialog(this); is a blocking call, so pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit); is never reached: switch the order.
Submit(this,this.node); throws a null object reference because no event is assigned to it (see above). Generally, you should always check first: if (Submit != null) Submit(this,this.node);
You should change ``pf.ShowDialog(this);topf.Show(this);` so that your main form isn't disabled while your dialog box is open, if that's what you want, or use the model below (typical for dialog boxes.)
I'm not sure what pf_Submit is supposed to do, so this might not be the best way to go about it in your application, but it's how general "Proceed? Yes/No" questions work.
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(testString);
pf.ShowDialog(this); // Blocks until user submits
// Do whatever pf_Submit did here.
};
public partial class ParameterForm : Form
{
public string test; // Generally, encapsulate these
public XmlElement node; // in properties
public void SubmitButton_Click(object sender, EventArgs e)
{
Debug.WriteLine(test);
this.Close(); // Returns from ShowDialog()
}
}
When you want to use your second variant, you have to use a getString()-Method, where you can put the e.g. "testString". The way you wrote it, "testString" should be a method (and got brackets).
EDIT (a bit more precise):
You could write:
pf.getString(testString);
, if "pf" is an instance of your own class, otherwise you had to look up, whether you can retrieve a String in this class.
the thing was in line order :)
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
and
pf.Test = "test";
should have been set before
pf.ShowDialog(this);
my mistake thingking that parameter can be passed after 2nd form was displayed
thnx for answers