VB.Net - Is there a better way to write this code?

C

C# Learner

I'd really like other people to chime in and
say what *they'd* like to see on the language groups and what they'd
like to see in .general.

As I've said in the past, I think it'd be better to see strictly
language-related questions on the language groups. An example of such a
question:

What's the difference between 'string' and 'System.String' in C#?

The answer, of course, would be that there is no real practical difference
-- 'string' is an alias for 'System.String'.

I think general .NET questions belong in .general. All that's left for the
reader to do is define what "general" means in this context. <g>

An example of something I'd feel to be on-topic here would be a post about
the status of .NET in the computing world today.

To be honest, I think that all /framework/ questions belong in .framework,
but I don't really mind seeing them here, partly because I don't feel that
they're *very* off-topic -- which they would be in a language group.
 
G

Guest

Hello,

I would like to apologize for double posting this question because I posted
this same question in what looks like the VB 6 newgroups and not the .Net
newsgroup...

Here goes:
The code below works but I just keep looking at it and thinking that there
should be some way to compact it. There are 10 ImagePages and the only
difference is their name propertie. i.e. ImagePage1, ImagePage2,
ImagePage3...

My goal is to remove the Select Case statement and replace it with something
like ImagePage&miCnt. (think that is how I did this in Clipper a long, long
time ago.

The problem is that ImagePage is on the left side of the equal symbol as I
am assigning values to the properties of the ImagePage.

If anyone has a solution for this I would be very grateful. Code snippet
below...

Thanks,
Rhek


For miCnt = 1 To 10

msFileName = myDataRow("Image" & miCnt & "URL").ToString()

If Len(msFileName) > 0 Then

msFileName = msApplicationPath & "img\" & msFileName

Select Case miCnt
Case 1
ImagePage1.picImage.Image = Image.FromFile(msFileName)
ImagePage1.lblFileName.Text = msFileName
ImagePage1.picImage.BorderStyle = BorderStyle.FixedSingle
ImagePage1.editImageCaption.Text =
myDataRow("Image1Caption").ToString()
Case 2
ImagePage2.picImage.Image = Image.FromFile(msFileName)
ImagePage2.lblFileName.Text = msFileName
ImagePage2.picImage.BorderStyle = BorderStyle.FixedSingle
ImagePage2.editImageCaption.Text =
myDataRow("Image2Caption").ToString()
Case 3
ImagePage3.picImage.Image = Image.FromFile(msFileName)
ImagePage3.lblFileName.Text = msFileName
ImagePage3.picImage.BorderStyle = BorderStyle.FixedSingle
ImagePage3.editImageCaption.Text =
myDataRow("Image3Caption").ToString()
Case 4
ImagePage4.picImage.Image = Image.FromFile(msFileName)
ImagePage4.lblFileName.Text = msFileName
ImagePage4.picImage.BorderStyle = BorderStyle.FixedSingle
ImagePage4.editImageCaption.Text =
myDataRow("Image4Caption").ToString()
Case 5
ImagePage5.picImage.Image = Image.FromFile(msFileName)
ImagePage5.lblFileName.Text = msFileName
ImagePage5.picImage.BorderStyle = BorderStyle.FixedSingle
ImagePage5.editImageCaption.Text =
myDataRow("Image5Caption").ToString()
Case 6
ImagePage6.picImage.Image = Image.FromFile(msFileName)
ImagePage6.lblFileName.Text = msFileName
ImagePage6.picImage.BorderStyle = BorderStyle.FixedSingle
ImagePage6.editImageCaption.Text =
myDataRow("Image6Caption").ToString()
Case 7
ImagePage7.picImage.Image = Image.FromFile(msFileName)
ImagePage7.lblFileName.Text = msFileName
ImagePage7.picImage.BorderStyle = BorderStyle.FixedSingle
ImagePage7.editImageCaption.Text =
myDataRow("Image7Caption").ToString()
Case 8
ImagePage8.picImage.Image = Image.FromFile(msFileName)
ImagePage8.lblFileName.Text = msFileName
ImagePage8.picImage.BorderStyle = BorderStyle.FixedSingle
ImagePage8.editImageCaption.Text =
myDataRow("Image8Caption").ToString()
Case 9
ImagePage9.picImage.Image = Image.FromFile(msFileName)
ImagePage9.lblFileName.Text = msFileName
ImagePage9.picImage.BorderStyle = BorderStyle.FixedSingle
ImagePage9.editImageCaption.Text =
myDataRow("Image9Caption").ToString()
Case 10
ImagePage10.picImage.Image = Image.FromFile(msFileName)
ImagePage10.lblFileName.Text = msFileName
ImagePage10.picImage.BorderStyle = BorderStyle.FixedSingle
ImagePage10.editImageCaption.Text =
myDataRow("Image10Caption").ToString()
End Select

End If

Next
 
C

Cor Ligthert

Rhek,

And again not the right newsgroup.
In my opinion is the best one for this

microsoft.public.dotnet.languages.vb

To give you an answer in advance with what you can make your question better
for making the question in that newsgroup. In general has dotNet 3 different
windowforms, which have there own used methods (It is better to use the name
Forms because Page stands more for a webform in dotNet, now I had to look at
image.fromfile because that exist not in a Webform).

The form can be
- a standard form which is showed with frm.show
- a MDI child frm which is added to the collection of the MDI parent
- a standard form which is showed with frm.showdialog

In dotNet exist everything from references so you can use in one ore the
other way those references. However the way those forms are instanced is
important to get the right collection.

In VB6 there was one formarray per form, now has every Control, from which
is the Form one a (Child) Control Collection, while the form has even more.

I hope this helps so far?

Cor
 
J

Jon Skeet [C# MVP]

Rhek said:
I would like to apologize for double posting this question because I posted
this same question in what looks like the VB 6 newgroups and not the .Net
newsgroup...

Here goes:
The code below works but I just keep looking at it and thinking that there
should be some way to compact it. There are 10 ImagePages and the only
difference is their name propertie. i.e. ImagePage1, ImagePage2,
ImagePage3...

My goal is to remove the Select Case statement and replace it with something
like ImagePage&miCnt. (think that is how I did this in Clipper a long, long
time ago.

The problem is that ImagePage is on the left side of the equal symbol as I
am assigning values to the properties of the ImagePage.

If anyone has a solution for this I would be very grateful. Code snippet
below...

Sure, it's very easy: create an array instead, eg ImagePages, and then
use
ImagePages(miCnt).picImage.Image = Image.FromFile(msFileName)

etc.
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
And again not the right newsgroup.
In my opinion is the best one for this

microsoft.public.dotnet.languages.vb

I'm afraid we disagree again, Cor :)

Using an array of controls (my suggested solution) rather than naming
each one separately isn't a language issue, it's a general design
issue.
 
C

Cor Ligthert

Jon,
I'm afraid we disagree again, Cor :)

I was preperad on this one, however I noticed that creating a form in C# is
done often in a
slightly other way than VB.Net.

(You can use the C# method in VBNet, however in VBNet is often as well used
the build in Sub Main (as I do), withouth witing it).

When that was not I had only pointed the OP on the VB newsgroup for language
questions.

(By the way I was typing the same solutions as yours and than thought there
can be better one when it are MDI's because than you can use the
childcollection)

Cor
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
I was preperad on this one, however I noticed that creating a form in C# is
done often in a slightly other way than VB.Net.

Sure - but that doesn't affect *every* question to do with forms.
(You can use the C# method in VBNet, however in VBNet is often as well used
the build in Sub Main (as I do), withouth witing it).

I don't see why that might stop you from using an array of controls...
When that was not I had only pointed the OP on the VB newsgroup for language
questions.

But I still don't see that this is a real language question...
(By the way I was typing the same solutions as yours and than thought there
can be better one when it are MDI's because than you can use the
childcollection)

Well, you could use the Controls collection for non-MDIs. It just
doesn't make much sense to do so, IMO.
 
C

Cor Ligthert

Jon,

Telling that in this case the language.vb newsgroup was a better place for
this question was not the only part in my message, reread that, the rest was
completly answered (prepared on your message) about dotNet in a general way.

I handle now the measurement myself that when I can answer a question
without code it is General, with code it is for a Language newsgroup.

(Not saying that you should handle the same method)

:)

Cor
 
C

Cor Ligthert

Jon,
Well, you could use the Controls collection for non-MDIs. It just
doesn't make much sense to do so, IMO.
Are you sure of that, than that would be a difference with VBNet, however I
doubt that, a form is not added to the Control collection normally.
Otherwise there was no reason for a MDIchildcollection.

So the alternative is in my opinion creating (in any way you do that) that
form array to hold the references.

Cor
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
Are you sure of that, than that would be a difference with VBNet, however I
doubt that, a form is not added to the Control collection normally.

The *form* isn't added to a controls collection, but I don't see where
there are multiple forms. Maybe the VB posting had more information in
it.
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
Telling that in this case the language.vb newsgroup was a better place for
this question was not the only part in my message, reread that, the rest was
completly answered (prepared on your message) about dotNet in a general way.

Sure - I was only responding to the part about what the right newsgroup
was though.
I handle now the measurement myself that when I can answer a question
without code it is General, with code it is for a Language newsgroup.

(Not saying that you should handle the same method)

I very strongly disagree with that method of deciding the best
newsgroup. Something can be a perfectly general question but expressed
in a particular language.

Here's an example:

<question>
How do I read a file which contains accented characters? I've tried the
following code, but it doesn't work:

StreamReader sr = new StreamReader ("foo.txt", Encoding.ASCII);
string text = sr.ReadToEnd();
sr.Close();
</question>

The answer to that question has nothing to do with C# - it has
everything to do with how the framework handles encodings, and what an
encoding is. The question can be answered by anyone who knows about
those issues, regardless of what language they use. It's a general
question, not a language-specific one. Why would you want to redirect
such a question to the C# group?
 
J

Jon Skeet [C# MVP]

<snip>

For what it's worth, I'm not having this same argument with you again
just for the sake of it. I'd really like other people to chime in and
say what *they'd* like to see on the language groups and what they'd
like to see in .general.

At this stage I should mention that a lot of the "general" questions
might actually be better in .framework - there's a lot of overlap
between the two groups. The lack of charters doesn't help :(
 
C

Cor Ligthert

Jon,

We do not agree about that, and we probably will not, when I point an OP on
the language groups, which I normally always do telling with that that he is
free to go to that, it is something I do and find the right way helping
people, when you find that should be in the framework group, feel free to
tell that, you never will see that I make the decissions for the OP and tell
him to go there.

In this case it was to obvious that this OP was searching for a good group
for VBNet questions, because he had previous post his message in a classic
VB newsgroup he told..

That gives another way of writting about this.

Cor
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
We do not agree about that, and we probably will not, when I point an OP on
the language groups, which I normally always do telling with that that he is
free to go to that, it is something I do and find the right way helping
people, when you find that should be in the framework group, feel free to
tell that, you never will see that I make the decissions for the OP and tell
him to go there.

Well, you did specifically say "And again not the right newsgroup."
That's quite different from saying that he was "free to go to" the
language group - it's saying that this is the wrong group for the
question.
In this case it was to obvious that this OP was searching for a good group
for VBNet questions, because he had previous post his message in a classic
VB newsgroup he told..

That gives another way of writting about this.

Yes, it's fine to point out the VB.NET group. That doesn't mean that
this isn't a perfectly reasonable group for his question though.
 
C

Cor Ligthert

Yes, it's fine to point out the VB.NET group. That doesn't mean that
this isn't a perfectly reasonable group for his question though.
Why did you think that I answered the question almost completly, however was
because of the different usual way of creating forms in C# and VBNet in the
opinion that when it should go in code was better in the VBNet newsgroup.

:)

Cor
 
J

Jon Skeet [C# MVP]

Rhek said:
Thanks for the reply. Are you talking about a Control Array? If so, I
thought those were discontinued in VB.Net. If not, then I am not sure how an
array would be used to populate the contents of a UserControl (maybe I should
have mentioned that ImagePages is a UserControl).

I was talking about an array of controls - just a normal array, of type
Control().

You may have to write the code yourself instead of using the designer -
or populate the array in your constructor, after the designer code has
created the ten different variables, but that shouldn't matter, as the
controls are just references.
 
G

Guest

Thanks for the reply again. An array of controls would work nicely. Not
sure how to do it but will dive into the books!

Thanks,
Rhek
 
G

Guest

Hello again,

Thanks for your help, you pointed me in the right direction. After some
more research I solved the problem as follows (condensed version)...

Dim aImagePage(10) As ImagePage

Private Sub frmInventory_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Create the ImagePage Control Array
SetControlArray()

End Sub

Sub SetControlArray()
aImagePage(1) = ImagePage1
aImagePage(2) = ImagePage2
aImagePage(3) = ImagePage3
aImagePage(4) = ImagePage4
aImagePage(5) = ImagePage5
aImagePage(6) = ImagePage6
aImagePage(7) = ImagePage7
aImagePage(8) = ImagePage8
aImagePage(9) = ImagePage9
aImagePage(10) = ImagePage10
End Sub

I was then able to change the properties with...

aImagePage(miCnt).picImage.Image = Image.FromFile(msFileName)
aImagePage(miCnt).lblFileName.Text = msFileName
aImagePage(miCnt).picImage.BorderStyle = BorderStyle.FixedSingle
aImagePage(miCnt).editImageCaption.Text = msCaption

Thanks again!
Rhek
 
N

Nick Malik

Well... I'm a C# developer. I'll do my best at VB.NET code, but I don't
make any promises:

Dim iList as new ArrayList()
Dim curImage as ImagePage ' I don't know what type you are using
here... whatever the type of ImagePage is...
iList.Add(ImagePage1)
iList.Add(ImagePage2)
...
iList.Add(ImagePage10)

For miCnt = 1 to 10
msFileName = myDataRow("Image" & miCnt & "URL").ToString()

If Len(msFileName) > 0 Then

msFileName = msApplicationPath & "img\" & msFileName

curImage = iList(miCnt)
curImage .picImage.Image = Image.FromFile(msFileName)
curImage .lblFileName.Text = msFileName
curImage .picImage.BorderStyle = BorderStyle.FixedSingle
curImage .editImageCaption.Text =
myDataRow("Image1Caption").ToString()
End If
Next

Sorry if there are syntax errors. Like I said, I'm a C# person now. I
moved to C# directly from VB6 about 2 years ago.
HTH,
--- Nick
 
C

Cor Ligthert

Nick,

In my opinion had been the most simple code in this in VBNet.

Private imgpages() as myUserControl

And in the load event
Dim imgpages = New myUserControl() {Imagepage1, Imagepage2 etc}

However that has the same effect as Rhek made, which would not go for real
Forms where I though he was speaking about, because he used the word pages,
this goes only for non top controls.

Cor
 

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