|
First, in comparing JavaScript and C#: you are comparing "dogs" to "cats."
JavaScript is a loosely-typed language where all variables Types are determined at run-time; C# is a strongly-typed language where all variable Types must be known at compile-time.
With C# 4.0 came the implementation of the 'dynamic keyword which allows a functionality similar to JavaScript's 'var: determination of Type of a 'dynamic C# variable can be resolved at run-time. In computer science terms this is "late-binding."
So, you can do this in C#:
dynamic v1 = "string";
v1 = 100;
v1 = new Dictionary<string, string>(); That's nonsense of course, but it illustrates the fact that the compiler will not throw an error, which indicates that a 'dynamic variable in C# can be used for "anything."
In JavaScript declaring a variable with 'var makes it a local variable. In C# 'var is short-hand for "the Type is inferred from the context," and is used to save duplicating Type declarations on the left-hand side of a variable declaration, and to hold the anonymous Types resulting from Linq operations, queries, etc.
“But I don't want to go among mad people,” Alice remarked.
“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”
“How do you know I'm mad?” said Alice.
“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll
|
|
|
|
|
please tell me about the keyDown event that using enter key.
i have used the code, but it transfer the controll to next cell after pressing the enter key two times
|
|
|
|
|
abdul rafi wrote: but it transfer the controll to next cell after pressing the enter key two times That's the default behaviour of the datagridview (and hence, what most users will expect).
You might find this[^] interesting.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Dear Sir/Mam,
How to create the 3D type image Model.
|
|
|
|
|
You need to research the 3D graphics libraries.
Veni, vidi, abiit domum
|
|
|
|
|
Or go to Victoria's Secret and ask if you can borrow one of their angels.
|
|
|
|
|
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Applica
{
class Program
{
static void Main(string[] args)
{
long Totbyte = 0;
string filePath = null;
DirectoryInfo da = new DirectoryInfo("C:\\Folder");
FileInfo[] Arr = da.GetFiles();
foreach (FileInfo ap in Arr)
{
Totbyte = ap.Length;
filePath = ap.FullName;
}
Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
string temPath = Path.GetTempFileName();
byte[] data = new byte[Totbyte];
if (File.Exists(temPath))
{
data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
}
decimal[] arry = new decimal[Totbyte];
for (int count = 0; count < data.Length; count++)
{
arry[count] = data[count];
}
byte[] data2 = new byte[Totbyte];
for (int count = 0; count < arry.Length; count++)
{
data2[count] = (byte)arry[count];
}
FileStream file = new FileStream(filePath, FileMode.Create);
BinaryWriter binarystream = new BinaryWriter(file);
binarystream.Write(data2);
binarystream.Close();
}
}
}
|
|
|
|
|
Firstly, why have you reposted this question when Harold is already helping you on your previous copy of this question[^]?
Secondly, the code you have posted does not produce a "garbage file". The bytes in data2 will match the bytes in data exactly. You will end up overwriting the original file with an exact copy of itself.
Thirdly, you state that you don't know how to use the File.WriteAllBytes method, and yet you have a perfectly valid example of it in your code!
Cleaning up your code, replacing the BinaryWriter with File.WriteAllBytes , and adding some sanity checks:
DirectoryInfo da = new DirectoryInfo("C:\\Folder");
FileInfo[] Arr = da.GetFiles();
if (Arr.Length == 0)
{
throw new InvalidOperationException("No files found.");
}
FileInfo ap = Arr[Arr.Length - 1];
long Totbyte = ap.Length;
string filePath = ap.FullName;
Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
string temPath = Path.GetTempFileName();
byte[] data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
decimal[] arry = new decimal[Totbyte];
for (int count = 0; count < data.Length; count++)
{
arry[count] = data[count];
}
byte[] data2 = new byte[Totbyte];
for (int count = 0; count < arry.Length; count++)
{
data2[count] = (byte)arry[count];
}
if (data2.Length != data.Length)
{
throw new InvalidOperationException("Wrong length!");
}
for (int index = 0; index < data.Length; index++)
{
if (data[index] != data2[index])
{
throw new InvalidOperationException("Data has changed at index " + index);
}
}
File.WriteAllBytes(filePath, data2);
data = File.ReadAllBytes(temPath);
data2 = File.ReadAllBytes(filePath);
if (data2.Length != data.Length)
{
throw new InvalidOperationException("Wrong length!");
}
for (int index = 0; index < data.Length; index++)
{
if (data[index] != data2[index])
{
throw new InvalidOperationException("Data has changed at index " + index);
}
}
If you run this code and end up with a "garbage" file, that means you started with a garbage file!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Can you put the new file in another directory so I can have two files to compare later? Or maybe just change the name so i can have two files to compare?
|
|
|
|
|
Yes, you just need to change the path in your WriteAllBytes call:
string filePath2 = Path.Combine("C:\\A Different Folder", Path.GetFileName(filePath));
File.WriteAllBytes(filePath2, data2);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
<pre>
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Applica
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo da = new DirectoryInfo("C:\\Folder");
FileInfo[] Arr = da.GetFiles();
if (Arr.Length == 0)
{
throw new InvalidOperationException("No files found.");
}
FileInfo ap = Arr[Arr.Length - 1];
long Totbyte = ap.Length;
string filePath = ap.FullName;
Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
string temPath = Path.GetTempFileName();
byte[] data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
decimal[] arry = new decimal[Totbyte];
for (int count = 0; count < data.Length; count++)
{
arry[count] = data[count];
}
byte[] data2 = new byte[Totbyte];
for (int count = 0; count < arry.Length; count++)
{
data2[count] = (byte)arry[count];
}
if (data2.Length != data.Length)
{
throw new InvalidOperationException("Wrong length!");
}
for (int index = 0; index < data.Length; index++)
{
if (data[index] != data2[index])
{
throw new InvalidOperationException("Data has changed at index " + index);
}
}
string filePath2 = Path.Combine("C:\\check", Path.GetFileName(filePath));
File.WriteAllBytes(filePath, data2);
data = File.ReadAllBytes(temPath);
data2 = File.ReadAllBytes(filePath);
if (data2.Length != data.Length)
{
throw new InvalidOperationException("Wrong length!");
}
for (int index = 0; index < data.Length; index++)
{
if (data[index] != data2[index])
{
throw new InvalidOperationException("Data has changed at index " + index);
}
}
}
}
}
|
|
|
|
|
computerpublic wrote: string filePath2 = Path.Combine("C:\\check", Path.GetFileName(filePath));
File.WriteAllBytes(filePath, data2);
If you want to write the bytes to the file whose path is stored in the filePath2 variable, then you need to pass that variable to the WriteAllBytes method, as I showed you in my previous answer[^].
string filePath2 = Path.Combine("C:\\check", Path.GetFileName(filePath));
File.WriteAllBytes(filePath2, data2);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It works. Thank you very much Richard.
|
|
|
|
|
<pre>
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Applica
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo da = new DirectoryInfo("C:\\Folder");
FileInfo[] Arr = da.GetFiles();
if (Arr.Length == 0)
{
throw new InvalidOperationException("No files found.");
}
FileInfo ap = Arr[Arr.Length - 1];
long Totbyte = ap.Length;
string filePath = ap.FullName;
Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
string temPath = Path.GetTempFileName();
byte[] data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
string arry = ASCIIEncoding.ASCII.GetString(data);
Console.WriteLine(arry);
byte[] data2 = Encoding.ASCII.GetBytes(arry);
foreach (byte element in data2)
{
Console.WriteLine("{0}={1}",element ,(char)element);
}
if (data2.Length != data.Length)
{
throw new InvalidOperationException("Wrong length!");
}
for (int index = 0; index < data.Length; index++)
{
if (data[index] != data2[index])
{
throw new InvalidOperationException("Data has changed at index " + index);
}
}
string filePath2 = Path.Combine("C:\\check", Path.GetFileName(filePath));
File.WriteAllBytes(filePath2, data2);
data = File.ReadAllBytes(temPath);
data2 = File.ReadAllBytes(filePath);
if (data2.Length != data.Length)
{
throw new InvalidOperationException("Wrong length!");
}
for (int index = 0; index < data.Length; index++)
{
if (data[index] != data2[index])
{
throw new InvalidOperationException("Data has changed at index " + index);
}
}
}
}
}
|
|
|
|
|
SORRY, THE PROBLEM IS ON LINE 61. BEFORE THE PROBLEM WAS ON LINE 41, BUT I ADDED COMMENTS AND SHIFTED THE ENTIRE THING DOWN.
|
|
|
|
|
Hardly surprising - ASCII encoding can only cope with 128 characters. If any of your bytes are greater than or equal to 128, the ASCIIEncoding class[^] will replace them with a question mark. When you re-encode the string, these bytes will have been replaced with the value 63.
You still haven't explained what you're trying to achieve.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am trying to become a good programmer, but following the examples in the books sometimes does not work. The professor in the school is also very suspicious. I think she is reading the chapters before the class and pretending like she knows. She never answers direct questions. The answer always come in the next class session. I feel I am on my own with c#. Sometimes I feel that the people who write the books, don't know any c# at all. How it is possible for examples in text not to work. I encounter this problem many times and it is very frustrating. To add to the frustration, I go online to the help forums and I continually being ask "What are you trying to achieve"? As if a non programmer is not allowed to ask questions and learn from people who know. I am trying to learn from people who know, that is what I am trying to achieve. I hope this answers the question.
|
|
|
|
|
Could you please kindly respond to my new post "Out Of Memory Exception"? It is a completely different question, so I didn't thought it wise to post it here. Thank You.
|
|
|
|
|
computerpublic wrote: I continually being ask "What are you trying to achieve"? As if a non programmer is not allowed to ask questions and learn from people who know.
We don't ask what you're trying to achieve because we don't want you asking questions; it's just that, if you tell us what you want the code to do, we might be able to point you towards a better solution. It's often better to take a step back and look at the bigger picture of what you want to do, rather than fixating on why a particular line of code doesn't do what you expect.
Your professor certainly doesn't sound like she's up to the job. Have you tried talking to your tutor about your concerns?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
<pre>
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Applica
{
class Program
{
static Random _random = new Random (); public static void Shuffle<T>(T[] array)
{
var random = _random;
for (int i = array.Length; i > 1; i--)
{
int j = random.Next(i);
T tmp = array[j];
array[j] = array[i - 1];
array[i - 1] = tmp;
}
} static void Main(string[] args)
{
byte[] outdata = new byte[1];
BitArray[] bits2 = new BitArray[8];
char[] array = {'1','0','1','0','1','0','1','0'};
Shuffle(array);
for (int i = 0; i < 8; i++)
bits2[i] = array[1];
outdata[i] = (byte)bits2;
}
}
}
|
|
|
|
|
|
I would be interested to know exactly what you are trying to achieve with this, particularly as you say it is important to create the decimal array. You read an array of bytes from a file, and copy it to an array of decimals (each element 16 bytes long). You then copy those decimals back to an array of bytes, i.e back to the original, and write them out to a file. So both files contain exactly the same data.
Veni, vidi, abiit domum
|
|
|
|
|
hi!
I am in need of assistance for my project
I need a real example for this.
in the treeview there is the directory and the file that go with it
ex: ----C#
----Test.txt
I have no problem for creating the database in Sql Compact edition
for reading it in the treeview saving it and deleting it
if someone can help with this.
Daniel
|
|
|
|
|
Are you trying to show hierarchical data from your database in a treeview? If so, it would help if you explained the structure of your tables.
/ravi
|
|
|
|
|
in fact I am trying to build a snippet database for my personnel use
All the one I have seen where a bit complicated to work with
I want to create mine. It will more likely help to understand more the logic behind it
and for the other project that I have with treeview
I am Using SqlCe 3.5 and up.
For the database I have done it this way
static public String Dpath = Application.StartupPath + "\\Data\\";
static public String DB_NAME = Dpath + Application.ProductName + ".sdf";
static public String DB_PWD = "";
static public string sql;
static private string _filename;
static public string filename
{
get { return _filename; }
set { _filename = value; }
}
static private string _filecontent;
static public string filecontent
{
get { return _filecontent; }
set { _filecontent = value; }
}
static public string ConnectString()
{
string connectionString = string.Format("DataSource=\"{0}\"; Password=\"{1}\"", DB_NAME, DB_PWD);
return connectionString;
}
static public void CreateDB()
{
SqlCeEngine en = new SqlCeEngine(ConnectString());
en.CreateDatabase();
en.Dispose();
en = null;
}
static public void CreateTable(string TName)
{
SqlCeConnection cn = new SqlCeConnection(ConnectString());
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
SqlCeCommand cmd;
sql = "create table " + TName + "("
+ "ID INT IDENTITY (1,1) PRIMARY KEY not null, "
+ "FileName NVarChar(50), "
+ "FileContent NTEXT)";
cmd = new SqlCeCommand(sql, cn);
cmd.ExecuteNonQuery();
cn.Close();
}
If there is a better way of doing it it will be appreciated
Thank'a Lot
Daniel
|
|
|
|