Validating exact length of value [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am using Microsoft Visual Developer C#. I am trying to validate a textbox so that the Book Code (int) entered is exactly 4 characters long. I used the Range Validator control to do so. For the MaximumValue and MinimumValue properties of the Range Validator I made them both equal 4. However this doesnt seem to work. Am I doing it wrong?

This is very simple, you should probably be thinking more about your problem before posting. However, I will empathize with a beginner and give a couple of solutions.
option 1 - convert to a string and check it's length;
string myVar = BookCode.ToString()
if (myVar.Length < 5)
// it's good!
else
// ERROR
option 2 - the largest value less than ten thousand is 9999, a four digit value.
if (BookCode < 10000)
// it's good
else
// it's bad

If you're just having users enter text into a text box and once they press some type of submit button you want to confirm that the text is 4 characters long then you can check that with inputControl.Text.Length == 4
from there you can show a message box and return if it's not equal to 4 or continue if it is.

Related

Generating folders using textboxes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello experts, I have to generate series of folders from a TextBox into specified location.I am having two textboxes to specify the limit of folders(say 30 folders).The problem am facing is that the folder names that i will be providing are alpha-numeric(say 121cs3h101) .
How to set limit when i provide an alpha-numeric values?
(For example: i provide textbox1=12cs3h101 and textbox2=12cs3h131 , i need the series limit to be generated). I am working with visual studio 2013 in c# windows form application. Thanks in advance.
ok I will try to give you a lead.
To parse a string or find specific characters one can use RegEx.Match or a simler method called String.Split. In both cases you have to be aware how your string is structured and how it can vary. The limits of variation are very important.
If as you say the beginning is always"12cs3h" you can either split the string at the character 'h'.
string[] sa = s.Split('h');
Or you can even use the index of 'h' (since the length seems to be fixed) and take the rest of the string to get the numbers.
int index = s.IndexOf('h');
The rest is up to you, ... convert, enumerate and so on.
EDIT: There is a nice method that does the enumeration job for you: Enumerable.Range Good luck

how do i display how many digits the number has? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Currently very new to C# and coding , so i will be more than happy if someone will explain me how to display how many digits the number has. For example the number 12345 has 5 digits.the main theme in the class is while loops so the answer probably need to contain while loop.TY
You can either use this
Math.Abs(myint).ToString().Length
and if you absolutely must use a while loop then
number = Math.Abs(number);
int length = 1;
while ((number /= 10) >= 1)
length++;
To test code
string.Trim().Replace("-","").Length
so if you have a number you should make it a string first using ToString()
The Length returns the number of characters that you hold within your string minus your white spaces (Because of the Trim()),i don't see why you would want to use the while loop in the first place.
Edit : if you have a minus number the .Replace() will take care of that.

What does string.IndexOf do? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm working through string manipulation, where I need to find characters or copy some part of the string that the user has input and divide it to 3 different areas. I'm not asking anything here about code, I'll do it myself, but I was searching in the documentation that Microsoft provide about the IndexOf method and its 8 overloads and I can't really understand how to apply it. I simply can't understand what it is supposed to do.
Returns the first appearance of a specified char.
For example
string x = "Hello World";
x.indexOf("W");
it will return 6 (0 based count).
The overloads let you choose for example, where you want to start searching.. like
x.indexOf("W", 7); it will return -1 because W is at position 6 so if starts at 7 it won't find any.
I hope this helps ! the best way is to play with it
This also works with arrays.
I believe the MSDN explanation is pretty clear.
For example:
string something = "something";
int indexOfT = something.IndexOf("t"); // => returns 4
Reports the zero-based index of the first occurrence of the specified string in this instance.
So if "t" is in the fifth position of "something", 4 would be it's zero-based index.

Can I add numbers to int value instead of changing it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is it possible to add numbers to the total value of an int cell instead of set its value? Correctly I'm using a list and I and things are getting weird.. the list is <int> list and its value is jumping into random numbers without any reason. I think that changing the count type (from list into int) will solve it. (c#. visual studio 2013).
Doesn't look so hard to me...?
int variable = 10; // start with 10 points
variable++; // add 1
variable += 3 // add 3

Should the currecy textbox in winforms gets rounding done all the time? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have standard windows textbox that is formatted for currency but there is minor issue, when the user enter a value like 19999.9999999, this get rounded to 20000.00. If the user enters the value as 19999.99 this value will not get rounded. In the big industry, what is the correct way to format this number?
I need this to be rounded without harming the property value!
How about Math.Round.
try like this
decimal dValue = Math.Round(19999.999999M, 3);
Take a look at the msdn for complete reference of Math.Round() method.

Categories

Resources