Improve company productivity with a Business Account.Sign Up

x
?
Solved

How to write this condtion in VB.net

Posted on 2011-02-20
10
Medium Priority
?
334 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 1200 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 800 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

SingleRun is a tool that ensures that only one instance of an application is started, running it again brings the application to focus.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…

608 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