i am trying to use Session["emailcc3"] in windows service application ..but it shows me an error session not available in this context....is there any alternative way to do that??
THIS IS MY CODE:
foreach (DataRow newRow in employee.Rows)
{
num = num - 1;
if (num == 4)
{
Session["emailto"] = newRow["EMAILID"].ToString();
}
if (num == 3)
{
Session["emailcc1"] = newRow["EMAILID"].ToString();
}
if (num == 2)
{
Session["emailcc2"] = newRow["EMAILID"].ToString();
}
if (num == 1)
{
Session["emailcc3"] = newRow["EMAILID"].ToString();
}
}
string emailto = Session["emailto"].ToString();
string emailcc1 = Session["emailcc1"].ToString();
string emailcc2 = Session["emailcc2"].ToString();
string emailcc3 = Session["emailcc3"].ToString();
If I took a wild guess, I think all that you want is this:
string emailto = null;
string emailcc1 = null;
string emailcc2 = null;
string emailcc3 = null;
foreach (DataRow newRow in employee.Rows)
{
num = num - 1;
switch (num)
{
case 4:
emailto = newRow["EMAILID"].ToString();
break;
case 3:
emailcc1 = newRow["EMAILID"].ToString();
break;
case 2:
emailcc2 = newRow["EMAILID"].ToString();
break;
case 1:
emailcc3 = newRow["EMAILID"].ToString();
break;
default:
//Do something here?
}
}
I'm not sure why you were using Session at all - unless you were using it because it allows you to declare variables on the fly, as it were.
Related
when i tried to use the Switch case function, it goes always to the default message besides case 5:
private void btnCandlesLight_Click(object sender, EventArgs e)
{
int result;
result = Convert.ToInt32(textBox1.Text);
switch(result)
{
case 1:
day1.Start();
candlesOne();
break;
case 2:
day2.Start();
candlesTwo();
break;
case 3:
day3.Start();
candlesThree();
break;
case 4:
day4.Start();
candlesFour();
break;
case 5:
day5.Start();
candlesFive();
break;
case 6:
day6.Start();
candlesSix();
break;
case 7:
day7.Start();
candlesSeven();
break;
case 8:
day8.Start();
candlesEight();
break;
default:
MessageBox.Show("Enter new day");
break;
}
}
When I Enter the value 1 for example to the text box, the default case works, but only when I enter the value 5 it works perfectly.
If you want to see the difference between the function "candlesOne" to "candlesFive":
The "c" variable is a variable of the seconds. i tried to use a timer in a way of lighting up the candles every 2-3 seconds.
public void candlesOne()
{
firedmatch.Left = firedmatch.Left + 100;
if (c == 1)
{
candle1.Visible = true;
}
if (c == 3)
{
candle2.Visible = true;
}
}
and:
public void candlesFive()
{
firedmatch.Left = firedmatch.Left + 100;
if(c == 1)
{
candle1.Visible = true;
}
if(c == 3)
{
candle2.Visible = true;
}
if(c == 5)
{
candle3.Visible = true;
}
if(c == 7)
{
candle4.Visible = true;
}
if(c == 11)
{
candle5.Visible = true;
}
}
I haven't found a mistake,
can you guys help me?
Thanks
Have you checked if you really get for example (int)1 as a result of the "1" input from your conversion?
On a broader scale, there is a lot of repetition in your code, you should consider refactoring it a little.
In your CandlesOne and CandlesFive methods, you use a c variable, no idea what that is or where it comes from. Those two methods (and probably the other CandlesXXX() do the same kind of things. Can't you remove complexity by generalizing the logic? Can the result used in your switch-case be passed as a parameter and used to trigger the numbers of c == X calls in the CandleXXX() methods?
This way you could remove the switch and lose a lot of complexity!
Edit
If you have further problems, consider creating a .NET Fiddle, I miss a lot of context in your code so I cannot efficiently help you here.
Some refactoring ideas for you:
// Somewhere else in your code, create a dictionary with your day1-day8 objects
var days = new Dictionary<int, Day>()
days[1] = day1;
...
days[8] = day8;
//Simplfiy your method
private void btnCandlesLight_Click(object sender, EventArgs e)
{
try
{
var dayIndex = Convert.ToInt32(textBox1.Text);
if(dayIndex > 0 && dayIndex <= 8)
{
days[dayIndex].Start(); //Get the corresponding day via its Key
LightUpCandles(dayIndex); //pass the key as a parameter
}
else
{
MessageBox.Show("Enter new day");
}
}
catch(InvalidCastException exception)
{
//Whatever you do when the textbox cannot be parsed
}
}
I still don't get what your candlesOne to five methods are really doing or why the method "candlesOne" lights up two candles (pay attention to the variable naming). I also don't get how this makes up some kind of timer... but here's a first potential refactoring for it anyway:
public void LightUpCandles(int dayIndex)
{
firedmatch.Left = firedmatch.Left + 100;
if(c == 1)
{
candle1.Visible = true;
}
if(c == 3 && dayIndex > 1)
{
candle2.Visible = true;
}
if(c == 5 && dayIndex > 2)
{
candle3.Visible = true;
}
if(c == 7 && dayIndex > 3)
{
candle4.Visible = true;
}
if(c == 11 && dayIndex > 4)
{
candle5.Visible = true;
}
}
Your switch logic is correct which I tested with the following;
int result;
result = Convert.ToInt32(textBox1.Text);
switch (result)
{
case 1:
MessageBox.Show("1");
break;
case 2:
MessageBox.Show("2");
break;
case 3:
MessageBox.Show("3");
break;
case 4:
MessageBox.Show("4");
break;
case 5:
MessageBox.Show("5");
break;
case 6:
MessageBox.Show("6");
break;
case 7:
MessageBox.Show("7");
break;
case 8:
MessageBox.Show("8");
break;
default:
MessageBox.Show("Enter new day");
break;
}
If you don't get the same results I would perhaps look at making the message boxes above display the data type of the variable.
MessageBox.Show(result.GetType().ToString());
using System;
public class ReadWriteTxt // Read and write to/from text files
{
public static string cipherTxt()
{
return System.IO.File.ReadAllText(#"myfile1");
}
internal static void decipherTxt(string result)
{
System.IO.File.WriteAllText(#"myfile2", result);
}
}
public class LetterArray
{
internal static string[] Alphabet()
{
var letterValues = new string[26];
letterValues[0] = "A";
letterValues[1] = "B";
letterValues[2] = "C";
letterValues[3] = "D";
letterValues[4] = "E";
letterValues[5] = "F";
letterValues[6] = "G";
letterValues[7] = "H";
letterValues[8] = "I";
letterValues[9] = "J";
letterValues[10] = "K";
letterValues[11] = "L";
letterValues[12] = "M";
letterValues[13] = "N";
letterValues[14] = "O";
letterValues[15] = "P";
letterValues[16] = "Q";
letterValues[17] = "R";
letterValues[18] = "S";
letterValues[19] = "T";
letterValues[20] = "U";
letterValues[21] = "V";
letterValues[22] = "W";
letterValues[23] = "X";
letterValues[24] = "Y";
letterValues[25] = "Z";
return letterValues;
}
}
public class Decipher
{
public static void Main() //Main method
{
int res = 34;
string[] letterValues = LetterArray.Alphabet();
//Create for loop that runs through every possible shift value
for (int shift = 0; shift <= 25; shift++)
{
Console.WriteLine("\nShift Value = " + shift + ": ");
// For each character in the text file
foreach (var ch in ReadWriteTxt.cipherTxt())
{
string result = string.Empty;
if (ch == ' ')
{
}
else
{
for (int i = 0; i <= 25; i++)
{
if ((ch.ToString().ToUpper()) == letterValues[i])
{
res = i;
if (shift > res)
{
// print results out
Console.Write(letterValues[26 - (shift - res)][0]);
}
else
{
Console.Write(letterValues[res - shift][0]);
}
}
}
ReadWriteTxt.decipherTxt(result);
}
}
}
}
}
I'm working on a Caesar Cipher program that decrypts cipher text that it reads in from a file.
It lists all the possible shifts throughout the alphabet.
I need to be able allow the user to enter the 'correct' shift value (e.g. the one closest to what the decrypted text should translate too) and then have the corresponding string written to a file.
What is the best to go about this?
P.S. My understanding of C# is a bit basic so please be gentle with me ;).
This is my class:
using System;
using System.Collections.Generic;
using System.Text;
namespace Num2Wrd
{
public class NumberToEnglish
{
public String changeNumericToWords(double numb)
{
String num = numb.ToString();
return changeToWords(num, false);
}
public String changeCurrencyToWords(String numb)
{
return changeToWords(numb, true);
}
public String changeNumericToWords(String numb)
{
return changeToWords(numb, false);
}
public String changeCurrencyToWords(double numb)
{
return changeToWords(numb.ToString(), true);
}
private String changeToWords(String numb, bool isCurrency)
{
String val = "", wholeNo = numb, points = "", andStr = "", pointStr = "";
String endStr = (isCurrency) ? ("Only") : ("");
try
{
int decimalPlace = numb.IndexOf(".");
if (decimalPlace > 0)
{
wholeNo = numb.Substring(0, decimalPlace);
points = numb.Substring(decimalPlace + 1);
if (Convert.ToInt32(points) > 0)
{
andStr = (isCurrency) ? ("and") : ("point");// just to separate whole numbers from points/Rupees
endStr = (isCurrency) ? ("Rupees " + endStr) : ("");
pointStr = translateRupees(points);
}
}
val = String.Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);
}
catch
{
;
}
return val;
}
private String translateWholeNumber(String number)
{
string word = "";
try
{
bool beginsZero = false;//tests for 0XX
bool isDone = false;//test if already translated
double dblAmt = (Convert.ToDouble(number));
//if ((dblAmt > 0) && number.StartsWith("0"))
if (dblAmt > 0)
{//test for zero or digit zero in a nuemric
beginsZero = number.StartsWith("0");
int numDigits = number.Length;
int pos = 0;//store digit grouping
String place = "";//digit grouping name:hundres,thousand,etc...
switch (numDigits)
{
case 1://ones' range
word = ones(number);
isDone = true;
break;
case 2://tens' range
word = tens(number);
isDone = true;
break;
case 3://hundreds' range
pos = (numDigits % 3) + 1;
place = " Hundred ";
break;
case 4://thousands' range
case 5:
case 6:
pos = (numDigits % 4) + 1;
place = " Thousand ";
break;
case 7://millions' range
case 8:
case 9:
pos = (numDigits % 7) + 1;
place = " Million ";
break;
case 10://Billions's range
pos = (numDigits % 10) + 1;
place = " Billion ";
break;
//add extra case options for anything above Billion...
default:
isDone = true;
break;
}
if (!isDone)
{//if transalation is not done, continue...(Recursion comes in now!!)
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));
//check for trailing zeros
if (beginsZero) word = " and " + word.Trim();
}
//ignore digit grouping names
if (word.Trim().Equals(place.Trim())) word = "";
}
}
catch
{
;
}
return word.Trim();
}
private String tens(String digit)
{
int digt = Convert.ToInt32(digit);
String name = null;
switch (digt)
{
case 10:
name = "Ten";
break;
case 11:
name = "Eleven";
break;
case 12:
name = "Twelve";
break;
case 13:
name = "Thirteen";
break;
case 14:
name = "Fourteen";
break;
case 15:
name = "Fifteen";
break;
case 16:
name = "Sixteen";
break;
case 17:
name = "Seventeen";
break;
case 18:
name = "Eighteen";
break;
case 19:
name = "Nineteen";
break;
case 20:
name = "Twenty";
break;
case 30:
name = "Thirty";
break;
case 40:
name = "Fourty";
break;
case 50:
name = "Fifty";
break;
case 60:
name = "Sixty";
break;
case 70:
name = "Seventy";
break;
case 80:
name = "Eighty";
break;
case 90:
name = "Ninety";
break;
default:
if (digt > 0)
{
name = tens(digit.Substring(0, 1) + "0") + " " + ones(digit.Substring(1));
}
break;
}
return name;
}
private String ones(String digit)
{
int digt = Convert.ToInt32(digit);
String name = "";
switch (digt)
{
case 1:
name = "One";
break;
case 2:
name = "Two";
break;
case 3:
name = "Three";
break;
case 4:
name = "Four";
break;
case 5:
name = "Five";
break;
case 6:
name = "Six";
break;
case 7:
name = "Seven";
break;
case 8:
name = "Eight";
break;
case 9:
name = "Nine";
break;
}
return name;
}
private String translateRupees(String Rupees)
{
String cts = "", digit = "", engOne = "";
for (int i = 0; i < Rupees.Length; i++)
{
digit = Rupees[i].ToString();
if (digit.Equals("0"))
{
engOne = "Zero";
}
else
{
engOne = ones(digit);
}
cts += " " + engOne;
}
return cts;
}
}
}
Form contains two Textboxes (textBox1 and textBox2) and a Button(button1).
I want to type an amount in numbers in textBox1 and click on the button. The amount entered in numbers in textBox1 has to be converted to text and appear in textbox2. Functions to convert are in above C# class file. I am a new student. Can anyone help me in solving this problem.
You have to create an object for 'NumberToEnglish' Class and use it in Form1.cs this way
public partial class Form1 : Form
{
NumberToEnglish neObj = new NumberToEnglish();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = neObj.changeCurrencyToWords(Convert.ToDouble(textBox1.Text));
}
}
public partial class Form1 : Form
{
NumberToEnglish Obj = new NumberToEnglish();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Obj.changeCurrencyToWords(textBox1.Text);//As your method accept a string..
}
}
Having a problem with my code here. My code is supposed to (what I want it to do) is after the game ends a spritefont appears that allows you to enter text and once that text is entered then it would write that score to the text file. But of course it does not do that. If anyone could help, would be grand.
EDIT
This is what I am using to try and input text after the game is over (Game is Space Invaders) Perhaps there is something wrong here also.
SpriteFont Label;
string txt = ".";
Keys PrevKey;
string[] HighScores = new string[5];
int count = 0;
KeyboardState ks = Keyboard.GetState();
Keys[] k = ks.GetPressedKeys();
Keys tempKey = Keys.None;
string keyChar = "";
foreach (Keys q in k)
{
Keys currentKey = q;
if (ks.IsKeyUp(PrevKey))
{
if (!(q == Keys.None))
{
switch (q)
{
case Keys.Space: keyChar = " "; break;
case Keys.Delete: txt = ""; break;
case Keys.Back: txt = txt.Remove(txt.Length - 1); break;
case Keys.Enter: HighScores[count] = txt; count++; txt = "";
if (count == 5) count = 0; break;
case Keys.End: WriteScores(); break;
case Keys.Home: ReadScores(); break;
default: keyChar = q.ToString(); break;
}
txt += keyChar;
}
}
if (currentKey != Keys.None && ks.IsKeyDown(currentKey))
{
tempKey = currentKey;
}
}
PrevKey = tempKey;
base.Update(gameTime);
}
}
}
}
public void WriteScores()
{
StreamWriter Rite = new StreamWriter(#"C:\TheScores.txt");
for (int i = 0; i < HighScores.Length; i++)
{
Rite.WriteLine(HighScores[i]);
}
Rite.Close();
}
public void ReadScores()
{
if (File.Exists(#"C:\TheHighScores.txt"))
{
StreamReader Reed = new StreamReader(#"C:\TheScores.txt");
for (int i = 0; i < HighScores.Length; i++)
{
txt += Reed.ReadLine() + "\n";
}
Reed.Close();
}
}
Change the path to a user folder like Documents or AppData. Your code as it is will throw an access violation for insufficient permissions unless you run your program as administrator, as the root of C:\ is only available from an elevated context.
I've been working with OpenDesign Specification for .dwg files about two weeks. And now, i've got all information except non-entity object and entity object. Maybe i can't understand how this information written. Concretely i need to know how to differ non-entity object and entity object. Work on C#. http://opendesign.com/files/guestdownloads/OpenDesign_Specification_for_.dwg_files.pdf on the page 98.
This is how i found out non-entity object format:
private bool ReadNonEntityObject(FileReader fileReader, DWGVersion version, long handle, long fileLoc)
{
long oldPos = fileReader.BufferPosition;
BaseTypes bReader = new BaseTypes(fileReader);
fileReader.SeekAbsolute(fileLoc);
var size = bReader.GetModularShort();
if (version.IsLaterOrEqual(DWGVersion.VersionEnum.R2010))
{
var HandleSize = bReader.GetModularChar(false);
}
var objectType = bReader.GetObjectType(version);
Console.WriteLine(BitConverter.ToString(BitConverter.GetBytes(objectType), 0).Substring(0, 2));
if (version.IsLaterOrEqual(DWGVersion.VersionEnum.R2000) && version.IsEarlier(DWGVersion.VersionEnum.R2010))
{
var ObjectSize = bReader.GetLongRaw();
}
var handl = bReader.GetHandle();
if (handl != handle)
throw new Exception("DWG file is corrupted or incorrect");
var extendedSize = bReader.GetShort();
int size1 = 0;
bool isGraphic = fileReader.GetBits(1, true)[0];
if (isGraphic)
size1 = bReader.GetLongRaw();
if (extendedSize != 0)
{
var appHandle = bReader.GetHandle();
var endPos = fileReader.BufferPosition + extendedSize;
string data = "";//DEBUG for testing
while (fileReader.BufferPosition < endPos)
{
int byteCode = bReader.GetByteRaw();
object val = null;
switch (byteCode) //TODO add all byteCode
{
case 0:
{
if (version.IsEarlier(DWGVersion.VersionEnum.R2007))
{
int N = bReader.GetByteRaw();
var codePage = bReader.GetShortRaw();
val = bReader.GetStringAscii(N);
}
if (version.IsLaterOrEqual(DWGVersion.VersionEnum.R2007))
{
//TODO
}
}
break;
case 1:
val = bReader.GetText();
break;
case 8:
{
break;
}
case 2:
val = bReader.GetCharAscii() == 0 ? '{' : '}';
break;
case 40:
bReader.Get3DDouble();
break;
case 145:
{
val = bReader.GetDouble();
break;
}
case 70:
val = bReader.GetShortRaw();
break;
case 71:
val = bReader.GetLongRaw();
break;
default:
val = "";
break;
}
data += val + " ";
//Console.WriteLine(data);
}
}
if (version.Equals(DWGVersion.VersionEnum.R13_R14))
{
var DataSize = bReader.GetLongRaw();
var persistentNum = bReader.GetByteRaw();
}
if (version.IsLaterOrEqual(DWGVersion.VersionEnum.R2004))
{
}
fileReader.SeekAbsolute(oldPos);
return true;
}
There is no isGraphic bit in non-entity object.
Extended data only. it is loop-like, see page 254.
in my experience most types - are entity objects.
i have base class for object and some heder reader in it. Many types extends from each other.
Use crc check, where it is for check you proof.