I am trying to validate a text box.I have validated a couple of other text boxes and they work fine.This one has some error.
My code looks correct to me.Someone please point out my mistake and tell me why Visual Studio 2010 is prompting an error of invalid arguments and variable not in current context:
You need to define string errorMsg; in addTextBox_Validating function before you call ValidAddress.
You need to define the errorMsg variable before using it as an out parameter.
string errorMsg;
Read up on how to use out.
Although variables passed as an out arguments need not be initialized prior to being passed, the calling method is required to assign a value before the method returns.
class OutExample
{
static void Method(out int i)
{
i = 44;
}
static void Main()
{
int value;
Method(out value);
// value is now 44
}
}
You need to pass a string as second parameter to ValidAddress. Try and add
string errorMsg = null;
as first line of addTextBox_Validating()
You have not declared the errorMsg string.
private void addTextBox_Validating (object sender, CancelEventArgs e)
{
string errorMsg = "";
...etc
}
In ValidAddress, the errorMsg string is passed in to the function as a parameter, so this issue does not arise.
As far as I can see, errorMsg is not declared anywhere.
Try changing addTextBox_Validating by adding a declaration for it
e.g.
var errorMsg = string.Empty;
if (!ValidAddress(...
An out variable needs to be declared in the context that it is used.
hth
Alan.
Where is errorMsg defined? It looks like it's sent in as a parameter to ValidAddress, so addTextBox_Validating, being a different method entirely, doesn't have access to it, as errorMsg is scoped to only exist in ValidAddress. Long story short, you haven't initialised your variable.
Related
Okay so in short:
I declare a variable, say
string str = "Random";
then I try to perform any sort of operation whatsoever, say
str.ToLower();
And neither visual studio, nor intellisense recognise it at all.
VS gives me the name "str" does not exist in the current context. This happened right after I installed xamarin but I'm not sure if it's related.
Also this issue would not occur if I was inside a method, just when I'm directly inside a class.
This is my code:
public class Program {
public void randomMethod() {
string str2 = "Random";
str.ToUpper(); //this line shows no errors
}
string str = "Random";
str.ToLower(); //this line does show the error
}
str would be underlined red and the warning mentioned above would appear.
Does anybody know what's going on?
you even point the issue out yourself
you cannot do this
public class Program {
string str = "Random";
str.ToLower(); //this line does show the error
}
when would you expect that code to run?
You must put executable code inside a function. You point out that this works.
I cannot propose a fix since I do not know what you are trying to do.
It is a scope issue:
public class Program {
public void randomMethod() { //method scope starts
string str2 = "Random";
str.ToUpper(); //this line shows no errors
} //method scope ends
string str = "Random"; //this is a class field, but is missing an accessibility level
str.ToLower(); //this line SHOULD show an error, because you can't do this in a class
}
I have properly overwrite commit in InstallerSetup.cs I do not wish to write the user entered value to app.config but rather I want to pass the string Context.Parameters["TESTPARAMETER"]; to another class in form1.cs on load function. I tried string test = InstallerSetup.Context.Parameters["TESTPARAMETER"];
but getting InstallerSetup.Context is null. Please Help.
InstallerSetup.cs
public static string SQLSERVERNAME = "";
public static string HMSTENANTDB;
public static string SQLLOGIN;
public static string SQLPASSWORD;
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
try
{
SQLSERVERNAME = Context.Parameters["SQLSERVERNAME"];
HMSTENANTDB = Context.Parameters["HMSTENANTDB"];
SQLLOGIN = Context.Parameters["SQLLOGIN"];
SQLPASSWORD = Context.Parameters["SQLPASSWORD"];
}
catch (Exception e)
{
MessageBox.Show("Failed to update the application configuration file : " + e.Message);
base.Rollback(savedState);
}
}
from1.cs
InstallerSetup InsSetup = new InstallerSetup();
string Vsqlserver = InsSetup.Installers.Count.ToString();
string Vtenant = "";
if (InsSetup.Context != null)
{
Vtenant = InsSetup.Context.Parameters["HMSTENANTDB"];
}
else
{
Vtenant = "context is null";
}
As far as I can tell, the issue is that the property values are not being passed into the custom action. That would be the most obvious explanation. A commentfrom the poster says:
"passed those parameters to the custom action...................................... SQLSERVERNAME = Context.Parameters["SQLSERVERNAME"];
etc...
//................there is only these 4 lines in my custom actions"
which is essentially repeating the code that was previously posted.
This is NOT passing the values into the custom action. This is retrieving values which must already have been passed into the custom action.
Assuming that the custom action has been correctly added to (typically) the install nod of the custom action, and also assuming that the property names are in a TextBoxes dialog in the install, the values must be passed in to the custom action via the CustomActionData settings. To use one example, the CustomActionData setting must be something like:
/SQLSERVERNAME=[SQLSERVERNAME]
or /SQLSERVERNAME=[EDITA1] if EDIOTA1 is being used because that's the default property name.
However there is no reference to the TextBoxes (or any other) install dialog in the original question, so it's not really clear where the value of (say) SQLSERVERNAME is supposed to come from. It may be passed in on the msiexec command line, for example.
I'm using VS2015 on Windows 7.
Code analysis rule CA1804 (http://msdn.microsoft.com/library/ms182278.aspx) states that I am not using a variable and to remove it. However, I am using this variable further down in my code. This is happening across the whole solution in hundreds of places. The code block looks like this:
[WebMethod]
public bool ValidateUser(string userName, string password)
{
string soapResult = String.Empty;
try
{
// code here
using (StreamReader rd = new StreamReader(responseStream))
{
soapResult = rd.ReadToEnd();
}
// code here
bool isValidated = true;
}
catch (Exception e)
{
// throw error
}
return isValidated;
}
I'm getting this error from Code Analysis:
Error CA1804 'ValidateUser(string, string)' declares a variable, 'soapResult', of type 'string', which is never used or is only assigned to. Use this variable or remove it.
Is there something I'm missing here? It's not within an if/else like some of the instances I'm getting this error. But I figured that if it's being used at all this error would not be thrown.
Thanks for any help.
Read the analysis message carefully, note the bit I have highlighted:
Error CA1804 'ValidateUser(string, string)' declares a variable, 'soapResult', of type 'string', which is never used or is only assigned to. Use this variable or remove it.
It is telling you that you only assign a value to it (you actually do that twice including the initialisation to string.Empty) but you never use the value. So it's effectively a waste of a variable.
What you should be doing is either using the value, for example:
soapResult = rd.ReadToEnd();
if(soapResult.Contains("something")
{
isValidated = true;
}
else
{
//Not needed but added it to better show how this works in context
isValidated = false;
}
Or remove it altogether and discard the result you get from the StreamReader:
rd.ReadToEnd();
im not sure why this isnt working, Im just passing a blank string and a number and it should check if that number is in the string, if not then it should add it into the string.the variable 'thing' gets set but the global variable that it is supposed to update(strduplicates) is never updated.
I call the function like this
Trackstats(strduplicates,"1337");
private void TrackStats(string thing, string variable)
{
if (!(thing.Contains(variable)))
{
thing += variable + ",";
}
}
A better design might be to return the new value:
private string TrackStats(string thing, string variable)
{
if (!(thing.Contains(variable)))
{
thing += variable + ",";
}
return thing;
}
and call it using:
strduplicates = this.TrackStats(strduplicates, "1337");
You are passing thing by value. You need to pass it by reference to make changes visible outside the method:
private void TrackStats(ref string thing, string variable)
{
if (!(thing.Contains(variable)))
{
thing += variable + ",";
}
}
Strings in .NET are immutable. Every modification will allocate and return a new instance.
To alter your global variable, you have to pass it as ref parameter, like this:
private void TrackStats(ref string thing, string variable)
{
if (!(thing.Contains(variable)))
{
thing += variable + ",";
}
}
Then you can call it like this:
TrackStats(ref strduplicates, "42");
string thing is not an output param. Use 'out string thing' to signal for output.
I am using dynamic method calls to access a method in a dynamically loaded dll.
I am doing:
dynamic classInstance = Activator.CreateInstance(cmd.Type);
classInstance.AddString(); //This line works
classInstance.AddString(cmd); //this line does not work
The methods in the dll are:
public CustomCommandTest1()
{
}
public void AddString(Command cmd, ExposedVariables exv)
{
exv.ChopResults += "Add string Command";
}
public void AddString(ExposedVariables exv)
{
exv.ChopResults += "Add string Command";
}
public void AddString()
{
string ChopResults = "Add string Command";
}
I can access (call) all the methods that do not have parameters but those that do give me a RuntimeBindingInternalCompilerException. There is no usable info in the exception to try to figure this out.
I have done this before and it has worked. I don't know what I am doing differently here.
Further discovery here reveals that it is related to the complex variable types. Simple builtin variable types works. There is no difference in the definition of the complex variables however as I refer to the definition in a common file.
AddString(cmd) could only work if cmd is actually an instance of ExposedVariables. There's no overload of just
public void AddString(Command cmd)
which is what it looks like you're expecting.
This has nothing to do with complex variable types - it has everything to do with you trying to call a method which doesn't exist. Which overload did you expect to be called, out of the ones you presented to us?
If the cmd variable in your example is a reference to a Command instance rather than an ExposedVariablesinstance, then the call is wrong. You do not have an AddString overload that takes a Command only.
try
ExposedVariables exv = new ExposedVariables();
classInstance.AddString(cmd, exv);
as you don't seem to have an overload that takes just cmd.