Solved

How to write this condtion in VB.net

Posted on 2011-02-20
10
321 Views
Last Modified: 2012-05-11
I have very basic question.
What would be the best way to write this condition.
Here is my condtion

I have 4 images.
If ONLY one of the image file exists I would like to make the image width "460"
otherwise, width stays 250 and change visibility accordingly.

Here is what I have so far.

If System.IO.File.Exists(Server.MapPath(strFileName)) Then
            If System.IO.File.Exists(Server.MapPath(strFileName2)) Then
                If System.IO.File.Exists(Server.MapPath(strFileName3)) Then
                    If sketchimg2.Width.ToString < "245" Then
                        sketchimg2.Width = 245
                        sketchimg2.Visible = True
                    End If
                Else
                    sketchimg2.Visible = False
                End If
                If System.IO.File.Exists(Server.MapPath(strFileName4)) Then
                    If cadimg2.Width.ToString < "245" Then
                        cadimg2.Width = 245
                        cadimg2.Visible = True
                    End If
                Else
                    cadimg2.Visible = False
                End If
                If sketchimg.Width.ToString < "245" Then
                    sketchimg.Width = 245
                    sketchimg.Visible = True
                End If
                If cadimg.Width.ToString < "245" Then
                    cadimg.Width = 245
                    cadimg.Visible = True
                End If
            Else
                If sketchimg.Height.ToString < "460" Then
                    sketchimg.Height = 460
                    sketchimg.Visible = True
                End If
            End If
        Else
            sketchimg.Visible = False
        End If\


what would be the best way to achieve this?

Thank you
0
Comment
Question by:jtuttle99
  • 5
  • 3
  • 2
10 Comments
 

Author Comment

by:jtuttle99
Comment Utility
I guess I can do this way but is this the only way?

 If a = True Then
            If b = True Then
                If c = True Then
                    If d = True Then

                    Else
                    End If
                Else

                End If
            Else
                If c = True Then
                    If d = True Then

                    Else
                    End If
                Else
                    If d = True Then

                    Else
                        a = 460
                    End If
                End If
            End If
        Else
            If b = True Then
                If c = True Then
                    If d = True Then
                    Else
                    End If
                Else
                    If d = True Then
                    Else
                        b = 460
                    End If
                End If
            Else
                If c = True Then
                    If d = True Then
                    Else
                        c = 460
                    End If
                Else
                    If d = True Then
                        d = 460
                    Else
                    End If
                End If
            End If
        End If
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
Just count them
int count = 0;
if a
  count++;
if b
  count++;
if c
  count++;
if d
  count++;

if count == 1
{
  //code
}
else
{
  //code
}
0
 

Author Comment

by:jtuttle99
Comment Utility
TommySzalapski:

Thank you for your reply
If I count them, How could I know which one should be the large size?

if count =1  
// code - I should make one of the image large (size 460)

Thank you
0
 

Author Comment

by:jtuttle99
Comment Utility
somthing like this... it's going to be long...

