As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I can't open the second form I made by clicking a button.
The second Form is in the namespace MoonFTP and it has the name Form2.
I open the first Form (Form1) and want to press a button to open Form2.
But If I write this :
private void button3_Click(object sender, EventArgs e)
{
MoonFTP.Form2.Show;
}
I get this Error :
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
MoonFTP.Form2 form2 = new MoonFTP.Form2();
form2.show();
You need to call the method with params for starters.
Create a new instance of Form2 and show that instance
MoonFTP.Form2 f = new MoonFTP.Form2();
f.Show();
You can't call directly the Show() method on the class name, you need an instance of that form.
This is possible in VB.NET for compatibility reasons, but not in C#
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Can anyone give me a practical usage where clone is used, I understand how its done and what are the types in it, but I dont see its practical usage. Also I read it somewhere that it is used to save the state of the object, but I still think that rarely the entire state of object would be so important.
If you have an object y encapsulated (for instance it is private) within object X, but you provide an accessor method (such as getY()) then you risk returning the reference to your private object and that's not good. You don't want client code to be able to get the reference to it. So instead you might copy or clone the object within getY(). As mentioned though copying is probably preferred vs. clone -- see Effective Java item 11.
Here is the example code:
public class X {
private Date y = new Date();
// other code here
public Date getY() {
//this could be bad:
//return y;
//this is good:
return new Date(y);
}
Sometimes you load your object properties to a form then bind each property to a (textbox,combobox, ...), after you do changes to the (textbox, checkbox,...) even if you cancel you can't get the original values, so cloning is the best option,
clone your object then bind, if you want to cancel the original object is always unchanged, this is just one case
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am developing a Windows Application by using C#. I have a form in which I dragged one textBox and one button control. I want to retrieve the user Full Computer Name in the textBox by clicking the button.I wrote some codes for that as..........
private void button1_Click(object sender, EventArgs e)
{
string name = System.Environment.MachineName;
textBox1.Text = name[0].ToString();
}
By clicking the button, it retrieves only the first letter(eg: D for Donald-PC) of the Computer Name by which it starts but I want to retrieve the Full Name(eg: Donald-PC). Please someone help if I have make any change in my codes.Thanks
Just output the full name, not just the 0 index:
textBox1.Text = name;
You are taking the first character of System.Environment.MachineName. You need to do this:
textBox1.Text = System.Environment.MachineName;
As an aside, textBox1 for a control name is really lousy.
You are using char index of array, drop [0] and use just name.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Consider if I have a Dictionary<Key,List<item>> TestDictionary
If I do:
List<item> someCollection;
TestDictionary.TryGetValue(someKey,out someCollection); //assuming that someCollection will not return null;
someCollection.add(someItem);
Will the object someItem be added to the collection in the Dictionary value TestDictionary[someKey] ?
Yes, you will have a reference of the object if it is a Ref type, and of course a copy if it is a Value type
Jon Skeet posted great article on this regard. But, anyway, here code snippet that can help you:
class Item
{}
void Main()
{
var dictionary = new Dictionary<int, Item>();
dictionary[1] = new Item();
Item i1;
Item i2;
dictionary.TryGetValue(1, out i1);
dictionary.TryGetValue(1, out i2);
Debug.Assert(object.ReferenceEquals(i1, i2));
}
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
For my parser i want to (depending on user input) save parse stats for the sites a user chooses to parse.
So lets say the user chooses site A, site B and site D:
I then from my threads want to be able to add different statitsitcs like; links parsed, pages parsed, duplicates found, etc. (so basically int values for the sites).
How would i create this into c#?
My idea is to make some kind of multidimensional array?
Is there a better way?
Any suggestions are very welcome :)
I would create a class that encapsulates the statistics and than use a dictionary with a website name as key.
public class Statistics
{
public int PagesParsed { get; set; }
}
var collection = new Dictionary<string, Statistics>();
collection.Add("Site A", new Statistics { PagesParsed = 42 });
var siteAStatistics = collection["Site A"];
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Never seen anything like this before. I'm doing a very simple DataReader value assignment.
story.Byline = _r["Byline"].ToString();
But the values of _r["Byline"].ToString() and story.Byline are different after the assignment. Here's the output from the Immediate window:
story.Byline
"Associated Press - FIRST LASTAssociated Press - FIRST LAST"
_r["Byline"].ToString()
"Associated Press - FIRST LAST<br />Associated Press - FIRST LAST"
Why is <br /> being removed?
Calling reader["x"].ToString() you actually calls x' type possibly overridden method ToString().
If you're sure that it's string, use reader.GetString(reader.GetOrdinal("x"))
Well, this is a bit embarrassing:
public string Byline
{
get { return !_elements.ContainsKey("Byline") ? "" : (string)_elements["Byline"]; }
set
{
string _buf = Functions.StripTags(value);
_elements["Byline"] = _buf;
}
}
Incorrect assumptions FTL. Can this question be deleted?