Forgive, new to c# always been a PHP man.
Have a form to generator random string for passwords based on the number of characters selected from drop down.
Having a problem with obtaining the values from drop down (casting the object to an int).
code:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string endword = "";
int chrnumber = Convert.ToInt16(comboBox1.SelectedValue);
string[] Nochars = { "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "#", "z", "x", "c", "v", "b", "n", "m", "/", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "{", "}", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "#", "~", "Z", "X", "C", "V", "B", "N", "M", "<", ">", "?", "!", "£", "$", "%", "^", "&", ".*", "(", ")", "_", "+", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=" };
Random rndchar = new Random();
for (int i = 0; i < chrnumber; i++)
{
int iSelect = rndchar.Next(0, Nochars.Length);
string word1 = Nochars[iSelect];
string word2 = word1;
if (i == 0) { endword = word1; } else { endword += "." + word2; }
}
pwd.Text = endword;
}
//On form load
public Form1(){
List<Values> reasons = new List<Values> {
new Values("0", 0),
new Values("1", 1),
new Values("2", 2),
new Values("3", 3),
new Values("4", 4),
new Values("5", 5) };
comboBox1.DataSource = reasons;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
reason = Convert.ToInt32(comboBox1.SelectedValue);
}
here the default value is 0, so your error will not occur at any point. I hope your combox datasource default value has ", which is causing this error.
or make sure the value part is not a alphabets/other characters instead of numbers in your datasource.
try this
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string endword = "";
int chrnumber = int.Parse(this.comboBox1.GetItemText(this.comboBox1.SelectedItem).ToString());// change line
string[] Nochars = { "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "#", "z", "x", "c", "v", "b", "n", "m", "/", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "{", "}", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "#", "~", "Z", "X", "C", "V", "B", "N", "M", "<", ">", "?", "!", "£", "$", "%", "^", "&", ".*", "(", ")", "_", "+", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=" };
Random rndchar = new Random();
for (int i = 0; i < chrnumber; i++)
{
int iSelect = rndchar.Next(0, Nochars.Length);
string word1 = Nochars[iSelect];
string word2 = word1;
if (i == 0) {
endword = word1;
}
else {
endword += "." + word2;
}
}
pwd.Text = endword;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have written this code to generate a random password. When I run it I get an error saying that public Main() method is supposed in public class.
using System;
namespace Rnadom_Password_generator
{
class Program
{
void Main()
{
string[] allCharacters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "U", "V", "W", "Q", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "7", "9", "0", "!", "#", "#", "$", "%", "^", "&", "*", "(", ")", "-", "=", "+", "*", "/"};
string password = "";
Random character = new Random();
int passwordLength = 10;
for (int i = 0; i < passwordLength; i++)
{
int rnadomNum = character.Next(0, allCharacters.Length);
password += allCharacters[rnadomNum];
Console.WriteLine(password);
}
}
}
}
What should I do?
Make sure your main method is static. Like this.
using System;
namespace Rnadom_Password_generator
{
class Program
{
static void Main(string[] args)
{
string[] allCharacters = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "U", "V", "W", "Q", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "7", "9", "0", "!", "#", "#", "$", "%", "^", "&", "*", "(", ")", "-", "=", "+", "*", "/" };
string password = "";
Random character = new Random();
int passwordLength = 10;
for (int i = 0; i < passwordLength; i++)
{
int rnadomNum = character.Next(0, allCharacters.Length);
password += allCharacters[rnadomNum];
Console.WriteLine(password);
}
}
}
}
So basically, I have a list, and I have an empty string. My goal is to search if x has one of the values inside of it. Here is the code
string x = Console.ReadLine();
var strings = new List<string>() {"A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L", "M", "N", "O", "P", "Q","R", "S", "T", "U", "V", "W", "X", "Y","Z", "a", "b", "c", "d", "e", "f", "g", "h", "i","j", "k", "l", "m", "n", "o", "p", "q","r", "s", "t", "u", "v", "w", "x", "y","z"};
For example, if someone inputted "A B a 1" I want to see if there is a thing from the list in it (which there is) How would I do this? (C#)
I hope I understood you question. You can check if it exists using following.
var result = inputString.Split(new []{" "},StringSplitOptions.RemoveEmptyEntries)
.Any(x=>strings.Contains(x));
You need to split the input string based on whitespace, and compare if any of them exist in your original collection. You can do so using Linq
I have a problem with replaceing non-ASCII characters to their ASCII equivalent.
I can do that simply in Windows Forms, using something like this:
Encoding.UTF8.GetString(Encoding.GetEncoding("Cyrillic").GetBytes("Crazy text with żźćńąśłęó and other special characters"));
and i will receive something like this:
Crazy text with zzcnasleo and other special characters
Problem is, that in Silverlight I do not have Encodings like "Cyrillic" - I can use only four of them:
utf-8 - UTF8Encoding
utf-16 - UnicodeEncoding (little-endian)
utf-16BE - UnicodeEncoding (big-endian)
utf-16LE - UnicodeEncoding (little-endian)
Do you know how to achive this same result in C# Silverlight?
I wrote my own converter, but which isn't so cool:
private string Convert(string value)
{
string[] nonAsciiCharacters = new string[] { "À", "Á", "Â", "Ã", "Å", "Ä", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ñ", "Ò", "Ó", "Ô", "Ö", "Õ", "Ù", "Ú", "Û", "Ü", "Ý", "à", "á", "â", "ã", "ä", "å", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ý", "ÿ", "Ā", "ā", "Ă", "ă", "Ą", "ą", "Ć", "ć", "Ĉ", "ĉ", "Ċ", "ċ", "Č", "č", "Ď", "ď", "Đ", "đ", "Ē", "ē", "Ĕ", "ĕ", "Ė", "ė", "Ę", "ę", "Ě", "ě", "Ĝ", "ĝ", "Ğ", "ğ", "Ġ", "ġ", "Ģ", "ģ", "Ĥ", "ĥ", "Ĩ", "ĩ", "Ī", "ī", "Ĭ", "ĭ", "Į", "į", "İ", "ı", "Ĵ", "ĵ", "Ķ", "ķ", "ĸ", "Ĺ", "ĺ", "Ļ", "ļ", "Ľ", "ľ", "Ŀ", "ŀ", "Ł", "ł", "Ń", "ń", "Ņ", "ņ", "Ň", "ň", "ʼn", "Ŋ", "ŋ", "Ō", "ō", "Ŏ", "ŏ", "Ő", "ő", "Ŕ", "ŕ", "Ŗ", "ŗ", "Ř", "ř", "Ś", "ś", "Ŝ", "ŝ", "Ş", "ş", "Š", "š", "Ţ", "ţ", "Ť", "ť", "Ũ", "ũ", "Ū", "ū", "Ŭ", "ŭ", "Ů", "ů", "Ű", "ű", "Ų", "ų", "Ŵ", "ŵ", "Ŷ", "ŷ", "Ÿ", "Ź", "ź", "Ż", "ż", "Ž", "ž", "Ơ", "ơ", "Ư", "ư", "Ǎ", "ǎ", "Ǐ", "ǐ", "Ǒ", "ǒ", "Ǔ", "ǔ", "Ǖ", "ǖ", "Ǘ", "ǘ", "Ǚ", "ǚ", "Ǜ", "ǜ", "Ǻ", "ǻ", "Ǿ", "ǿ" };
string[] asciiCharacters = new string[] { "A", "A", "A", "A", "A", "A", "C", "E", "E", "E", "E", "I", "I", "I", "I", "N", "O", "O", "O", "O", "O", "U", "U", "U", "U", "Y", "a", "a", "a", "a", "a", "a", "c", "e", "e", "e", "e", "i", "i", "i", "i", "n", "o", "o", "o", "o", "o", "o", "u", "u", "u", "y", "y", "A", "a", "A", "a", "A", "a", "C", "c", "C", "c", "C", "c", "C", "c", "D", "c", "D", "d", "E", "e", "E", "e", "E", "e", "E", "e", "E", "e", "G", "g", "G", "g", "G", "g", "G", "g", "H", "h", "I", "i", "I", "i", "I", "i", "I", "i", "I", "i", "J", "j", "K", "k", "k", "L", "l", "L", "l", "L", "l", "L", "l", "L", "l", "N", "n", "N", "n", "N", "n", "n", "N", "n", "O", "o", "O", "o", "O", "o", "R", "r", "R", "r", "R", "r", "S", "s", "S", "s", "S", "s", "S", "s", "T", "t", "T", "t", "U", "u", "U", "u", "U", "u", "U", "u", "U", "u", "U", "u", "W", "w", "Y", "y", "Y", "Z", "z", "Z", "z", "Z", "z", "O", "o", "U", "u", "A", "a", "I", "i", "O", "o", "U", "u", "U", "u", "U", "u", "U", "u", "U", "u", "A", "a", "O", "o" };
value = Regex.Replace(name.Trim(), #"[\s]", "_"); // Replace white with under character
for (int currentChar = 0; currentChar < nonAsciiCharacters.Length; currentChar ++)
{
value = value.Replace(nonAsciiCharacters[i], asciiCharacters[currentChar ]);
}
return Regex.Replace(value, #"[^0-9a-zA-Z_]+", string.Empty); // Remove white characters
}
Maybe somebody will make it better and put at this post :-)
When I try to replace the the inputed morse code into text I get a problem. Basically the problem is that when the user inputs the ".", the prorgram prints the letter "E"(which is "." in morse code) and ignores all of the other periods after it to make other letters with two or more consecutive periods.
How should I solve this?
I know that this is probably a very newbie question but I've been struggling all day to find an answer for this.
Here's the code
public partial class Morsetext : PhoneApplicationPage
{
public string[] aakkoset = { ".", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T",
"U", "V","W", "X", "Y",
"Z", "Ä", "Ö", "0", "1",
"2", "3", "4", "5", "6",
"7", "8", "9", "?", ":",
",", "#", "/", "=", " ",
};
public string[] morse = {".-.-.-", ".-", "-...", "-.-.", "-..", ".",
"..-.", "--.", ".... ", "..", ".---",
"-.-", ".-..", "--", "-.", "---",
".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--",
"--..", ".-.-", "---.", "-----", ".----",
"..---", "...--", "....-", ".....", "-....",
"--...", "---..","----.", "..--..", "---...",
"-....-", ".--.-.", "-..-.", "-...-", " ",
};
public Morsetext()
{
InitializeComponent();
}
private void bShort_Click(object sender, RoutedEventArgs e)
{
char piste = '.';
tBoxMorse2.Text += piste.ToString();
}
private void tBoxMorse2_TextChanged(object sender, TextChangedEventArgs e)
{
tBlockText2.Text = tBoxMorse2.Text.ToUpper()
.Replace(morse[0],aakkoset[0])
.Replace(morse[1],aakkoset[1])
.Replace(morse[2],aakkoset[2])
.Replace(morse[3],aakkoset[3])
.Replace(morse[4],aakkoset[4])
.Replace(morse[5],aakkoset[5])
.Replace(morse[6],aakkoset[6])
.Replace(morse[7],aakkoset[7])
.Replace(morse[8],aakkoset[8])
.Replace(morse[9],aakkoset[9])
.Replace(morse[10],aakkoset[10])
.Replace(morse[11],aakkoset[11])
.Replace(morse[12],aakkoset[12])
.Replace(morse[13],aakkoset[13])
.Replace(morse[14],aakkoset[14])
.Replace(morse[15],aakkoset[15])
.Replace(morse[16],aakkoset[16])
.Replace(morse[17],aakkoset[17])
.Replace(morse[18],aakkoset[18])
.Replace(morse[19],aakkoset[19])
.Replace(morse[20],aakkoset[20])
.Replace(morse[21],aakkoset[21])
.Replace(morse[22],aakkoset[22])
.Replace(morse[23],aakkoset[23])
.Replace(morse[24],aakkoset[24])
.Replace(morse[25],aakkoset[25])
.Replace(morse[26],aakkoset[26])
.Replace(morse[27],aakkoset[27])
.Replace(morse[28],aakkoset[28])
.Replace(morse[29],aakkoset[29])
.Replace(morse[30],aakkoset[30])
.Replace(morse[31],aakkoset[31])
.Replace(morse[32],aakkoset[32])
.Replace(morse[33],aakkoset[33])
.Replace(morse[34],aakkoset[34])
.Replace(morse[35],aakkoset[35])
.Replace(morse[36],aakkoset[36])
.Replace(morse[37],aakkoset[37])
.Replace(morse[38],aakkoset[38])
.Replace(morse[39],aakkoset[39])
.Replace(morse[40],aakkoset[40])
.Replace(morse[41],aakkoset[41])
.Replace(morse[42],aakkoset[42])
.Replace(morse[43],aakkoset[43])
.Replace(morse[44],aakkoset[44]);
}
Morse code can be decoded using dichotomic search. The decision tree looks something like this
The simplest solution is to reorder your replaces from longest morse string first to shortest morse string last.
I need help with translating some code from VB to C#.
Public Function ToBase36(ByVal IBase36 As Double) As String
Dim Base36() As String = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
Dim v As String
Dim i As Decimal
Do Until IBase36 < 1
i = IBase36 Mod 36
v = Base36(i) & v
IBase36 = Math.DivRem(Long.Parse(IBase36), 36, Nothing)
Loop
Return v
End Function
My problem is how type conversion works in VB, and this line gives me most trouble since IBase36 is a double, Math.DivRem() in this case should return long and Long.Parse() need string.
IBase36 = Math.DivRem(Long.Parse(IBase36), 36, Nothing)
Here is my translated, working code thanks to the JaredPar and others
public static string ToBase36(double IBase36)
{
string[] Base36 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
string v = null;
long i = default(long);
while (!(IBase36 < 1))
{
IBase36 = Convert.ToDouble(Math.DivRem(Convert.ToInt64(IBase36), 36, out i));
v = Base36[i] + v;
}
return v;
}
To translate it's important to understand first how the VB.Net code functions. Under the hood it will essentially generate the following
Dim unused As Long
IBase36 = CDbl(Math.DivRem(Long.Parse(CStr(IBase36)), 36, unused)
Note: The unused variable is necessary because the third argument is an out. This is an important difference when translating to C#.
The most natural C# equivalent of the above is
long unused;
IBase36 = Convert.ToDouble(Math.DivRem(Long.Parse(IBase36.ToString()), 36, out unused);
Now try it:
long unused = null;
IBase36 = Convert.ToDouble(Math.DivRem(Convert.ToInt64(IBase36), 36, out unused));
IBase36 = (long)IBase36 / 36;
is what you want. Math.DivRem() returns the quotient which the above integer division should take care of. The third out parameter returns the remainder but since you don't care about it in vb. net code, you can just ignore that.