|
That didn't work. dwData should be (IntPtr)copyDataType which worked.
Thanks
|
|
|
|
|
HI,
My question is,What is Stack and Heap in C#???
As i search in net also,but i didn't understand it clearly?
So,can anyone explain me with simple example?
Thanks...
|
|
|
|
|
|
Not really, no... it's a complicated subject.
The first thing you need to get sorted is the difference between a variable, a reference, and an instance - because without that, none of the rest of this will make any sense.
An instance is an example of a class. Thinking about cars, an instance is a car you can physically get into and drive - it is distinct from all other cars in that it will have a different registration / licence number, may be a different colour, may have a different owner. Each physical car is a different instance of a generic subject "A Car".
A reference is a "pointer" to an instance: "my car" is a reference to a physical vehicle, "your car" is a reference to a different one. "This car" could refer to the same vehicle as "my car" now, and to "your car" in a moment as we physically move from one to another. Clearly, you can have many references to the same instance!
A variable is a named "place" that holds a value - this could be a number like 12 or 14, or a reference to an instance.
So when we say:
Car myCar = new Car("Mercedes", "A160", Color.Red); We declare a variable called myCar which holds a reference to an instance which is created via the new keyword.
A variable doesn't have to hold a reference : it can hold a value instead:
int i = 666; We create a variable called i which contains the value 666
This is important, because we have different names for each type of variable: we call the first one a reference type and the second a value type and how they behave is different.
-- We'll get to stack and heap soon, honest! --
When you copy a variable, it's important to know if it's a reference type or value type, because they seem to behave very differently:
int i = 666;
int j = i;
i = i + 1;
Console.WriteLine("{0}:{1}", i, j); You expect to get "667:666" because if you didn't, then doing any kind of math would get very difficult!
And you do: i and j are value types, so the value in i is copied into j, and then incremented - that doesn't affect the value in j because it's a copy of the value. If you think about variables as "pockets" it makes sense: you have five pennies in your right trouser pocket, and you copy the value to your left trouser pocket - you now have five coins in each pocket. Adding a coin to the right pocket doesn't affect the number of coins in the left (unfortunately).
When you do this with reference types it seems to work very differently:
MyClass a = new MyClass();
a.Value = 666;
MyClass b = a;
a.Value = a.Value + 1;
Console.WriteLine("{0}:{1}", a.Value, b.Value); This time we get "667:667" because the reference in a is copied to b and so both variables reference the same instance.
-- Stack and heap coming up - very soon now --
It looks like they are doing totally different things, but they aren't, not really - the assignment is doing the same thing in both cases: it's copying the value in one variable to the other variable. It's just that the effects are different because in the first case it copies the value and in the second it copies the reference .
And if any of that make sense, we can now get around to the stack and the heap!
The heap is a big "lump" of memory which is sorted out by the Garbage collector and all classes, methods and threads in your application share it. The stack on the other hand is specific to a thread, and everything on the stack is discarded when you exit the method.
That doesn't mean a lot, does it?
Let's try starting by using them both.
private MyClass MyMethod(MyOtherClass parameter)
{
int i = parameter.Index;
MyClass mc = new MyClass();
mc.Name = parameter.UserName;
mc.ID = i + 1;
return mc;
}
When you call it, this uses quite a few bits of memory:
1) The stack gets the return address of the method - so it knows where to go back to.
2) The stack gets a copy of the reference to the parameter (the actual class instance is on the heap already)
3) The stack gets an integer called i (which is given a copy of the Index value from the parameter)
4) The stack gets a variable called mc (which can hold a reference to a MyClass instance)
5) The heap constructs an new instance of MyClass and the reference to it is copied to mc
6) The parameter reference is used to identify the instance of MyOtherClass you passed into the method, and it's UserName property is copied to the MyClass instance Name property. This copies a reference to the property and UserName and Name are both reference types.
7) The value in i is fetched, incremented by one, and is copied to the MyClass instance ID property.
8) The reference to the instance of MyClass it returned to the outside world.
9 The method ends, so the stack is reset to the state it was in when the method started - which releases the space occupied by both the i and mc variables - and the return address is fetched from the stack so the method knows where to go back to.
At this point, i and mc no longer exist - they have gone out of scope and the space for them has be recycled. But... the instance of MyClass that mc referred to hasn't - it is on the heap and will only ever be released and recycled by the Garbage Collector. Which means that the mc variable is lost, but the data it references is not - if you have copied the reference to a variable outside the method, then you can still access it from the rest of your program. So the outside world can happily use the reference the method returned:
MyClass newUser = MyMethod(registrationInfo);
Console.WriteLine(newUser.Name);
The heap holds the instances of all reference types, the stack holds all the variables your methods declare.
Does that make sense?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hello!^^ Im currently trying to take all parametres from an object(they are all strings) and pass onto a single string.
To do this i create 3 lists, the first is where all my objects are added, the second is one i use to add all the parametres from a single object from the first list. The second list only hold the parametres of 1 object at a time. When it has all the parametres of the single object it passes them on to my third and final list(and then clears itself) which is the list i want printed with my stringbuilder. If the objects in my first list only contain 1 string, it works just fine, but when the objects has several parametre values the stringbuilder result of the third list returns this : "System.Collections.Generic.Listxxx" instead of the string item in the list, why?
Here is some of the code:
private List<aTableRow> allTableRows = new List<aTableRow>();
private List<string> singleTableRowValues = new List<string>();
private List<string> finalList = new List<string>();
public void SetTableContent()
{
StringBuilder builder = new StringBuilder();
foreach (var tableRow in allTableRows)
{
singleTableRowValues.Add(tableRow.Product.ToString());
singleTableRowValues.Add(tableRow.Price.ToString());
singleTableRowValues.Add(tableRow.StartDate.ToString());
singleTableRowValues.Add(tableRow.EndDate.ToString());
singleTableRowValues.Add(tableRow.Where.ToString());
singleTableRowValues.Add(tableRow.Note.ToString());
singleTableRowValues.Add(tableRow.TippedBy.ToString());
builder.Append(singleTableRowValues).Append(" - ");
tableContent = builder.ToString();
finalList.Add(tableContent);
singleTableRowValues.Clear();
tableContent = "";
}
foreach (var preparedItem in finalList)
{
builder.Append(singleTableRowValues).Append(" - ");
allTableContent = builder.ToString();
}
}
|
|
|
|
|
..because singleTableRowValues on the line below is a list of strings;
builder.Append(singleTableRowValues).Append(" - ");
If you "toString" that, you get the description as in your title. If you want the values from the list as a single string, I'd recommend the string.Join method.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
In addition to what Eddy said about why you're getting "System.Collections.Generic.List", you've significantly over-complicated this!
Consider something like:
public void SetTableContent()
{
StringBuilder builder = new StringBuilder();
foreach (var tableRow in allTableRows)
{
builder.Append(tableRow.Product.ToString()).Append(" - ");
builder.Append(tableRow.Price.ToString()).Append(" - ");
builder.Append(tableRow.StartDate.ToString()).Append(" - ");
builder.Append(tableRow.EndDate.ToString()).Append(" - ");
builder.Append(tableRow.Where.ToString()).Append(" - ");
builder.Append(tableRow.Note.ToString()).Append(" - ");
builder.AppendLine(tableRow.TippedBy.ToString());
}
allTableContent = builder.ToString();
}
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
I want to convert ms word file .docx into .html with image and equation without any paid .dll file.
|
|
|
|
|
The way your question is stated makes it look like you are asking for permission. I'm going to guess that you are looking for a free alternative?
A docx-file is a compressed archive (a zip-file) that you can unpack and read.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I would suggest you using Open xml sdk 2.5 along with Power tool. Trust me, it works everytime.
|
|
|
|
|
If you have DOCX installed on your machine, then you can use Word automation (Interop object) to convert DOCX to HTML
referred assembly 'Microsoft Office 8.0 or 14.0 object library' and you can do it easily
see below link
Microsoft Interop API to convert the .doc, .docx, .dot, .dotx and .xls,.xlsx, .rtf to HTML[^]
OR
you can use OpenXmlSDK to convert it to HTML, PowerTools for Open XML has DOCX to HTML facility
check here[^]
Hope it helps
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
Hi all,
I want in my project(in all forms or object of my project) when user Alt+Shift pressed change text of a label.
please help me.
thanks
|
|
|
|
|
Without Hooking you can't do it automatically in all forms - and that's not a nice idea - hooking is complicated and prone to crashing your system if you make a mistake.
Instead, create a form class called "KeyTrapForm" and derive all your project forms from that.
In KeyTrapForm, override ProcessCmdKey:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData & Keys.Modifiers) == (Keys.Shift | Keys.Alt))
{
Console.WriteLine("Both pressed");
}
return true;
}
And do your label stuff.
Do be aware that you will get a lot of passes on the if condition as SHIFT and ALT do autorepeat while held down.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
modified 4-Jul-16 5:27am.
|
|
|
|
|
hi to all
i am new with c#
i tray to sum time in grid view footer
i can sum integer but not time
this is my code
int TotalQuantity = 0;
DateTime dt = new DateTime();
TimeSpan ts();
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TotalQuantity += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Quantity"));
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "TotalQuantity";
e.Row.Cells[0].Font.Bold = true;
e.Row.Cells[1].Text = TotalQuantity.ToString();
e.Row.Cells[1].Font.Bold = true;
e.Row.Cells[2].Text = "TotalTime";
e.Row.Cells[2].Font.Bold = true;
e.Row.Cells[3].Text = dt.ToShortTimeString();
e.Row.Cells[3].Font.Bold = true;
}
}
|
|
|
|
|
Where is the code to calculate the total time, and what values are you using as inputs?
|
|
|
|
|
the code is in the body of the message
|
|
|
|
|
I see two lines that are commented out. What happens when you uncomment them, and why are you converting to int in order to add to a double ? What value is returned from Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Time")); ? What do you expect to see from the line e.Row.Cells[3].Text = dt.ToShortTimeString(); ?
|
|
|
|
|
static int Pow(int x, int y=2)
{
int result = 1;
for (int i = 0; i < y; i++)
{
result *= x;
}
return result;
}
static void Main(string[] args)
{
Console.WriteLine(Pow(6));
Console.WriteLine(Pow(3, 4));
}
So, its to pow Method that's confusing me. If someone can brake it down for me in pseudo code this would be a great help. Thanks
|
|
|
|
|
Why not step through the code in the debugger and examine the variable contents after each line of code?
About the only two things that might confuse you are the method header where you have int y = 2 and the result *= x; line.
The method header sets a default value of 2 for y if the caller doesn't provide a 2nd parameter in the method call.
The result line is a shortcut that expands to
result = result * x;
|
|
|
|
|
Not really much that you can break it down to:
static int Pow(int x, int y=2)
{
int result = 1;
for (int i = 0; i < y; i++)
{
result *= x;
}
return result;
}
|
|
|
|
|
Are you aware that the .NET C# Math library includes a 'Pow function: [^]. Note that this function takes Type 'double arguments, and returns a Type 'double result.
Tutorial on Math.Pow here: [^].
Note that my posting this does not mean that I think the OP working this out for themselves is not a good thing
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
This function calculate nth power of a number (where n is the input given by user), if user has not given any input to application, it default collect it as 2 and calculate its 2nd power.
To calculate power system has multiply same number with itself upto nth time and get the desired value
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
I was trying to load a type using only the type FullName and the Assembly name (unqualified, no version number or hash signature)
It seemed to work for some types....
But some .NET dll does not work, for example:
Type.GetType("System.Drawing.Bitmap,System.Drawing")
returns null (even though my app is currently displaying some WinForm Forms).
Any clue ho how to fix that?
I am also hampering myself by trying to write code that will work on Android, .NET Core, UWP....
[EDIT]
AppDomain.Current.GetAssemblies() works on iOS, Android, .NET desktop!
And .NET Core has something similar..
problem solved!
modified 2-Jul-16 8:21am.
|
|
|
|
|
An overloaded version of the same method allows you to throw an exception if there's an error. It would throw a TypeLoadException[^].
On my machine it is complaining that there is no such type in "ConsoleApplication14". Which is not exactly what I expected
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
My first thought would be that the current Project does not have a valid reference to System.Drawing, but, I am not familiar with the context (UWP ?) you are working with here.
cheers, Bill
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|