Solved

How to write this condtion in VB.net

Posted on 2011-02-20
10
322 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
ID: 34939715
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
ID: 34939736
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
ID: 34939766
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
ID: 34939772
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
ID: 34939786
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 37

Accepted Solution

by:
TommySzalapski earned 300 total points
ID: 34939856
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
ID: 34939895
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
ID: 34939961
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
ID: 34940126
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
ID: 34944483
It worked! Thank you so much for your help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

21 Experts available now in Live!

Get 1:1 Help Now