sketchimg.ImageUrl = strFileName
        cadimg.ImageUrl = strFileName2
        sketchimg2.ImageUrl = strFileName3
        cadimg2.ImageUrl = strFileName4
        sketchimg3.ImageUrl = strFileName5

 If System.IO.File.Exists(Server.MapPath(strFileName))  Then
            If System.IO.File.Exists(Server.MapPath(strFileName2)) Then
                If System.IO.File.Exists(Server.MapPath(strFileName3)) Then
                    If System.IO.File.Exists(Server.MapPath(strFileName4)) Then
                              'all exists
                                    If sketchimg.Width.ToString < "245" Then
                                    sketchimg.Width = 245
                                    sketchimg.Visible = True
                                    End If
                                    If sketchimg2.Width.ToString < "245" Then
                                    sketchimg2.Width = 245
                                    sketchimg2.Visible = True
                                    End If
                                    If cadimg.Width.ToString < "245" Then
                                    cadimg.Width = 245
                                    cadimg.Visible = True
                                    End If
                                     If cadimg2.Width.ToString < "245" Then
                        cadimg2.Width = 245
                        cadimg2.Visible = True
                        End If
                    Else
                              '1,2,3
                                    If sketchimg.Width.ToString < "245" Then
                                    sketchimg.Width = 245
                                    sketchimg.Visible = True
                                    End If
                                    If sketchimg2.Width.ToString < "245" Then
                                    sketchimg2.Width = 245
                                    sketchimg2.Visible = True
                                    End If
                                    If cadimg.Width.ToString < "245" Then
                                    cadimg.Width = 245
                                    cadimg.Visible = True
                                    End If
                    End If
                Else
                               If System.IO.File.Exists(Server.MapPath(strFileName4)) Then
                              '1,2,4
                                    If sketchimg.Width.ToString < "245" Then
                                    sketchimg.Width = 245
                                    sketchimg.Visible = True
                                    End If
                                    If cadimg.Width.ToString < "245" Then
                                    cadimg.Width = 245
                                    cadimg.Visible = True
                                    End If
                                     If cadimg2.Width.ToString < "245" Then
                        cadimg2.Width = 245
                        cadimg2.Visible = True
                        End If
                    Else
                              '1,2
                                    If sketchimg.Width.ToString < "245" Then
                                    sketchimg.Width = 245
                                    sketchimg.Visible = True
                                    End If
                                    If sketchimg2.Width.ToString < "245" Then
                                    sketchimg2.Width = 245
                                    sketchimg2.Visible = True
                                    End If      
                    End If
                End If
            Else
                If System.IO.File.Exists(Server.MapPath(strFileName3)) Then
                    If System.IO.File.Exists(Server.MapPath(strFileName4)) Then
                    Else
                    End If
                Else
                    If System.IO.File.Exists(Server.MapPath(strFileName4)) Then
                    Else
                        a = 460
                    End If
                End If
            End If
        Else
            If System.IO.File.Exists(Server.MapPath(strFileName2)) Then
                If System.IO.File.Exists(Server.MapPath(strFileName3)) Then
                    If System.IO.File.Exists(Server.MapPath(strFileName4)) Then
                    Else
                    End If
                Else
                    If System.IO.File.Exists(Server.MapPath(strFileName4)) Then
                    Else
                        b = 460
                    End If
                End If
            Else
                If System.IO.File.Exists(Server.MapPath(strFileName3)) Then
                    If System.IO.File.Exists(Server.MapPath(strFileName4)) Then
                    Else
                        c = 460
                    End If
                Else
                    If System.IO.File.Exists(Server.MapPath(strFileName4)) Then
                        d = 460
                    Else
                    End If
                End If
            End If
        End If
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
Comment Utility
You could do something like this
Public Class Form1

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

        Dim imageFiles() As String = {"c:\windows\system32\calc.exe", _
                                      "f:\windows\system32\notepad.exe", _
                                      "f:\bogus.exe"}

        Dim result() As String = Array.FindAll(imageFiles, AddressOf MoreThanOneExists)

        If result.Length = 1 Then
            ' make the image width "460"
            Debug.Print(result(0))
        Else
            'width stays 250 and change visibility accordingly.
        End If
    End Sub

    Private Function MoreThanOneExists(ByVal path As String) As Boolean
        Return System.IO.File.Exists(path)
    End Function

End Class

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 37

Accepted Solution

by:
TommySzalapski earned 300 total points
Comment Utility
After you get the count just do
if a exists
set it up (MAX_WIDTH/count) or something

if b exists
set it up

No nested ifs needed. You could even make it a function.
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 200 total points
Comment Utility
Yup. You probrably need a way to preserve the path information using an array or even creating a class to hold the information and adding logic into your predicate could help take care of alot of the if blocks.
For example you could create a class that has a method to handle resizing create and instance of the class add it to the array, the predicate will return you the classes that have an existing file than all you do is run the method. Hope that makes sense.

This is just a general idea you can make changes to suit your needs.
Public Class Form1

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

        Dim imageFiles() As ImageFileStuff = {
                                        New ImageFileStuff("c:\windows\system32\calc.exe"), _
                                        New ImageFileStuff("c:\windows\system32\notepad.exe"), _
                                        New ImageFileStuff("f:\bogus.txt")}


        Dim result() As ImageFileStuff = Array.FindAll(imageFiles, AddressOf MoreThanOneExists)

        If result.Length = 1 Then
            ' make the image width "460"
            result(0).Resize()
            Debug.Print("resize {0} ", result(0).FileName)
        Else
            'width stays 250 and change visibility accordingly.
            For Each obj As ImageFileStuff In result
                obj.PreserveSizeAndSetVisibility()
                Debug.Print("change visibilty {0}", obj.FileName)
            Next
        End If
    End Sub

    Private Function MoreThanOneExists(ByVal obj As ImageFileStuff) As Boolean
        ' Predicate
        Return System.IO.File.Exists(obj.FileName)
    End Function

    Private Class ImageFileStuff
        Private _filename As String = String.Empty
        Public Sub New(ByVal fileName As String)
            _filename = fileName
        End Sub
        Public Sub PreserveSizeAndSetVisibility()
            ' TODO: Add logic for visibility
        End Sub
        Public Sub Resize()
            ' TODO: Add logic for resize
        End Sub
        Public ReadOnly Property FileName As String
            Get
                Return _filename
            End Get
        End Property
    End Class

End Class

Open in new window

0
 

Author Comment

by:jtuttle99
Comment Utility
TommySzalapski:
Ahh, Now I get it. I will try the code tomorrow.

egl1044:
Your way is a little bit over my head but I would like to understand your way.
I will also try and let you know.

Thank you so much
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
The reason I like the way I set it up is that you easily use more than two sizes. I've used something like that before to tile windows (actually web page frames). Now it's more built in.
0
 

Author Closing Comment

by:jtuttle99
Comment Utility
It worked! Thank you so much for your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now