Finally and c# reference types [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am not able to figure how will this work:
public Class1 Function1()
{
DataTable dt;
try
{
dt = new DataTable();
//.. Do some work
return new Class2(byref dt);
}
finally
{
dt.dispose();
}
}
public Class2(byref DataTable dTable)
{
this.dataTable = dTable;
}
So, now if I say Class1 obj1 = Function1(); will my obj1.dataTable be disposed? or it will have proper data?

yes assuming obj1.dataTable refers to the same object you created inside Function1, it will have been disposed. Finally blocks are always executed, regardless of whether an exception is thrown or not.
Here's some more information on try-finally blocks.

Related

C# integer variable in foreach loop [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In C# I have a foreach loop where I want to ++ a integer.
The code is like this:
private void btnClick(object sender, EventArgs e)
{
int Counter = 0;
foreach (SettingsProperty currrentProperty in Properties.Settings.Default.Properties)
{
Counter++;
}
lblText.Text = Counter.ToString();
}
Simple, but of course because I have to assing the integer the variable sticks to 0, otherwise the compiler errors. So the lblText.Text prints 0 to me.
I just can't get it working properly..
Sure this is an easy one, but I couldn't find a awnser.
I think that Properties.Settings.Default.Properties is empty . So to get sure that it is empty try:
private void btnClick(object sender, EventArgs e)
{
if(Properties.Settings.Default.Properties.Count != 0)
{
int Counter = 0;
foreach (SettingsProperty currrentProperty in Properties.Settings.Default.Properties)
{
Counter++;
//Some stuff here else just use .Count without use a foreach
}
lblText.Text = Counter.ToString();
}
else
throw new Exception("Properties.Settings.Default.Properties is empty");
}
Else try to set some breakpoints before compile the code.

Need an equivalent for a C# code in VB.Net [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm doing C# And now I want to learn about VB.Net, and I want the equivalent for this:
Hide();
using (login loginForm = new login())
{
var result = loginForm.ShowDialog();
if (result == DialogResult.OK)
{
Show();
}
else
{
Close();
}
}
Hide()
Using loginForm As New login()
Dim result = loginForm.ShowDialog()
If result = DialogResult.OK Then
Show()
Else
Close()
End If
End Using
What did you try yourself? Just look up each thing on MSDN (Using statement, If statement, ...) and look at the code examples in VB.
Anyway, here is the converted code:
Hide()
Using loginForm As New login()
Dim result = loginForm.ShowDialog()
If result = DialogResult.OK Then
Show()
Else
Close()
End If
End Using

How can I prevent a ListView option from changing? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How can I prevent a ListView option from changing?
I have tried the following, however it still changes:
bool q=false;
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (q)
{
// Let change happen
}
else
{
// Stop change from happening
return;
}
}
Thank you for any help.
Save the prior value and then reset it to the prior.
int lastIndex = -1;
if (q)
{
lastIndex = (ListView)Sender.SelectedIndex;
}
else
{
// Stop change from happening
(ListView)Sender.SelectedIndex = lastIndex;
}

"No Overload Method for GetLine" Help (C#) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have tried something like: "GetLine(fileName,line)" no luck
Code:
static void Main(string[] args)
{
GetLine();
}
string GetLine(string fileName, int line)
{
......
}
You are calling the GetLine declared non-static from within a static function.
Either mark the GetLine declaration as static, or create an instance of the class containing both the functions.
it should be a static method if you want to call directly like that.
private static string GetLine(string fileName, int line)
If you want to overload the method GetLine it must be marked with the virtual indicator.
virtual string GetLine(string fileNmae, int line)
{
//Code for method goes here
}
Update :
As Mario Vernari suggested you will need to make the method static.
If you want to call the method like this GetLine() then you will need to create a new overloaded method for GetLine.
static string GetLine()
{
return "Some string message"; //Return a string.
}

how to open usercontrol and close 2 seconds [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
Hello People how can i open userControl and close in the 2 seconds and show another form?
c# WinForms
public void MyFunction()
{
firstForm.ShowDialog();
secondForm.Show();
}
public void firstForm_Load(object sender, EventArgs e)
{
Timer timer = new System.Windows.Forms.Timer() { Interval = 2000 };
timer.Tick += delegate { timer.Stop(); Close(); };
timer.Start();
}

Categories