How to Pass Textbox Value to Method or Function - c#

I have a class CURPRFC that contains the following:
static public string CalcRFC(string firstname, string lastname, string middlename, string date)
I'm calling this method/function in my codebehind as follows:
CURPRFC.CalcRFC(txtfirstname.text, txtlastname.text, txtmidlename.text, txtdate.text)
where each of the values are textbox values. However, I receive an error stating:
System.Web.UI.WebControls.TextBox' does not contain a definition for 'text' and no extension method 'text' accepting a first argument of type 'System.Web.UI.WebControls.TextBox' could be found (are you missing a using directive or an assembly reference?)
My C# is a bit weak. I think I need to change my textbox text values to strings, but am not sure of how to go about this. Am I on the right track?
Your help is most appreciated!
Thanks,
Sid

You need to remember that C# is case-sensitive. TextBox doesn't have a text property, but it does have a Text property:
CURPRFC.CalcRFC(txtfirstname.Text, txtlastname.Text,
txtmidlename.Text, txtdate.Text);
(I'd also suggest that you think about naming clarity - conventionally C# variables use camelCasing to indicate word breaks, and a class named "CURPRFC" is far from self-explanatory.)

Related

C# how to get value from specific cell datagridview

i am working on C# and trying to save my datagridview to xml.
my code as follow :
writer.WriteStartElement("NamaBarang");
writer.WriteString(dgvCart.Rows[i].Cells[1].Value.ToString);
writer.WriteEndElement();
and the error occur at
writer.WriteString(dgvCart.Rows[i].Cells[1].Value.ToString);
Error 2 The best overloaded method match for
'System.Xml.XmlWriter.WriteString(string)' has some invalid
arguments C:\Users\Eric\Desktop\C#\Gridview\Gridview\Form1.cs 59 25 Gridview
and
Error 3 Argument 1: cannot convert from 'method group' to
'string' C:\Users\Eric\Desktop\C#\Gridview\Gridview\Form1.cs 59 44 Gridview
i am previously a vb user so i am having a hard time in c#
please help.
Youve forgotten to put empty brackets () after your ToString
VB isn't bothered about this but c# is- every time you want to call a method that has no arguments, you have to put empty brackets
//this is fine in VB
Dim x as String = y.ToString
//so is this
Dim x as String = y.ToString()
//in c# we insist on brackets when calling a method
string x = y.ToString();
//no brackets means "get the value of the property"
int I = mystring.Length;
Seeing something that looks like a method name, without brackets, usually means it's a property rather than a method. It can occasionally mean that the method itself is being used as an argument - a way of passing a method around as a variable, but that's beyond the scope of what we're discussing here.. Read up on delegates if you're interested.
In short, if you want to call a method in c#, it absolutely must have brackets after the name, whether there are arguments or not
Also handy to remember that when you see the error about "is a method group" it probably means you've tried to call a method but omitted the brackets.
C# is a struggle for VB people because it's essentially way more demanding that the syntax be absolutely right- you'll get used to it though, and it does help eventually!

IsNullEmptyOrWhiteSpace Yielding True when Text is ">="

I have a if statement that is doing validation for textbox on a WinForms application.
if (txtRule.Text.IsNullEmptyOrWhiteSpace())
{
result = false;
//error provider code
}
I know for a fact that the text in this textbox is the string ">=" because I'm using a breakpoint to figure out what the current text is in the textbox.
Obviously the text cannot be null since there is something in the textbox, and the same can be said about it not being empty. This means that is must be the case that the special characters ">=" are considered white space for some reason?
I would like to know the reason this if statement yields true when everything seems to be pointing towards a false value.
Considering that there is no instance method with that name in the System.String class (the most similar by name is String.IsNullOrWhiteSpace that is static, while you are using your method on a txtRule.Text that is a System.String), then probably that is an extension method written by someone where you work. Try doing a Go To Definition and check.

Find string with many names which are not know at the same time in a string

i have the following selfmade definition in on of our customer files:
<bitmaskdef name=SDC_STATUS_BITMASKM_1>
..content
</bitmaskdef>
I had to read out the content between the open and closing tags of the bitmask def.
To achive that, i've used regex in my c# code as following:
var bitmaskDefinitionRegex = new Regex(string.Format(#"<bitmaskdef name={0}>(.*?)</bitmaskdef>", bitmaskName));
bitmaskName contains the name of the bitmask we are searching for, for example: SDC_STATUS_BITMASKM_1
Now our customer wants to change the definition as following:
<bitmaskdef name=SDC_STATUS_BITMASKM_1; SDC_STATUS_BITMASKM_2; SDC_STATUS_BITMASKM_3; SDC_STATUS_BITMASKM_4>
...content
</bitmaskdef>
So that he can set more than one name for the definition. With the current solution it is not able to get the content between the definition. Thats why i thought to change my RegEx but it currently don't know how. Important to know is, that i always only know one of the names.
For example:
Reading out the name SDC_STATUS_BITMASKM_1
Find the definition for the name SDC_STATUS_BITMASKM_1
Reading out the name SDC_STATUS_BITMASKM_2
Find the definition for the name SDC_STATUS_BITMASKM_2
Reading out the name SDC_STATUS_BITMASKM_X
and so on
So far my current solution is not working because i only consider one name, but now i need a solution to find the definition with the content also if there are many other names defined.
Update #1
Here is the way im looking for the regex match. The field "fc" is my file content of the "textfile" which contains the xml like definition.
var bitmaskDefinitionMatch = bitmaskDefinitionRegex.Match(String.Join(String.Empty, fc.ToArray()));
You could use
new Regex(string.Format(#"<bitmaskdef name=[^>]*?{0}(?:>|;[^>]*>)(.*?)</bitmaskdef>", bitmaskName));
It will match any bitmaskdef tag whose name attribute includes bitmaskName, demo here: http://regex101.com/r/lR0kG6

Cannot implicitly convert type 'RazorEngine.Text.RawString' to 'string'

Instead of having to define it every single time, I've done a subtemplate to determine whether a product is: in stock, out of stock, discontinued etc.
So in my main template, I want to display something based on the returned value.
When I use #Raw(Model.TemplateUtil.Subtemplate("SubAvailabilityCheck")) I get the proper value (ie. IN STOCK or COMING SOON etc.)
but when I add the following to declare my variable:
string stockCheck = Raw(Model.TemplateUtil.Subtemplate("SubAvailabilityCheck"));
It gives me: ERROR:Cannot implicitly convert type 'RazorEngine.Text.RawString' to 'string'
When I convert to string it outputs the entire html code with the default html comments that mark the beginning and end of the template etc. Where as RAW strips everything but the content.
Any way to work around this?
The Raw() function simply wraps the string you pass it in a RawString, which tells RazorEngine not to HTML-escape it.
As the error is trying to tell you, a RawString isn't a string and cannot be stored in a string variable.
Change the variable to be of type RawString.

no System.IO.File.ReadLines(file).Count() in .Net 4?

I am trying to read the number of lines of a file
I found here (stackoverflow) that the best way to read the number of lines in a large file is by using the following code:
int count = System.IO.File.ReadLines(file).Count();
However, I can't make it compile. Does any know what is the problem?
Error 5 'System.Collections.Generic.IEnumerable<string>' does not
contain a definition for 'Count' and no extension method 'Count'
accepting a first argument of type
'System.Collections.Generic.IEnumerable<string>' could be found (are
you missing a using directive or an assembly reference?)
Thanks,
Eyal
Count<T>() is an extension method for objects of IEnumerable<T>. Try adding a using statement for the namespace System.Linq.
Could you do:
int count = File.ReadAllLines(#"C:\filepath\file.txt").Length;
EDIT: As pointed out in the comments, this could (will) perform badly for large files. For a similar question with more detailed explanation why, view Determine the number of lines within a text file
This is count for line from window application in read from text file:
int linecount = System.IO.File.ReadAllLines(#"D:\yfile.txt").Length;
MessageBox.Show(linecount != null && (!string.IsNullOrEmpty(linecount.ToString())) ? linecount.ToString() : "Unable To Count");

Categories