Newbie question - "With-Statement" equivalent

J

Jay Willms

Hi!
In VB I was used to use the with-statement.
C# doesn't have one, so I tried the following:

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form f = Form1;
f.button1.text = "Hi!";

}
}
}

But C# claims that there is no button1 on my Form1...
I think I do not reference Form1 correctly. Can somebody please tell me
how to do it correctly?

Thank you!
Jay
 
G

Graeme Bradbury

The code you wanted is:
private void button1_Click(object sender, EventArgs e)
{
button1.text = "Hi!";
}

button1 is a member of Form1, so just use it.
 
J

Jure Bogataj

There is no definition of button1 on instance of class Form, which is the
type of "f" variable (although it is on Form1, but compiler doesn't know
that since f is declared as Form, not as Form1):

Should be something like:
((Form1)f).button1.text = "Hi!";



Best regards,
 
G

Guest

Change:
Form f = Form1;
to:
Form f = this;

but your line could just be written:
button1.text = "Hi!";
 
K

Kevin Spencer

You wire up the Button's Click event to the handler. Example:

public Form1()
{
InitializeComponent();
button1.Click += button1_Click;
}

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Jay Willms said:
Hi!
In VB I was used to use the with-statement.
C# doesn't have one, so I tried the following:

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form f = Form1;
f.button1.text = "Hi!";

}
}
}

But C# claims that there is no button1 on my Form1...
I think I do not reference Form1 correctly. Can somebody please tell me
how to do it correctly?

It does exist in Form1, it does not in Form.

Not only that but the code cannot compile. you are assigned a Type to a
variable, you canot do that.
 
J

Jay Willms

Hi Ignacio,

I was wondering why it doesn't work but was afraid to ask again.
It strikes me that I cannot assign a type to a variable.

Then I do not understand this article from Microsoft:

VB-Code:
With MyDataStructure.GetButton(44)
.Text = "Hello"
.BackColor = Color.Blue
End With

C#-Code:
Dim b as Button = MyDataStructure.GetButton(44)
b.Text = "Hello"
b.BackColor = Color.Blue

I think the C#-Code does assign a type to a var.

Not?

Cheers,
Jay
 
P

Peter Duniho

Jay said:
I was wondering why it doesn't work but was afraid to ask again.
It strikes me that I cannot assign a type to a variable.

Then I do not understand this article from Microsoft:

Where do you see the code you posted? What article?
VB-Code:
With MyDataStructure.GetButton(44)
.Text = "Hello"
.BackColor = Color.Blue
End With

C#-Code:
Dim b as Button = MyDataStructure.GetButton(44)
b.Text = "Hello"
b.BackColor = Color.Blue

The "C#-Code" is not C# at all. There's no "Dim" keyword in C#, and
semi-colons are required to terminate statements.

You might write the code like this:

Button b = MyDataStructure.GetButton(44);

b.Text = "Hello";
b.BackColor = Color.Blue;

That would be legal and does not "assign a type to a var". It assigns
an object instance reference to a variable, and then assigns a couple of
values to properties of that instance.

I'm not really even sure what you mean by "assign a type to a var". If
it's important, you should try to rephrase the question.

Pete
 
J

Jay Willms

Hi Peter,

here is the article:

http://msdn2.microsoft.com/en-us/vcsharp/aa336816.aspx

As the guy in the article says...

Dim b as Button = MyDataStructure.GetButton(44)
b.Text = "Hello"
b.BackColor = Color.Blue

is easier than

MyDataStructure.GetButton(44).Text = "Hello"
MyDataStructure.GetButton(44).BackColor = Color.Blue

But I think I just recognized that the "Dim b as button..." example is
VB... sorry.
But when he writes...
<<
In such situations, there can be a performance benefit over the
equivalent in-line code that duplicates the complex sub-expression:

MyDataStructure.GetButton(44).Text = "Hello"
MyDataStructure.GetButton(44).BackColor = Color.Blue

However, there is equivalent in-line code that is just as performant:

Dim b as Button = MyDataStructure.GetButton(44)
b.Text = "Hello"
b.BackColor = Color.Blue
<<
.... and talks about equivalent then I automatically think of C#, not VB.
Sorry.

Jay.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Jay Willms said:
Hi Peter,

here is the article:

http://msdn2.microsoft.com/en-us/vcsharp/aa336816.aspx

As the guy in the article says...

Dim b as Button = MyDataStructure.GetButton(44)
b.Text = "Hello"
b.BackColor = Color.Blue

The same apply in C#
is easier than

MyDataStructure.GetButton(44).Text = "Hello"
MyDataStructure.GetButton(44).BackColor = Color.Blue

But I think I just recognized that the "Dim b as button..." example is
VB... sorry.
But when he writes...
<<
In such situations, there can be a performance benefit over the equivalent
in-line code that duplicates the complex sub-expression:

MyDataStructure.GetButton(44).Text = "Hello"
MyDataStructure.GetButton(44).BackColor = Color.Blue

However, there is equivalent in-line code that is just as performant:

Dim b as Button = MyDataStructure.GetButton(44)
b.Text = "Hello"
b.BackColor = Color.Blue
<<
... and talks about equivalent then I automatically think of C#, not VB.
Sorry.

Ok, hope it;s clear now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top