Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6577357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:37:31+00:00 2026-05-25T15:37:31+00:00

I’m working on a Unit Conversion Module. I’ve found several good ideas here, as

  • 0

I’m working on a Unit Conversion Module. I’ve found several good ideas here, as well as on CodeProject. My code looks very similar to this C# code at http://www.codeproject.com/KB/cs/Unit_Conversion_Sample.aspx
From the following you’ll probably gather that I’m pretty new to programming:)

I’ve created a Units base class that I inherit to create each unit type.

Public Class Units
Private _unitvalue As Double
Private _unittype As [Enum]

Public Sub New(UnitValue As Double, UnitType As [Enum])
    _unitvalue = UnitValue
    _unittype = UnitType
End Sub

Public Property UnitValue() As Double
    Get
        Return _unitvalue
    End Get
    Set(value As Double)
        _unitvalue = value
    End Set
End Property

Public Property UnitType() As [Enum]
    Get
        Return _unittype
    End Get
    Set(value As [Enum])
        _unittype = value
    End Set
End Property

Public Overrides Function ToString() As String
    Return String.Format("{0} {1}", UnitValue.ToString(), UnitType.ToString())
End Function

End Class

I then inherit this class to start creating Units with a Unit conversion function included.

Public Class WeightUnit
Inherits Units
Enum WeightSym
    'Pounds
    Lbs
    'Kilograms
    Kg
End Enum
Sub New(UnitValue As Double, UnitType As WeightSym)
    MyBase.New(UnitValue, UnitType)
End Sub
Public Function Convert(toUnit As WeightSym) As WeightUnit
    'Base Weight Unit is Lbs

    Dim fromUnit As WeightSym
    fromUnit = UnitType

    Dim Lbs As Double = 0
    Select Case fromUnit
        'Standard
        Case WeightSym.Lbs
            Lbs = UnitValue
        Case WeightSym.Kg
            Lbs = UnitValue * 2.2046226
    End Select

    Dim toVal As Double = 0
    'to unit based on Lbs
    Select Case toUnit
        'Standard
        Case WeightSym.Lbs
            toVal = Lbs
        Case WeightSym.Kg
            toVal = Lbs * 0.4535924
    End Select
    Return New WeightUnit(toVal, toUnit)
End Function
End Class

I need to create several different Unit types, such as Length, Pressure, etc. This is working well except for one issue. I’d like to be able to change the UnitType, and automatically update the UnitValue. Such that if the Unit object has a value of 1 and a type of Inch, and the type is changed to Cm, the value would update to 2.54.

Something like this…. I’ve seen examples of this, but the difference here is that, I can’t specify the Covert function in my base class because it changes with each new UnitClass I create.

Public Property UnitType() As [Enum]
Get
    Return _unittype
End Get
Set(value As [Enum])
    _unittype = value
    _unitvalue = Convert(value).UnitValue
End Set
End Property

I tried making the Property UnitType Overridable and creating a new Override Property for UnitType in each UnitClass that I created, but I failed to get that to work.

Any suggestions are much appreciated.
Thanks much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:37:31+00:00Added an answer on May 25, 2026 at 3:37 pm

    I’m answering my own questions as I found a way to accomplish updating the UnitValue and UnitType, by slightly rearranging my code. I broke the Convert Function out into two pieces. First I created a WeightConversion Function that just returns a Double.

    Public Function WeightConversion(Quantity As Double,fromUnit As WeightSym,_
    toUnit As WeightSym) As Double
    'Base Weight Unit is Lbs
    
    Dim fromUnit As WeightSym
    fromUnit = UnitType
    
    Dim Lbs As Double = 0
    Select Case fromUnit
        'Standard
        Case WeightSym.Lbs
            Lbs = Quantity
        Case WeightSym.Kg
            Lbs = Quantity * 2.2046226
    End Select
    
    Dim toVal As Double = 0
    'to unit based on Lbs
    Select Case toUnit
        'Standard
        Case WeightSym.Lbs
            toVal = Lbs
        Case WeightSym.Kg
            toVal = Lbs * 0.4535924
    End Select
    Return toVal
    End Function
    

    Then, I created a function that looks like this… In my implementation I made an overridable function in my base class, and override it with the below.

    Public Function ToUnit(toType As LengthSym) As Boolean
        'First Set UnitValue 
        Me.UnitValue = LengthConversion(Me.UnitValue, Me.UnitType, toType)
        'Then Set the UnitType to the newly choosen unit
        Me.UnitType = toType
        Return True
    End Function
    

    This gave me the effect I was looking for… Now converting to another UnitType simply changes the values on the existing object instead of creating a new Object.

    I’d still be interested in hearing other ideas for unit conversion…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.